Execute .m script from java

7 views (last 30 days)
Mario Inglés Esteban
Mario Inglés Esteban on 11 Oct 2022
Commented: Ayush on 22 May 2023
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 Comment
Ayush
Ayush on 22 May 2023
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.

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!