Access class properties using Matlab Engine in C++
4 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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:
https://www.mathworks.com/help/matlab/calling-matlab-engine-from-c-programs-1.html?s_tid=CRUX_lftnav
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.
See Also
Categories
Find more on Call MATLAB from C in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!