从 MATLAB 调用 Java? [英] Calling Java from MATLAB?

查看:35
本文介绍了从 MATLAB 调用 Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆