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

29 ビュー (過去 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 Center および File ExchangeMaintain 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