several tips on java and Matlab

转载 2004年07月05日 19:12:00

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)

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.

Tips and Tricks For CNN Structure

http://deeplearning.net/tutorial/lenet.html#tips-and-tricks -------------------------------------...
  • XLcaoyi
  • XLcaoyi
  • 2016年05月29日 23:31
  • 338

06-9-14:several Tips

1、FlashPGM有的时候会出现Unable to set board to Background mode错误,解决方法:修改某跳线 ,启动,进入FlashPGM的烧写界面,然后跳回来。2、当初没...
  • saintony
  • saintony
  • 2006年09月14日 01:05
  • 720

Java Tips

1、对象本身已知的事物称为实例biangl
  • u010264321
  • u010264321
  • 2014年05月02日 23:10
  • 743

Matlab Tips

fft各个点对应的频率值时 [0,n) * Fs / N. whos可以
  • hbisheng
  • hbisheng
  • 2014年11月25日 09:36
  • 155

matlab tips

Matlab高阶应用——matlab工程程序转成C,并在VC2010环境下编译运行(codesgen) 参考:http://www.chinadmd.com/file/xtvatuzuiws...
  • shenjiangrong
  • shenjiangrong
  • 2016年02月26日 14:11
  • 545

tips for matlab

1. help: 最有效的命令2. see also:不可小瞧的关联 3. Edit:查看m源文件的助手 在应用matlab过程中,可能我们想看看它的m源文件,当然用editor定位打开也行,但是我经...
  • sarah2014
  • sarah2014
  • 2010年01月30日 20:50
  • 319

Effective Java读书笔记三(Java Tips.Day.3)

TIP13.使类和成员的可访问性最小化(封装) 实例域决不能是公有的 不应当具有公有的静态final数组域,或者返回这种域的方法。作为替代,你可以使公有数组变成私有的,并增加一个公有的不可变列表: p...
  • cangely
  • cangely
  • 2016年11月18日 17:16
  • 381

java tips

1 当前路径:System.getProperty("user.dir"); 启示:注意System的属性。2 更多路径问题:http://www.blogjava.net/ericwang/arch...
  • yethyeth
  • yethyeth
  • 2007年04月09日 15:08
  • 550

腾讯QQ精准TIPS消息营销介绍

Tips消息:Tips消息是一种腾讯特有的消息精准告知方式,是在QQ用户在线时,不定时触发的一个图文并茂的消息框,该消息框在屏幕窗口右下角弹出,用户不主动点击则不会在桌面消失,保持时间长,点击效率高,...
  • lzlchangqi
  • lzlchangqi
  • 2016年10月16日 17:45
  • 673

Java代码优化TIPS

类名的首字母大写,变量,对象,方法的首字母小写,包全部小写。 实现一个类时,尽量的实现equals(),hashCode(),toString(),clone(),Serializable()方法。 ...
  • jennychensz
  • jennychensz
  • 2005年06月13日 17:57
  • 388
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:several tips on java and Matlab
举报原因:
原因补充:

(最多只允许输入30个字)