Unit Delay Initialization C Code Generation

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
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
Jack el 18 de Mzo. de 2024
Movida: Fangjun Jiang el 28 de Mzo. de 2024
Ah! Is "int myNum" possible? If so is it equivalent to int myNum = 0:.
I am not too familar with C code.
Fangjun Jiang
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
Fangjun Jiang el 18 de Mzo. de 2024
Movida: Fangjun Jiang el 28 de Mzo. de 2024
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
Jack el 18 de Mzo. de 2024
Movida: Fangjun Jiang el 28 de Mzo. de 2024
So i have tried generating the code for both cases, when is it 0 and when it is not. I have found the function where the non zero case is intialised but could not find the intialisation in the case when it is 0, hence the reason for my question. I thought it must be happening somewhere but I can't find where. Then I thought that is could be implicitly doing it when you define the datatype but as per you answer, not necessarily. Still not sure where intialising a variable with 0 happens when I generate code.
Fangjun Jiang
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
Jack el 18 de Mzo. de 2024
Movida: Fangjun Jiang el 28 de Mzo. de 2024
So I think it is doing it in the function argument. void Example(int8 X). Is this correct. sorry my C knowledge is limited. This is only the case for variables that exist in that function. Global Variables are defined in a master file.
Fangjun Jiang
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
Jack el 18 de Mzo. de 2024
Movida: Fangjun Jiang el 28 de Mzo. de 2024
Okay my mistake. I have tried the tracebility tool but it doesn't seem to help.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 14 de Mzo. de 2024

Movida:

el 28 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by