Unit Delay Initialization C Code Generation
Mostrar comentarios más antiguos
I have a model in which I am generating C code for. This model has a Unit Delay block in with an intial condition set to 0. When I generate the C code, where the intialization step would usually occur for a non zero Initial condition, No code is generated but I assume somewhere it must be intialising this variable. Where is this done instead and why does it change.
Thanks
Respuestas (1)
Fangjun Jiang
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
0 votos
You could experiment. Set the initial value to be zero and non-zero to see where the code is located. Note that in C code, the initial value for a variable is zero by default. Or it can be initialized when the variable is declared, such as
int myNum = 15;
8 comentarios
Jack
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Fangjun Jiang
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
"int myNum;" is not guranteed to be "int myNum = 0;" so it's better to use latter. For you, you want to know where the initial value is set for the Unit Delay output in the generated code, you can do experiment, just check the code in the zero and non-zero cases.
Fangjun Jiang
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Found this at the link. https://stackoverflow.com/questions/7975008/initializing-variables-in-c
A rule that hasn't been mentioned yet is this: when the variable is declared inside a function it is not initialised, and when it is declared in static or global scope it's set to 0:
int a; // is set to 0
void foo() {
int b; // set to whatever happens to be in memory there
}
However - for readability I would usually initialise everything at declaration time.
Jack
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Fangjun Jiang
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Try to search the code when the initial value is zero. It must be initialized when it is declared.
Jack
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Fangjun Jiang
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
no. There should be a generated C code variable that corresponds to the output of the Unit Delay block. Search the declaration of that variable in the C code. You can turn on traceability to help find it.
Jack
el 18 de Mzo. de 2024
Movida: Fangjun Jiang
el 28 de Mzo. de 2024
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!