Access pointer to struct in Matlab Coder

6 visualizaciones (últimos 30 días)
Philip Bergander
Philip Bergander el 27 de Nov. de 2020
Comentada: Philip Bergander el 15 de En. de 2021
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;

Respuesta aceptada

Darshan Ramakant Bhat
Darshan Ramakant Bhat el 27 de Nov. de 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);

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by