Access class properties using Matlab Engine in C++

조회 수: 4 (최근 30일)
Ranjit Roshan
Ranjit Roshan 2023년 10월 25일
댓글: Ranjit Roshan 2023년 11월 2일
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