Question on MatlabEngine and Sharing Instances in Java
Show older comments
I am working in Java and trying to execute some MATLAB code. So far this has involved using the MatlabEngine class and using functions like eval(), which has worked great. However, I want other Java classes to have the ability to access any MATLAB variables created by the first class. I'm getting errors, however, and would appreciate any advice.
Here's the code that runs for the first class:
String[] matlines = {"cd '...'",
"sim(\"modelName\");",
"set_param('modelName','StartTime','0','StopTime','0');"};
MatlabEngine matinst = null;
matinst = MatlabEngine.startMatlab();
matinst.engine.shareEngine('a');
for (int i = 0; i < matlines.length; i++)
{
matinst.eval(matlines[i],null,null);
System.out.println( "Executed code from line " + i + " from matlabLineArr." );
}
Here's what I'm trying to run from an external class that operates after the above code has run:
String[] matinsts = MatlabEngine.findMatlab();
MatlabEngine matinst = MatlabEngine.connectMatlab(matinsts[0]);
double[] javaArr = matinst.getVariable("MatVect");
I have a sneaking suspsion that I'm trying to do two contradictory things, somehow, but I don't know what the issue is. Again, any advice would be appreciated.
Answers (1)
halcyon
on 4 Sep 2024
0 votes
String[] matinsts = MatlabEngine.findMatlab('a'); just find the engine which you have shared
Categories
Find more on Call MATLAB from Java in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!