Execute .m script from java

조회 수: 7 (최근 30일)
Mario Inglés Esteban
Mario Inglés Esteban 2022년 10월 11일
댓글: Ayush 2023년 5월 22일
Hi!! After researching about it... i don't find a stable solution.
I can execute some Matlab's statements from java codifying it with MalabEngine. But this solution is very slow at time to execute and also non-viable for me to refactor and test all my existing matlab code to this method.
I've tried to execute with MatlabEngine.eval("path location") a .m script but i have always the same response "Invalud use of operator". I'm trying since differents paths(even from project path) but not lucky.
I would like to know what options I can have in order to execute huge scripts with huge input data in matlab calling from java app. This i would like to be called from my java api (although was an remote connection, or something like that), and obtain the response to java in order to format and save it.
I couldn't find any clear and realiable solution for this, and i thinkg i'm missing something.
Many thanks in Advance!
  댓글 수: 1
Ayush
Ayush 2023년 5월 22일
The `MatlabEngine.eval()` method in MATLAB Engine API is used to execute commands or functions in MATLAB from Java. It is not used to execute a `.m` script directly.
To call the main script file that contains the function, you can use the `matEng.eval("run('path to .m file')");` function. Here's an example of how you can use this method to execute a MATLAB script:
import com.mathworks.engine.MatlabEngine;
public class MatlabScriptRunner {
public static void main(String[] args) throws Exception {
// Start the MATLAB engine
MatlabEngine matEng = MatlabEngine.startMatlab();
// Execute a MATLAB script
matEng.eval("run('path/to/your/matlab_script.m')");
// Disconnect from the MATLAB engine
matEng.disconnect();
}
}
If you still encounter the same response "Invalid use of operator", please make sure that the file location is correct and that the MATLAB script in the file is not using unsupported operators or functions that are not available in the version of MATLAB that you are using.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call MATLAB from Java에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by