java和matlab混合编程

来源:互联网 发布:java高并发面试题实例 编辑:程序博客网 时间:2024/05/29 14:58

1. 从matlab 2006b版本起,开始提供javabuilder工具箱,以支持向java提供编程接口。

2. 安装matlab编译Runtime: MCR(例如:MATLAB/toolbox/compiler/deploy/win64/MCRInstaller.exe)。

3. 在matlab command window中使用deploytool命令打开deploy窗口。

    3.1 新建工程(将被编译为java中的一个Class)

    3.2 向工程添加m文件(每个m文件将被编译为一个method)

    3.3 build工程,matlab将于指定目录生成相应的jar包(例如MProj.jar)和javedoc文件。

4. 在eclipse中新建java工程,将matlab toolbox中javabuilder.jar和生成的MProj.jar置于java工程的类路径下。

5. OK,可以开始使用javabuiler.jar和MProj.jar提供的接口开始java编程了!javabuilder.jar的API javadoc可在matlab的帮助文档中查询,MProj.jar的API javadoc已在上面2.3中生成。

 

一个javabuilder工具箱中自带的例子:Compute Magicsquare square and print result

Java代码  收藏代码
  1. import com.mathworks.toolbox.javabuilder.*;  
  2. import magicsquare.*;  
  3.   
  4. public class TestMatlab {  
  5.     public static void main(String[] args) {  
  6.         MWNumericArray n = null/* Stores input value */  
  7.         Object[] result = null/* Stores the result */  
  8.         Magicsquare theMagic = null/* Stores magic class instance */  
  9.         try {  
  10.             /* If no input, exit */  
  11.             if (args.length == 0)  
  12.             {  
  13.                 System.out.println("Error: must input a positive integer");  
  14.                 return;  
  15.             }  
  16.             /* Convert and print input value */  
  17.             n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE);  
  18.             System.out.println("Magic square of order " + n.toString());  
  19.   
  20.             /* Create new magic object */  
  21.             theMagic = new Magicsquare();  
  22.   
  23.             /* Compute Magicsquare square and print result */  
  24.             result = theMagic.makesqr(1, n);  
  25.             System.out.println(result[0]);  
  26.         } catch (Exception e) {  
  27.             System.out.println("Exception: " + e.toString());  
  28.         } finally {  
  29.             /* Free native resources */  
  30.             MWArray.disposeArray(n);  
  31.             MWArray.disposeArray(result);  
  32.             if (theMagic != null)  
  33.                 theMagic.dispose();  
  34.         }  
  35.     }  
  36. }  

 

6. 以上步骤已经可以搞定使用matlab接口编程的大多数情况,但目前为止(我用的是2009a),貌似matlab还不能编译训练模型的函数,如时间序列模型armax(),调用java程序报错:

Undefined function or method 'armax' for input arguments of type 'iddata'.

不知该如何在java中使用armax()这样的函数.