Access class properties using Matlab Engine in C++

4 次查看(过去 30 天)
I am using the C MATLAB API and am programming classes to execute a program using the MATLAB Engine. I want to store and retrieve data of class object using engGetVariable and engPutVariable. For example when I have a class object called, 'clsObject = object()', then when I do something
like below,
MATLAB::engGetVariable(*engPtr, 'clsObject.a')
it fails to get the matrix inside that object to C. Would be nice if you could suggest any methods that worked for you on how to fetch the class properties directly using MATLAB C API.

回答(1 个)

Varun
Varun 2023-11-1
Hi Ranjit,
Looks like you are calling MATLAB from C and using the functions like "engGetVariable" and "engPutVariable" to store and retrieve data of class object as described below:
Engine* ep = engOpen(NULL);
mxArray* tmp = engGetVariable(ep, 'clsObject.a');
But you are still not able to access the property "a" of the object "clsObject". Please try with other approaches as described below:
const char* command = "propValue = demoObj.a;";
engEvalString(ep, command);
// Fetch the variable holding the property value
mxArray* propValue = engGetVariable(ep, "propValue");
const char* command = "propValue = get(demoObj, 'a');";
engEvalString(ep, command);
// Fetch the variable holding the property value
mxArray* propValue = engGetVariable(ep, "propValue");
To learn more about calling MATLAB from c, please refer to the following documentation:
In this documentation they have mentioned that this Engine API for C work with the MATLAB "mxArray" data structure, which is defined in the C Matrix API. To write applications using modern C++ features, see "Call MATLAB from C++".
Please refer to the documentation of "Call MATLAB from C++":
Hope it helps.
  1 个评论
Ranjit Roshan
Ranjit Roshan 2023-11-2
Hi Varun,
thanks a lot for the answer. I had tried this approach, the problem I had encountered with this approach was,
it slows down the speed of the application like crazy. For exampl, I have 30 variables to fetch, the application
speed jumps from 1X to 0.2x.
I also found that there is a getProperty function, but the documentation is not available properly on how to use it. So I swtiched to C++ API, which amazingly allows to fetch the object directly in one call and access all it's properties by name.
Once again thanks a lot for your response. I would suggest also people to use the C++ API rather than the C API. I will close the issue.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from C 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by