Execute .m script from java

7 次查看(过去 30 天)
Mario Inglés Esteban
评论: 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