Execute only once
조회 수: 13 (최근 30일)
이전 댓글 표시
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...
댓글 수: 0
답변 (2개)
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
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
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
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 ;)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!