Hi Everyone, I'm trying to find a way to call MATLAB computational functionality from Java code - in much the same way as the MATLAB engine appears to run for C and FORTRAN. The Java stand-alone application is running on UNIX. MATLAB can obviously be installed on Windows or UNIX (or other platforms for that matter). I need to push input from Java to MATLAB and then to receive MATLAB output back into Java. This seems to be an infrequent yet repeating request in the newsgroup archives and so I'll summarise my progress below. Any comments on these approaches and any new approach ideas would be really appreciated. 1. Use the MATLAB Compiler to compile the .m file to C/C++ code and then use the JNI to call the compiled code from Java. (This seems to be a Mathworks solution, but seems to be difficult for an inexperienced Java programmer like myself) 2. Use Web Services / Web Server toolbox to expose input/output information. (Haven't researched this avenue very thoroughly). 3. Use COM to start MATLAB as an automation server and an add-in such as JACOB (by Dan Adler) to bridge between Java and COM. (This will probably not work with Java running on UNIX). 4. Use the MatlabControl class (developed by Kamin Whitehouse) and MatlabJava server (developed by Bowen Hui)to call Matlab commands directly from Java. 5. Pay for J-Integra for COM - commercial software that should make MATLAB callable from Java on UNIX (at least that's how I understand their example). If anyone has experience in any of these approaches or another approach perhaps, I would really appreciate hearing from you. Thanks, Rob
![]() |
0 |
![]() |
Rob McIntyre wrote: > Hi Everyone, > I'm trying to find a way to call MATLAB computational functionality > from Java code - in much the same way as the MATLAB engine appears to > run for C and FORTRAN. The Java stand-alone application is running on > UNIX. MATLAB can obviously be installed on Windows or UNIX (or other > platforms for that matter). I need to push input from Java to MATLAB > and then to receive MATLAB output back into Java. > > This seems to be an infrequent yet repeating request in the newsgroup > archives and so I'll summarise my progress below. Any comments on > these approaches and any new approach ideas would be really > appreciated. > > 1. Use the MATLAB Compiler to compile the .m file to C/C++ code and > then use the JNI to call the compiled code from Java. (This seems to > be a Mathworks solution, but seems to be difficult for an > inexperienced Java programmer like myself) > > 2. Use Web Services / Web Server toolbox to expose input/output > information. (Haven't researched this avenue very thoroughly). > > 3. Use COM to start MATLAB as an automation server and an add-in such > as JACOB (by Dan Adler) to bridge between Java and COM. (This will > probably not work with Java running on UNIX). > > 4. Use the MatlabControl class (developed by Kamin Whitehouse) and > MatlabJava server (developed by Bowen Hui)to call Matlab commands > directly from Java. > > 5. Pay for J-Integra for COM - commercial software that should make > MATLAB callable from Java on UNIX (at least that's how I understand > their example). > > If anyone has experience in any of these approaches or another > approach perhaps, I would really appreciate hearing from you. > > Thanks, > Rob For fun I wrote a distributed computing toolbox for Matlab which you can download off my website. Though not exactly what you want some of the back end code is instructive as it uses Java to communicate between several Matlab sessions and you could that code to talk directly to the remote matlab from Java. http://xtargets.com/cms/Tutorials/Matlab-Programming/Matlab-2-Matlab-Distributed-Computing-Toolbox.html -- Brad http://xtargets.com/cms/Tutorials/Matlab-Programming/Simulink-RTW-Legacy-Code-Device-Driver-Tool.html
Just so you are aware, J-Integra works and is reliable because it is a pure Java implementation of the DCOM protocol. It works just like a VB client does to access MATLAB. Obviously if you can find a free solution that does the job for you, go with that. Otherwise, try the free 30 day evaluation of J-Integra and see if it meets your requirements. Shane Sauer J-Integra Interoperability Solutions http://j-integra.intrinsyc.com/ high performance interop middleware for java, corba, com & .net
Just so you are aware, J-Integra works and is reliable because it is a pure Java implementation of the DCOM protocol. It works just like a VB client does to access MATLAB. Obviously if you can find a free solution that does the job for you, go with that. Otherwise, try the free 30 day evaluation of J-Integra and see if it meets your requirements. Shane Sauer J-Integra Interoperability Solutions http://j-integra.intrinsyc.com/ high performance interop middleware for java, corba, com & .net
[Rob McIntyre] | | Any comments on these approaches and any new approach ideas | would be really appreciated. | | 4. Use the MatlabControl class (developed by Kamin Whitehouse) and | MatlabJava server (developed by Bowen Hui)to call Matlab commands | directly from Java. Hi Rob, I'm using some code, essentially identical to MatlabControl by Kamin Whitehouse, and find it to be recommendable. It works as advertized. There's a limitation as to what you can receive back from Matlab, but anything that can be represented by the java primitives as well as text strings and arrays of all these go. You can pass java objects to Matlab and convert them to structs. Because of the single-threaded nature of Matlab, the methods in Kamin's class need to be called in new java threads that you have to introduce in your own user code. If you want synchronous execution, you therefore need to implement utility code that wait()s the calling thread and wakes it up at the proper time. Rgds, -Tore
Hi Rob I have a little experience in this as I am investigating the feasibility myself at the moment. So only early days. I have used Brad Phelan's snippet MatlabFunction.java. I have used MatlabControl.java from Kamin Whitehouse and MatServer.java from Bowen Hui. All successfully. The main issue, I think, and looming on the horizon for me, is how easy/feasible it is to receive Matlab objects into java. JIntegar must have done it but I can find little information on how to do it so far. Bowen Hui's solution is to create an output file "cp.txt" and pass it back that way to Java. The other concern is how much Mathworks will support the interface and how stable it will be. Regards John Seers
Tore - what version of Matlab are you running? I tried you code and am getting an error with using R14 and the following line: MatlabFevalBCommand extends MatlabFevalCommand The error is: MatlabFevalCommand cannot be resolved to a type Thanks, Gary
[gary.v.willenbring@saic.com] | | Tore - what version of Matlab are you running? I tried you code and | am getting an error with using R14 and the following line: | | MatlabFevalBCommand extends MatlabFevalCommand | | The error is: | MatlabFevalCommand cannot be resolved to a type Oops, Try to remove 'extends MatlabFevalCommand'. I trimmed down the code a bit before posting, and obviously missed that one. I'm using R14 SP3, by the way. -Tore
[gary.v.willenbring@saic.com] | | Tore - one other error with the line below: | | Matlab.whenMatlabReady(new MatlabFevalBCommand(cmd, args, this)); | | Error is: | The method whenMatlabReady(Runnable) in the type Matlab is not | applicable for the arguments (MatlabEval.MatlabFevalBCommand) I'm sure you can see what's wrong. 'MatlabFevalBCommand' has a run() method. It must declare that it implements Runnable. So change 'MatlabFevalBCommand extends MatlabFevalCommand' in the original posting with 'MatlabFevalBCommand implements Runnable', and see if it works any better. -Tore
Tore - one other error with the line below: Matlab.whenMatlabReady(new MatlabFevalBCommand(cmd, args, this)); Error is: The method whenMatlabReady(Runnable) in the type Matlab is not applicable for the arguments (MatlabEval.MatlabFevalBCommand) Gary