`
华山剑
  • 浏览: 19867 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论
  • lyyf: 马马虎虎明白了点。但是,接口,搞成私有的,别的类能使用吗?
    线程安全

java和matlab混合编程

阅读更多

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

import com.mathworks.toolbox.javabuilder.*;
import magicsquare.*;

public class TestMatlab {
	public static void main(String[] args) {
		MWNumericArray n = null; /* Stores input value */
		Object[] result = null; /* Stores the result */
		Magicsquare theMagic = null; /* Stores magic class instance */
		try {
			/* If no input, exit */
			if (args.length == 0)
			{
				System.out.println("Error: must input a positive integer");
				return;
			}
			/* Convert and print input value */
			n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE);
			System.out.println("Magic square of order " + n.toString());

			/* Create new magic object */
			theMagic = new Magicsquare();

			/* Compute Magicsquare square and print result */
			result = theMagic.makesqr(1, n);
			System.out.println(result[0]);
		} catch (Exception e) {
			System.out.println("Exception: " + e.toString());
		} finally {
			/* Free native resources */
			MWArray.disposeArray(n);
			MWArray.disposeArray(result);
			if (theMagic != null)
				theMagic.dispose();
		}
	}
}

 

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

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

不知该如何在java中使用armax()这样的函数,企求高手不吝指点!先谢谢了:)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics