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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by