Java Matlab engine Struct array fields empty

16 次查看(过去 30 天)
I have an existing Java application using the matlab engine. The java program uses feval to pass an array of com.mathworks.matlab.types.Struct objects, a string, and a double to the running Matlab instance. This was working perfectly in the 2019b version of the matlab engine. I am required to start using the 2021b version of the engine.
Once I implemented the 2021b version of engine.jar, I started getting the com.mathworks.engine.MatlabException specifically telling me that all the struct fields were unrecogized.
After a bit of debugging in the Matlab workspace, and printing to console, I see that the size/dimensions of my struct array is being recognized, "structs = 1xN struct array with no fields". So the array structure is there, but all my data dissappears moving from Java to Matlab.
Has anyone seen this issue?
JAVA:
Struct[] data = createStructs();
double dval = 0.0;
String sval = "Some String";
engine.feval(1, "function", dval, data, sval);

回答(1 个)

Dhruv
Dhruv 2023-4-21
The issue could be due to changes in the 2021b version of the Matlab engine, which may have affected the way that Struct objects are handled, try using the following approach:
  1. Check whether the Struct objects you're passing from Java to Matlab have been properly initialized and populated with data.
  2. Simplify your code and test with a basic Struct object to isolate the issue. For example, create a Struct object with just one field and pass that to Matlab to see if it is recognized properly.
  3. Try using a different method of passing data between Java and Matlab, such as using a cell array instead of a struct array. This can sometimes help to avoid compatibility issues.
Hopefully, one of these suggestions will help you resolve the issue. Please refer to the following documentation link for further guidance:
  1 个评论
Alex King
Alex King 2023-7-25
Thank you for your input. I have tried your suggestions, and the result is the same.
For a bit more context, the MatlabEngine object is created on the main thread. Other threads are calling in to the engine to run computations. Is there an issue calling the engine instance create on main thread, from another thread?
Thanks.

请先登录,再进行评论。

类别

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