- Either the struct is not modified inside the called function, or
- The struct is always passed to the function in the form of "x = foo(x)", and the function signature also is "function p = foo(p)", that is, the struct is both an input and an output.
How to properly define a complex externally defined input argument to entry point function in code generation?
3 visualizaciones (últimos 30 días)
AB el 30 de Sept. de 2022
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:
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.)
Denis Gurchenkov el 30 de Sept. de 2022
I don't see any way to accomplish this directly. MATLAB Coder just can't comprehend a notion of an open-ended struct, nor it has a notion of "this has to be passed by reference". I'll pass this to the development team as a request - can they come up with a way to support this feature.
One thing - generated code should be passing this struct by reference in two cases:
So I wonder if you could, as a work around, define this struct as if it has a fixed size field, and then use the #2 above to avoid copy by value? I know this is a hack but it could work?