Pause execution of While loop and resume from the same point with PushButtons App Designer

조회 수: 31 (최근 30일)
Hi! I made an animation in AppDesigner with a while loop, I pause and resume it with push buttons using uiwait and uiresume in their callbacks, apparently it works because when I hit "pause" the image freezes but when I push the "resume" button it seems the while loop kept running because by that time the animation has already advanced like it jumped in time from an image to another.
I saw in another post
How can I do to pause the while loop and then resume from the same point I paused it??
I would really appreciate any suggestion
function StartButtonPushed(app, event)
close all
set(app.StartButton,'Text','Restart')
time = 120;
set(app.UIAxes, 'units','points')
cla(app.UIAxes);
R = 65;
ang=linspace(0,2*pi,50);
xp=R*cos(ang);
yp=R*sin(ang);
% Animation
% Draw outter circle
patch(app.UIAxes,xp,yp,'w')
hold(app.UIAxes,'on');
axis(app.UIAxes, 'equal');
% line
rho1 = 55;
line = plot(app.UIAxes,NaN, NaN,'-*', 'LineWidth', 5, 'MarkerSize', 0.5, 'color', 'k');
tic
while toc < time
t1=toc;
f = 0.2*360;
line.XData = [0, rho1*cosd(-f*t1)] ;
line.YData = [0, rho1*sind(-f*t1)] ;
drawnow
end
end
% Button pushed function: PauseButton
function PauseButtonPushed(app, event)
uiwait(app.animation)
end
% Button pushed function: ResumeButton
function ResumeButtonPushed(app, event)
uiresume(app.animation)
end
end

채택된 답변

Rik
Rik 2021년 7월 6일
편집: Rik 2021년 7월 8일
The reason for your issue is that you're using toc, which will still continue running.
If you measure how long your GUI has been paused, you can store that in a property. If you subtract that from t1, you can effectively pause the time.
See the code below for my suggestion. You should also consider always using an explicit handle for toc. Otherwise you will measure the time since the last tic, not the matching call.
t_h=tic;app.PauseTime=0;
while true
t1=toc(t_h)-app.PauseTime;
if t1>time,break,end
f = 0.2*360;
line.XData = [0, rho1*cosd(-f*t1)] ;
line.YData = [0, rho1*sind(-f*t1)] ;
drawnow
end
end
% Button pushed function: PauseButton
function PauseButtonPushed(app, event)
app.PauseHandle=tic;
uiwait(app.animation)
end
% Button pushed function: ResumeButton
function ResumeButtonPushed(app, event)
app.PauseTime=app.PauseTime+toc(app.PauseHandle)
uiresume(app.animation)
end
  댓글 수: 7
Karthikeyan Deivamani
Karthikeyan Deivamani 2021년 10월 14일
Hi, Is there any possibilty of pausing and resuming from the last point with out using App designed but with MATLAB code. I have been trying using waitforbuttompress and pause but I hit a roadblock everytime. I could make my visual simulation pause but make it resume from the last point.
Rik
Rik 2021년 10월 14일
You need an explicit position to pause your execution. What you can do is creating a while loop with a pause of a few seconds. That way you can check the flag every few seconds and resume from that point, without using too many resources.
This is the easy way. The hard way is to write your code in such a way that your code can resume your simulation. That will require you to write your code in such a way that it can simply calculate the next iteration based on the last one.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Maintain or Transition figure-Based Apps에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by