USING PERSISTENT VARS IN CUSTOM SIMULINK MATLAB FUNCTION

조회 수: 14 (최근 30일)
Evgeny Shavelzon
Evgeny Shavelzon 2016년 10월 31일
댓글: Snus Mumrik 2019년 7월 2일
Hello, I have a FOR loop that makes a call for a custom simulink matlab function block I wrote every iteration. The function block has persistent variables that are supposed to remember their values from previous calls, however currently they don't. What do I do wrong?
Thats how the initialization looks (I don't use CLEAR in between the calls):
function[] = custom_func()
persistent gen tstat hyst comps xvalve yvalve;
if (isempty(gen))
[gen, tstat, hyst, comps, xvalve, yvalve]=data_locker();
end
  댓글 수: 2
Jan
Jan 2016년 11월 2일
Please do not use UPPERCASE, because this signals a SHOUTING and there is no reason to get upset here. Thanks.
Snus Mumrik
Snus Mumrik 2019년 7월 2일
Dear Sir,
There is really no need in wasting the time for both of us: for you to write and for me to read this. However, if you DO have a constructive comment on the question I had asked, I would be very thankful to read this.
Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Shivang Menon
Shivang Menon 2016년 11월 2일
I understand that you want the persistent variables to remember their values in a MATLAB function block. I tried using persistent variables in MATLAB function block, and placed the function block in a for iterator subsystem, and the persistent variables remember their values everytime the MATLAB function block is called by the Simulink Engine. Consider this code in a MATLAB function block:
function y = fcn()
persistent z d;
if(isempty(z))
z=1; d =0;
end
y = z + d;
z=z+1;
d=d+1;
Place the MATLAB function block inside a For iterator subsystem that executes 5 times on each time step. Connect the output of the MATLAB function block to a scope. You will see that output increases by 9 at each time step (use FixedStepDiscrete and set time step to 1 and stop time to 10). Please note that since the MATLAB function block is placed inside a For iterator subsystem, the MATLAB function block will execute 5 times (or N times) at each time step and the output of the 5th iteration will be taken as the output of the MATLAB function block for that time step. This is because the blocks inside a For subsystem are executed N times at each time step.
It is better to put a breakpoint in the code for the MATLAB function block and step through the code to understand more.
Hope this helps!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by