필터 지우기
필터 지우기

Equivalent of #define's for matlab coder?

조회 수: 32 (최근 30일)
Jon
Jon 2012년 10월 27일
답변: Muthukumar Ganesan 2023년 5월 3일
Hello- Is there any way to get matlab coder to not hard code numeric values (magic numbers) in generated code? in particular, I'm thinking about declarations of array sizes, and array indexing (flattening of arrays), etc.
We used matlab coder to generate some code from an algorithm written in matlab. Later, when integrating it with other non-matlab generated code, we may decide that we don't want things sized in exactly the same way -- say, we had a function that operated on a 100 element array, but decide we instead want 200.
We can redefine the other external interfaces, using #defines. we may need to size things differently - the rest of our code can do that based on #define's that we set in one place. But the autogen'd code has to be regenerated from scratch. Ideally, there would be a way for matlab NOT to use magic numbers in the code, but to instead replace every magic number with a #define, so that the C code can be resized without having to re-autogen!
Is there any way to do that in the existing matlab coder versions? The only solution I can think of is, when telling matlab the size of arrays, would be to use unique, nonsense values which are never used elsewhere, then search for those values. Even that probably only works if we never use operations involving "end" in indexing operations, like , A(end-10), since I'm not sure if ML will replace this with a single number
Thanks, -jon b

채택된 답변

Mike Hosea
Mike Hosea 2012년 10월 27일
편집: Mike Hosea 2012년 10월 27일
Unfortunately, no, there is no way to do this, and it is quite a bit more complex a suggestion than it may seem at first because of the way the compiler works through partial evaluation and specialization. We really would like to provide a way to have named constants for improved readability and are considering how this might be done.
We would probably not do this, however, for the purpose of making it easy to change the constants to avoid re-generating the code, and it might be difficult to guarantee that it would even be valid for that purpose, as the compiler would want to constant-fold logic like
if constant1 < 3
y = do_this(x);
else
y = do_that(x);
end
In the event that constant1 is, in fact, a constant, this would not result in a "magic number" in the generated code, and yet the generated code would depend on its value. Another bad thing about not re-generating when constant values are changed is that it breaks the connection between the MATLAB source and the generated code, making the tool just a means of generating a "first draft". One can easily imagine some important change to the generated code being lost if it is later determined that the code should be regenerated for any reason.
  댓글 수: 1
Jon
Jon 2023년 5월 3일
Okay, I can see how this gets tricky.

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

추가 답변 (2개)

Andreas Schröffer
Andreas Schröffer 2017년 9월 12일
I have an similar probelm. I want to parameterize constants at the beginning of my functions.
Initialization script is not supported for matlab code generation. So I call a Init function with the constants as return values. A problem is that even if i provide coder.inline('never') in my initialization function and take the option "generate one file for each matalb file" it get's inlined. and my init function doesn't appear anywhere. Only way is put logic like a if else in the initfcn for generating a separate file.
function y= fcn1()
[const1, const2] = initfcn;
...
y = ...
end
function [a,b] = initfcn
coder.inline('never');
a = 1; b= 1;
end
Any idea why?
  댓글 수: 1
Jon
Jon 2023년 5월 3일
In this case, I believe you need to use coder.ignoreConst, if you don't want a and b to actually be constants.

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


Muthukumar Ganesan
Muthukumar Ganesan 2023년 5월 3일
Hi,
It can be implemented by using custom storage class "ImportedExtern". Hence EC will generate the variable with extern storage class and in an external C file you can define the variable as you wish.
Thanks.

카테고리

Help CenterFile Exchange에서 Build Configuration에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by