Access class properties using Matlab Engine in C++

4 views (last 30 days)
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.

Answers (1)

Varun
Varun on 1 Nov 2023
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 Comment
Ranjit Roshan
Ranjit Roshan on 2 Nov 2023
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.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!