several tips on java and Matlab

It is better to use the feval() method. This gives you the

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)

System.out.println("ERROR!");

}

 

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

function.

 

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.

 

In MATLAB:

 

function result=myeval(expr)

result=eval(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)

System.out.println("ERROR!");

}

 

 

 

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:

 

MATLAB:

 

myApplication=MY_JAVA_GUI_CLASS

while (true)

{

if (myApplication.readyToTakePArameters())

{

some_array=myApplication.readParameters();

calculate_sth_in_matlab

do_something_with_results_ie_show

myApplication.clearFlag()

}

sleep (1) #let user think end enter sth

}

 

 

JAVA

class MY_JAVA_GUI_CLASS

{

boolean readyToTakePArameters = false;

constructor()

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. 

 

 

 

 

 

 

/

 

Artful_Bobster,

 

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 (http://java.sun.com/docs/books/tutorial/native1.1/). 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 :

(http://matlabdb.mathematik.uni-stuttgart.de/download.jsp?MC_ID=9&MP_ID=163)

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!

 

Alex

 

/

 

 

我翻译的那篇文章

 

 

/

 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.

 

Thanks.

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!

 

Ironluca

 

//

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值