Question on MatlabEngine and Sharing Instances in Java

조회 수: 5 (최근 30일)
Shiloh Greer
Shiloh Greer 2019년 7월 10일
답변: halcyon 2024년 9월 4일
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.

답변 (1개)

halcyon
halcyon 2024년 9월 4일
String[] matinsts = MatlabEngine.findMatlab('a'); just find the engine which you have shared

카테고리

Help CenterFile Exchange에서 Call MATLAB from Java에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by