Simulink C-caller with persistent fcn

1 回表示 (過去 30 日間)
Jack Daniels
Jack Daniels 2024 年 10 月 30 日
回答済み: David Balbuena 2024 年 11 月 6 日
I'd like to have a short fcn in Simulink called by C-caller
if (state>0)
{
state = -1;
y = 0.1;
} else if (state < 0)
{
state = 1;
y = -0.1;
} else
y=0;
I need to have "state" variable as peristent, can I have a persistent variable within C-caller?
Thank you.

回答 (2 件)

Shantanu Dixit
Shantanu Dixit 2024 年 11 月 1 日
Hi Jack,
You can make the 'state' variable persistent within a C-caller block in Simulink by declaring it as 'static' in your C function. This ensures that the 'state' retains its value between calls to the function, effectively making it persistent across simulation time steps.
Hope this helps!

David Balbuena
David Balbuena 2024 年 11 月 6 日
You could use the C Function block instead. The C Function block lets you put that code directly in the block mask, and it also lets you define "state" to be a persistent symbol. This example shows how to do that: https://www.mathworks.com/help/simulink/ug/modify-states-of-a-c-function-block-using-persistent-scope.html

カテゴリ

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

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by