How to initialize static variable in C caller blocks during simulation
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
xianzhi gong
am 22 Mär. 2022
Kommentiert: Lucian
am 22 Jan. 2024
float func_1(void)
{
static float a = 0;
a = a +1;
return a;
}
Simulation setting Fixed step size = 0.1s. Run simulation for 1 second:
- First simulation: a = 10
- Second simulation: a = 20
- Third simulation: a = 30
It seems that re-run simulation after ending one simulation, the "a" wil stay same as the value of the last simulation.
I understnad for global variables we can write a initilization function to initialize it. How about the static variable within the function?
0 Kommentare
Akzeptierte Antwort
Angelo Yeo
am 5 Dez. 2022
Bearbeitet: Angelo Yeo
am 22 Dez. 2023
The "C Caller" block creates temporal C and header files, compiles these and creates .dll files under the slprj folder. These .dll files hold values of static variables between simulations.
Can you go to the Model Explorer and open "Callbacks" for the model? And, in "InitFcn", please type in
clear mex
This will solve the issue with initializing static variables in C Caller blocks.
Please check attached file for a specific example.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulink Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!