Equivalent of #define's for matlab coder?
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jon
el 27 de Oct. de 2012
Respondida: Muthukumar Ganesan
el 3 de Mayo de 2023
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
0 comentarios
Respuesta aceptada
Mike Hosea
el 27 de Oct. de 2012
Editada: Mike Hosea
el 27 de Oct. de 2012
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.
Más respuestas (2)
Andreas Schröffer
el 12 de Sept. de 2017
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
el 3 de Mayo de 2023
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.
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!