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.
You can read more about it here:Call MATLAB from Java - MATLAB & Simulink (mathworks.com)
