Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a python function to a matlab function? #89

Open
disadone opened this issue Aug 7, 2020 · 5 comments
Open

Pass a python function to a matlab function? #89

disadone opened this issue Aug 7, 2020 · 5 comments

Comments

@disadone
Copy link

disadone commented Aug 7, 2020

I am a little bit confused about how to use transplant.Matlab
I want to pass a python function and several other parameters to a matlab function, for example matlab.fminsearch. So I try

import transplant

matlab = transplant.Matlab()
def pyfunc(x):
    return x+1
func=transplant.MatlabFunction(matlab,pyfunc)

But func(1) returns

TypeError: can not serialize 'function' object

Many Thanks

@bastibe
Copy link
Owner

bastibe commented Aug 11, 2020

That is not possible at the moment, I'm afraid. But you can pass Matlab functions to Matlab:

func = matlab.eval("@(x) x+1")

@disadone
Copy link
Author

Thank you!Bastibe. If one can pass a function, that will be really cool.

@bastibe
Copy link
Owner

bastibe commented Aug 14, 2020

It can be done, but I don't have the time to implement the functionality at the moment, sorry.

One would have to create a proxy function on the Matlab side that wraps a call back to Python. At the moment, there is no back channel for Matlab to ask things from Python, so this is not a trivial thing to do.

@bigscientist
Copy link

Perhaps data interface such as TCP/IP can works. If the datatype can be freely transported , the tool will be more awesome

@bastibe
Copy link
Owner

bastibe commented Dec 25, 2020

The data is not the (biggest) issue. Transplant already serializes all data through a TCP connection or a local socket. The problem is that you have to create a function inside Matlab that behaves just like the Python function.

This is easier when passing a Matlab function to Python: A new function object is created in Python's local scope that passes all its arguments to Matlab, computes the results there, then passes the result or error back to Python.

But the other way around is much more demanding: How would you handle keyword arguments? Is it safe to create a function in Matlab's global scope? How do you handle generators, or async functions, or callable objects? How would you handle a Python function that returns something that is not serializable to Matlab data? How would you handle Exceptions? The thing is, Python is a much more powerful language than Matlab. Representing Matlab functions and data in Python is possible because Matlab functions and data are simple things. Representing a Python callable in Matlab is much harder, because they can do so much more than Matlab's functions.

It can be done, but it is not a trivial thing to do. There will be plenty of corner cases.

(But you can already grab a Matlab function and pass it back to Matlab.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants