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 Center および File ExchangeCall 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