bastibe / transplant

Transplant is an easy way of calling Matlab from Python
https://transplant.readthedocs.io
Other
110 stars 26 forks source link

Function handles #59

Closed wardsimon closed 6 years ago

wardsimon commented 6 years ago

Dear Bastian,

Is there a way of using function handles in function arguments?

For example in matlab syntax:
result = gateway_func('func',@func,'vars',xx,'additional',yy); I've tried:
result = matlab. gateway_func('func',matlab.str2func('func'),'vars',xx,'additional',yy) Without success as the matlab_function object cannot be transferred.

Any help or suggestions would be great.

Simon

bastibe commented 6 years ago

Function handles can be passed back to Matlab, i.e.

result = matlab.gateway_func('func', matlab.func, 'vars', xx, 'additional', yy)

As you know, matlab.func will create a Python object that calls the Matlab function func. However, just like any other Matlab object, Transplant will convert it back to the original Matlab function if you pass it back to Matlab.

By the way, we recently added a fun new feature that allows you to write that same call as

result = matlab.gateway_func(func=matlab.func, vars=xx, additional=yy)

Transplant now auto-converts keyword arguments to Matlab-style key-value lists.

wardsimon commented 6 years ago

Thanks for the response. I've tried as suggested but still can't get it to work. The error is:

TypeError: can't serialize <transplant.transplant_master.Matlab._decode_function.<locals>.matlab_function object at 0x7f5444531828>

It seems to be a problem with Transplant converting function object from python back into Matlab . I've chosen a simple example which shows the error:

matlab.feval(matlab.plus,1.,2.)

bastibe commented 6 years ago

Thank you for your bug report! This indeed does not work.

I'll look into it later today.

bastibe commented 6 years ago

The latest commit should fix your issue. Can you confirm that the fix works?

wardsimon commented 6 years ago

Thanks for the speedy fix, it works for both my test case and real case.

bastibe commented 6 years ago

Cool! I uploaded the new version to PyPi.