Access pointer to struct in Matlab Coder

3 ビュー (過去 30 日間)
Philip Bergander
Philip Bergander 2020 年 11 月 27 日
コメント済み: Philip Bergander 2021 年 1 月 15 日
Hello,
I have a c header file with the following struct and inline function to access the pointer to the struct:
typedef struct {
float field1;
unsigned field2;
} myStruct;
extern myStruct data;
inline myStruct* access_structData (void) {
return &data;
}
I tried to access the pointer to the struct with coder.opaque:
structPtr = coder.opaque('myStruct *','NULL','HeaderFile','struct_header.h')
structPtr = coder.ceval('access_structData')
a = structPtr.field1;
I cannot generate code for this since "??? Attempt to extract field 'field1' from 'coder.opaque'". How can I read and write data to a struct using a pointer? Is there any proper work-arounds?
In c the code for what I try to do would look something like this:
float a = access_structData->field1;

採用された回答

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2020 年 11 月 27 日
The struct type is opaque for the MATLAB Coder. So it will not have the information about he fields. Can you please try the below work arounds :
  • If you wan to access each fields then write a wrapper to access them like below
float access_struct_field1(void) {
data->field1;
}
unsigned access_struct_field2(void) {
data->field2;
}
then invoke these calls through coder.ceal as necessary.
  • Do the struct operation in another wrapper function
Consider you want to do addition of the struct fields, then you can do these operations in another C wrapper function
float myStructOperations(myStruct* aStruct ) {
reutrn (float)(aStruct->field1+aStruct->field2);
}
In MATLAB you can do like below
structPtr = coder.opaque('myStruct *','NULL','HeaderFile','struct_header.h');
structPtr = coder.ceval('access_structData');
output = coder.ceval('myStructOperations',structPtr);
  1 件のコメント
Philip Bergander
Philip Bergander 2021 年 1 月 15 日
Thank you, this seems to work.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by