views:

1650

answers:

4

How do I pass a matlab function handle to a Java object and invoke it from within Java (that is, I want Java to tell matlab when it is ready with a calculation). I am trying to use the com.mathworks.jmi.Matlab class for evaluating Matlab expressions in the Java object, but I can't see how to 1) transfer the callback funcktion handle to Java, and 2) invoke it from Java possibly using the com.mathworks.jmi.Matlab class.

Thanks, jakob

+2  A: 

Hmm. Looks like JMI is one of those matlab internals things which may be subject to change in future versions. I found these online articles, not sure if they will help in your case.

Jason S
Thanks, I have found those articles but they don't tell me how to pass function handlescallbacks to java and invoke them from java. The only thing I got to work through jmi was to evalue matlab commands from java, but I need to tell matlab (from java) to execute a specific function handle.
my guess is you can't do it. I've run into a similar issue (trying to pass in a Matlab function as a callback interface into a java call from MATLAB -- no apparent way to marshal into matlab) and ran out of luck.
Jason S
A: 

To pass a callback into matlab, you pass the name of the matlab function and it's arguments into com.mathworks.jmi.Matlab.feval("matlabControlcb", ... , 0); The ... is an Object[] where [0] = command and [1..end]=arguments.

See Also: FEVAL FUNC2STR

KitsuneYMG
A: 

I'm afraid that I do not know of a way to do exactly what you requested, but there are two ways you can do something similar:

1) If you set a Java callback from within Matlab, you can send it a Matlab function handle. When the Java callback event gets raised, your Matlab function will be invoked. An example for this can be found here: http://UndocumentedMatlab.com/blog/uicontrol-callbacks/. Note that this does not need JMI to run.

2) JMI needs a string (function name or command line) for Matlab to evaluate. If you know the function name in advance you can use it. Otherwise, you can prepare a Matlab switch-yard function (whose name is known in advance) to dynamically direct your callback to the appropriate Matlab action.

I plan to run an article about JMI in the http://UndocumentedMatlab.com website in mid-April, so stay tuned...

Yair Altman

Yair Altman
A: 

try this tool: http://jamal.sourceforge.net/

It does exactly what you need and well-documented.

The main principle it is based on is to make RMI call to the server part that is run inside Matlab. The output is returned to java program. Again, it depends on jmi...

Fred