Execute only once

17 次查看(过去 30 天)
Meral
Meral 2011-8-23
评论: Coolman 2021-3-3
Hi
I wrote a program in Embedded Matlab Function in Simulink. I have to define some variables as 0(zero) at first but they will be 0 only for the beginning. Then these variables are calculated in the program. Is there a way to execute the definatiion commands only once? Thanks in advice...

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2011-8-23
I guess you want to declare your variable persistent. type doc persistent for example. To initialize, you can do
if ~exist('VarName','var')
VarName=0;
end
  3 个评论
Fangjun Jiang
Fangjun Jiang 2011-8-23
Thank you Kaustubha for pointing it out. Indeed it needs to use isempty(), not to use exist(..,'var') because once the 'persistent VarName' line executes, the variable VarName will exist.
Coolman
Coolman 2021-3-3
Could you do this for a figure also so I can make sure someone does not make tons of pop ups by clicking the same button multiple times?

请先登录,再进行评论。


Clemens
Clemens 2012-1-19
@Kaustubha:
Thanks a lot for the solution I just came across. By adding these 3 extra lines of code my Embedded Function executes about 100 times faster in simulation ;)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by