Java调用Matlab函数笔记

欢迎使用Markdown编辑器写博客

笔记是记录通过java如何调用matlab函数:
1.在命令行中输入deploytool
Java调用Matlab函数笔记_第1张图片
2.点击上图中标有红框的部分
3.选择javapackage,依次填写name,classname
Java调用Matlab函数笔记_第2张图片
在package的过程中可能会出现mcc错误的情况,参考如下解决方法:mcc错误
4.编写java类,进行测试

import com.mathworks.toolbox.javabuilder.MWException;

import computef.ComputeF;

public class matlab2javaTest {
     
    public static void main(String args[]) throws MWException {
        System.out.println( System.getProperty("java.library.path"));
        ComputeF cl = new ComputeF();
        cl.computef(0);
    }
}

第一次运行时,会报如下错误
Exception in thread “main” java.lang.UnsatisfiedLinkError: Failed to find the library mclmcrrt7_14.dll, required by MATLAB Builder JA, on java.library.path.
This library is typically installed along with MATLAB or the MCR, its absence may indicate an issue with that installation or the current path configuration.
The MCR version that this component is trying to use is: 7.14.
错误原因可能是:path中没有matlab的路径,即找不到mclmcrrt7_14.dll这个文件,添加matlab路径到path中就可以了
5.调用有返回结果的函数
m文件内容:
function [f,c]=computef(key)
调用computerf函数时

Object[] result = null;
result = cl.computef(2,0);//第一个参数是返回结果的个数,第二个参数是实际参数

你可能感兴趣的:(工作经验)