several tips on java and Matlab

It is better to use the feval() method. This gives you the<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

output as an Object and you can try-catch the call.


Object out;

Matlab matlab = new Matlab();

try {

out=matlab.feval("sin", new Object[]{new double[]{17}}, 1);

} catch (Exception e)




The syntax of the feval method is as follows:


Argument 1: A string giving the name of the function

Argument 2: An Object array where you pass the inputs to the function.

The same convertion rules that are used when calling

Java methods on the MATLAB command line apply here.

Argument 3: An integer specifying the number of outputs from the



The output is returned as an Object that you can cast to the

appropriate type to pick up the output.


If you only want to do a simple eval in MATLAB you could pass the

string that you want to eval to a function that only has one line

that performs an eval of the string.




function result=myeval(expr)



In Java:


Object out;

Matlab matlab = new Matlab();

String expr = "sin(17)*cos(0.5667)/67";

try {

out=matlab.feval("myeval", new Object[]{expr}, 1);

} catch (Exception e)







I need to call Matlab from Java, run some programs in matlab and get the standard output.

I know I can start matlab with:


Process proc = Runtime.getRuntime().exec("matlab");


But then how do i run a program in matlab? (For example, if I have "myprogram.m", I would run it in matlab by typing "myprogram")


If I start a new process [Process proc2 = Runtime......exec("myprogram")], it does not start in matlab.


Any ideas?

thanx in advance.



Sounds like myprogram is just a parameter of a call to matlab.


Try some variations on

exec("matlab myprogram");


There is also - I believe - an exzec call that takes an array of parameters.






What i did is: create Gui in Java and run it from Matlab command line and force matlab to gather data from GUI by setting a flag in JAVA - all calculations were done in Matlab.

And there comes another problem: there is only one possibility to send anything from Java to Matlab: using a loop in matlab checking the java application if it finished preparing data to calculate


i don't remember the exact code, but if you insist i could give you some samples.

in meta-code it looks like this:





while (true)


if (myApplication.readyToTakePArameters())







sleep (1) #let user think end enter sth







boolean readyToTakePArameters = false;


public boolean readyToTakePArameters()


return readyToTakePArameters;


// action made by pushing a button All_Parameters_given (or pushing Enter Key by user)

public someMethod(action)


if (action) readyToTakePArameters =true;


public clearFlag()


readyToTakePArameters = false;





I don't know what has changed in 6.1, so check it first.



Summing up:

When I do next project with mathematical calculation, i will use pure Java with some

mathematical functions and algorithms written in Java. You may write it by yourself or download it somewhere from web. I do not reccommend using Matlab with Java. 











There are a few ways to call Matlab from Java programs:


1) Use the com.mathworks.jmi.jar api, this is undocumented and unsupported by mathworks. Plus, as far as i can tell it doesnt work! The only thing that ive had happen when ive tried to use simple matlab commands (like pwd) is that it throws a MatlabException (whatever that is!)


2) Use UNIX scripts to transfer text files containing your values/parameters between your java app and your instance of matlab. I didnt like this idea!


3) Use the Matlab Compiler to compile your mfile functions that you want to call, as dlls (or whatever) and then interface with them via the JNI ( The disadvantage with this is is means that your program will be tied to the platform that you compile the mfiles on. This is the method that mathworks recommend on their website, and i think that it works (it is what im trying to do at the moment).


4) I have also found today :


which provides ways of calling matlab from java.


Hopefully mathworks will get their fingers out and provide better connectivity to java (like they do to c) in future versions of matlab.


I hope that these resources are of some use to people, because its taken me ages to find a way to do this!











 have written a program in Matlab which creates a matrix. I now need to use this matrix in my Java code but i dont know how to do this. One suggestion I was given was to save the Matlab matrix as a binary file (easy) and then load the file in Java. Does anyone know how I could do this? And are there better ways? I think there are ways of running Matlab code from Java and vice versa but i dont know what that involves.



Well, OX


1) Use the Runtime.exec() method to execute the Matlab program


2) Get the InputStream from the Process object that the exec() method generates.


3) Modift the Matlab program to write the matrix in some format to the standard OutputStream of the Matlab program - for formating, you can take a row and print it to the OutputStream as a LWS separated line of text terminating with a CR-LF


4) Read the text line from the InputStream in the Java program.


5) Parse the string (may be with a StringTokenizer) and construct the matrix in a 2d array or by some other means.


Hope it helps!





1st way to do that, either run the matlab into the JVM, so you need some JMatlab package to do so, I do know and don't think that exist right now.


2nd way to do that, using java.lang.Runtime, launch Matlab, the real one. The problem with this method is that the interaction with the matlab process is reduced to stream out/err/in communication until Matlab have some kind of Matlabserver which again I'm pretty sure it doesn't support right now.


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他