Question on MatlabEngine and Sharing Instances in Java
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
halcyon
2024-9-4,3:39
String[] matinsts = MatlabEngine.findMatlab('a'); just find the engine which you have shared
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Java 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!