How to properly define a complex externally defined input argument to entry point function in code generation?
Show older comments
I have a C project where some of the code is generated using Matlab Coder. One of the inputs to the entry point function for the generated code is a struct defined in a header file outside of the code generation. I have managed to tell Matlab Coder that it is an externally defined input, and can generate code.
My problem is that one of the fields in the struct is an array of another struct with undefined size:
typedef struct
{
...
uint16 sizeOfSample
AnotherDefinedStruct sample[]
} ExternallyDefinedStruct;
I have not found a way to make Matlab Coder understand this. Within the Matlab code, the struct is sent as an input argument to another .m-function, and since sample is an array of unknown size, it needs to be sent as a reference in the generated code:
functionCall(ExternallyDefinedStruct * measurement)
However, in the generated code it is passed by value, which makes for some weird results when the function then tries to read the values in the array.
I have tried to find a way to set the argument to Matlab Coder so that it understands that this is an array and cannot be passed by value, but so far I only end up with another type in the generated .._types.h file (which shouldn't be there at all since the struct is fully defined in my header file), and/or a function call that still passes by value.
My approach has been along these lines, with a lot of testing different ways to get it to be an array:
arg = struct;
...
arg.sizeOfSample = coder.typeof(uint16);
sample = struct;
arg.sample = coder.typeof(sample,[1,inf]);
arg.sample = coder.cstructname(arg.sample,'AnotherDefinedStruct','extern','Headerfile','path.h')
arg = coder.cstructname(arg,'ExterallyDefinedStruct','extern','Headerfile','path.h')
Is there a way to make Matlab Coder understand that this is not a fixed size array so that the struct gets passed by reference in internal function calls? Ideally I would like to simply provide the header file where the struct is defined to Matlab Coder and it figures it out from there, but I have not been able to find such an option.
(At first, I thought that I could use coder.ref() instead to indicate to Matlab Coder to pass argument by reference, but I found that this can only be used inside a coder.ceval() call, which this is not since it the call is to another .m-function.)
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!