从MATLAB调用Java? [英] Calling Java from MATLAB?
问题描述
我希望Matlab程序调用一个java文件,最好带一个例子。
I want Matlab program to call a java file, preferably with an example.
推荐答案
好的,我会尝试在这里给出一个小例子。可以像在zellus建议的那样使用Matlab窗口中的java函数,或者,如果需要,可以创建自己的java类。这是一个例子:
Ok, I'll try to give a mini-example here. Either use the java functions right from the Matlab window as zellus suggests, or, if need permits, create your own java class. Here's an example:
package testMatlabInterface;
public class TestFunction
{
private double value;
public TestFunction()
{
value = 0;
}
public double Add(double v)
{
value += v;
return value;
}
}
然后将其转换为jar文件。假设您将文件放在名为 testMatlabInterface
的文件夹中,请在命令行运行此命令:
Then turn it into a jar file. Assuming you put the file in a folder called testMatlabInterface
, run this command at the command line:
jar cvf testMatlab.jar testMatlabInterface
然后,在Matlab中,导航到目录你的 testMatlab.jar
文件所在的位置并运行命令 import testMatlabInterface。*
以导入所有类中的所有类 testMatlabInterface
包。然后你可以像这样使用这个类:
Then, in Matlab, navigate to the directory where your testMatlab.jar
file is located and run the command, import testMatlabInterface.*
to import all the classes in the testMatlabInterface
package. Then you may use the class like so:
>> methodsview testMatlabInterface.TestFunction
>> me = testMatlabInterface.TestFunction()
me =
testMatlabInterface.TestFunction@7e413c
>> me.Add(10)
ans =
10
>> me.Add(10)
ans =
20
>> me.Add(10)
ans =
30
如果我能得到进一步的帮助,请告诉我。
Let me know if I can be of further assistance.
这篇关于从MATLAB调用Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!