Equivalent of #define's for matlab coder?
Show older comments
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
Accepted Answer
More Answers (2)
Andreas Schröffer
on 12 Sep 2017
0 votes
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?
Muthukumar Ganesan
on 3 May 2023
0 votes
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.
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!