Access pointer to struct in Matlab Coder

4 visualizzazioni (ultimi 30 giorni)
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;

Risposta accettata

Darshan Ramakant Bhat
Darshan Ramakant Bhat il 27 Nov 2020
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);

Più risposte (0)

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by