Is there a way to create an array with MATLAB Coder and use a #define in the generated C-code for its size?
Show older comments
Good morning,
Is there a way to create arrays in MATLAB Coder which use preprocessor #defines in the generated C-code for their sizes?
NOTE: My question is closely related to a previously asked question by Jon, see the link. Back in 2012 he did get an answer, but considering it has been 10 years I wanted to bring up the question again, hoping there has been changes in how the MATLAB Coder generates code.
I want to run my code in MATLAB and to some testing in a sort of MIL environment, but when I generate my code into C I still need the possibility to make changes in preprocessor defines to alter certain array sizes in static memory befor I compile my code. I am also restricted to static memory and cannot use dynamic memory allocation in C.
In Matlab I want to write something like:
global DEFINE_NAME
if coder.target('MATLAB')
DEFINE_NAME = 10;
elseif coder.target('RTW')
coder.storageClass('DEFINE_NAME','ExportedDefine');
end
af32MyArray = zeros(DEFINE_NAME,1,'single');
After code generation I want my C-code to look something like this (appearently code highlighting does not work for C-Code or I am not using it correctly):
/*define might be in another header*/
#define DEFINE_NAME 10
float32 af32MyArray[DEFINE_NAME];
I already managed to create certain variables as defines by using coder.storageClass, which is nice. However this only works for variables with fixed size, for example uint8 conditions for if-else or switch-case blocks or for threshold values. In the case above I will receive an error (see below) suggesting I should consider to turn dynamic memory allocation on which I cannot.
??? Non-constant expression or empty matrix. This expression must be constant because its value determines the size
or class of some expression.
or
??? Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [1 x :?].
I also found some 'hacks' to trick MATLAB Coder into placing C-Code in my generated Code, however this seems to be quite unreliable and is definitly NOT my way to solve this:
if coder.target('RTW')
coder.ceval('#ifdef WE_HACK //placedCode ');
coder.ceval('int myInt[DEFINE_NAME]; //placedCode ');
coder.ceval('#endif //placedCode ');
end
results in something like:
#ifdef WE_HACK //placedCode ();
int myInt[DEFINE_NAME]; //placedCode ();
#endif //placedCode ();
Although this gets compiled perfectly fine, everything inside the ceval is hard coded, therefore it cannot use structs or types that are created during the code generation process. If anyone knows an offical way to place code snippets I would be interested in learning this however.
Thanks anyone who can support me here!
-Thomas
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!