필터 지우기
필터 지우기

Why the loop does not stopped?

조회 수: 2 (최근 30일)
Hasung Hwang
Hasung Hwang 2019년 4월 16일
댓글: Ben Cunningham 2019년 4월 17일
Hello,
Using designer, I make same program as below.
When click 'Run Start', current time and loop running count will be described at 'Run Count' and 'Run Time'.
When click 'Run Stop', running loop will be stopped.
So I programmed as below.
------------------------------------
% Button pushed function: btnStart
function btnStartPushed(app, event)
app.btnStart.Enable = 'off';
app.btnStop.Enable = 'on';
runCount = 0;
runTime = datestr(now, 'HH:MM:SS');
global runStatus
runStatus = 0;
gcf
set(gcf, 'WindowButtonUpFcn', @btnStopButtonPushed)
while ~runStatus
drawnow
app.efRunCount.Value = num2str(runCount);
app.efRunTime.Value = runTime;
runCount = runCount + 1;
runTime = datestr(now, 'HH:MM:SS');
end
app.btnStart.Enable = 'on';
app.btnStop.Enable = 'off';
end
% Button pushed function: btnStop
function btnStopButtonPushed(app, event)
global runStatus
runStatus = 1;
end
---------------------------------------
I got this kind of method from this site.
Why I use 'drawnow' order in this program?
When I remove 'drawnow' because I have own windows, the loop is not stopped even through 'Run Stop' is clicked.
How can I handle this problem?

채택된 답변

Ben Cunningham
Ben Cunningham 2019년 4월 16일
See the 'interruptible' property of callbacks in app designer - e.g. here under the heading 'Callback execution control'.
The answer to your question is :
"The interruption occurs at the next point where MATLAB processes the queue, such as when there is a drawnow, figure, uifigure, getframe, waitfor, or pause command."
In other words the drawnow command is providing an oppurtunity for your stop button to interrupt the execution of the while loop to set the global variable, then when the while loop resumes ~runStatus will evaluate false.
Without drawnow, the stop button will wait until the previous callback is finished - which never occurs since your while loop will spin forever.
  댓글 수: 3
Hasung Hwang
Hasung Hwang 2019년 4월 17일
I solve this problem as below.
Previous---------------------------
gcf
set(gcf, 'WindowButtonUpFcn', @btnStopButtonPushed)
while ~runStatus
drawnow
app.efRunCount.Value = num2str(runCount);
app.efRunTime.Value = runTime;
runCount = runCount + 1;
runTime = datestr(now, 'HH:MM:SS');
end
Current------------------------------
set(get(groot, 'CurrentFigure'), ...
'WindowButtonUpFcn', @btnStopButtonPushed)
while ~runStatus
drawnow
app.efRunCount.Value = num2str(runCount);
app.efRunTime.Value = runTime;
runCount = runCount + 1;
runTime = datestr(now, 'HH:MM:SS');
end
----------------------------------------------------------
After then, there is no additional windown and while loop also closed by stop button click.
Thank you for your request.
Best regards,
Ben Cunningham
Ben Cunningham 2019년 4월 17일
Well figured out! No worries.
Cheers,
Ben

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by