How to initialize static variable in C caller blocks during simulation

9 ビュー (過去 30 日間)
xianzhi gong
xianzhi gong 2022 年 3 月 22 日
コメント済み: Lucian 2024 年 1 月 22 日
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:
  1. First simulation: a = 10
  2. Second simulation: a = 20
  3. 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?

採用された回答

Angelo Yeo
Angelo Yeo 2022 年 12 月 5 日
編集済み: Angelo Yeo 2023 年 12 月 22 日
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 件のコメント
Aitor Juanicorena
Aitor Juanicorena 2023 年 5 月 9 日
Hi!
It worked for me also. Thanks a lot!
Lucian
Lucian 2024 年 1 月 22 日
Thanks a lot Angelo, worked for me too

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by