tags:

views:

413

answers:

3

I have an .m file in MATLAB which I would like to call from Java an get the solution as a string or whatever in Java. This sounds really simple but for some reason I can't make it work.

I tried this:

matlab -nosplash -wait -nodesktop -r  myFunction

but I'm not sure how I parse the answer since MATLAB opens it's own command line (in Windows).

I use this, but it doesn't return anything.

Process p = Runtime.getRuntime().exec(commandToRun);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

also it seems that every time I call MATLAB it opens a separate window which is a problem because I'd like to run this many times.

+1  A: 

Take a look at this webpage where someone has created classes to call Matlab.

Rob Spieldenner
+1  A: 

The trick is to use the MatlabControl class http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. It's very easy to use and you can do exactly what you're trying to do (and more).

Jeff Storey
Jeff! Rob! Good to see you guys on here.
Scottie T
Small world, isn't it?
Jeff Storey
A: 

There exists a good Java-COM-Bridge called JaCoB (http://sourceforge.net/projects/jacob-project/) which you can use to automatically start Matlab as a COM-Server in the background. You can then follow the instructions in the Matlab help to interact with the Matlab COM Interface.

Although this is a very generic interface, it provides enough flexibility to easily do a few calls to Matlab like in your case.

Simply download the JaCoB package and look in the docs folder for some documentation. You also have to include the Jacob DLL in your path.

Wolfgang Ulmer