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

10 views (last 30 days)
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

Accepted Answer

Rik
Rik on 6 Jul 2021
Edited: Rik on 8 Jul 2021
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 Comments
Karthikeyan Deivamani
Karthikeyan Deivamani on 14 Oct 2021
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 on 14 Oct 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!