1. 程式人生 > >Java呼叫Matlab函式筆記

Java呼叫Matlab函式筆記

歡迎使用Markdown編輯器寫部落格

筆記是記錄通過java如何呼叫matlab函式:
1.在命令列中輸入deploytool
這裡寫圖片描述
2.點選上圖中標有紅框的部分
3.選擇javapackage,依次填寫name,classname
這裡寫圖片描述
在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);//第一個引數是返回結果的個數,第二個引數是實際引數