f



Running MATLAB code from Java

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
1/23/2006 12:58:55 PM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

9 Replies
1280 Views

Similar Articles

[PageSpeed] 43

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

0
bradphelan1 (255)
1/23/2006 12:52:28 PM
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

0
1/23/2006 1:48:18 PM
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

0
1/23/2006 1:48:35 PM
[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 

0
toreb (21)
1/23/2006 3:16:39 PM
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
0
1/23/2006 4:25:34 PM
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
0
1/31/2006 11:49:54 PM
[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 
0
toreb (21)
2/1/2006 9:57:44 AM
[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
  
0
toreb (21)
2/1/2006 1:28:53 PM
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
0
2/1/2006 1:34:57 PM
Reply: