Is there a way to create an array with MATLAB Coder and use a #define in the generated C-code for its size?

조회 수: 8 (최근 30일)
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

채택된 답변

Matan Silver
Matan Silver 2022년 11월 2일
Hi Thomas,
Unfortunately there's still no way to generate code for an array whose size is determined by a preprocessor define. As you have found, there could be some ways to "hack" MATLAB Coder into working with custom C-code called with coder.ceval. However, such a solution would be brittle. I've passed on the information you've written here to the development team, and we will consider it going forward.
Regarding using generated structs in custom C code, you may be able to use coder.cstructname to control the type name and fields of a generated struct. This could help lock down the struct definition if it would help for your use-case.
coder.ceval is the canonical way to call custom C code from generated code.
CustomSource, CustomLibrary, CustomInclude, and CustomSourceCode properties on the CodeConfig can also be useful for including custom code if you can factor out what you would like to re-compile on-demand into an external library.
However, as you noted, this may not fit your use-case.
Let me know if you have any more questions, and I'm sorry this limitation has impacted you,
Matan
  댓글 수: 1
Thomas Lichtenegger
Thomas Lichtenegger 2022년 11월 3일
Hi Matan,
Thank you for your answer, I have currently no further questions.
Although the answer is more or less bad news I can finally stop searching for a workaround and focus on a different solution.
-Thomas

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by