MATLAB Answers

Pausing and continuing an animation in App Designer

7 views (last 30 days)
R.S.
R.S. on 7 Nov 2020
Edited: Mario Malic on 8 Nov 2020
Hey everyone I'm trying to pause an animation I made.
Following is the loop that generates the animation.
Before, the position over time of the falling objects are calculated and saved as a vector.
Objects are generated by the patch command.
With every iteration of the loop the new position is shown in a figure.
tu is the time the objects fall until they hit the ground.
tf is a vector with the time intervals between 0 and tu.
tf consists of as many columns as needed to guarantee 25 frames per seconds.
So far everything works as planed.
%Schleife zur Animation
q = 1;
time = 0;
tic;
while time < tu
time = toc;
%Anzeigewerte
app.GeschwindigkeitEditField.Value = vt(q);
app.aktuelleZeitEditField.Value = tf(q);
app.StreckeEditField.Value = st(q);
app.kinetischeEnergieEditField.Value = Ekin(q);
app.potentielleEnergieEditField.Value = Epot(q);
app.GesamtenergieEditField.Value = Eges(q);
app.rotatorischeEnergieEditField.Value = 0; % visualisation of Information about velocity, energy, time etc.
%Position der Scheibe
Kreis.XData = xAkreis + R;
Kreis.YData = yAkreis + y(q); % positon of first falling object
InnenKreis.XData = xIkreis + R;
InnenKreis.YData = yIkreis + y(q); % positon of second falling object
%Grenzen des Diagramms
xlim([-R 20+R]);
ylim([-H-Boden*3/2 R+2]);
pau = tf(q) - toc;
pause(pau); % forcing matlab to wait until predefined time (tf)
q = q + 1;
if q > length(tf)
q = length(tf);
end
end
I now want to pause my animation at any point and continue it at the same point.
I tried a second infinite while loop inside the first one that is started by using a switch in app designer but if it started it started with different delays and didn't continue after I used the switch again.
Thanks for any help!

  3 Comments

Mario Malic
Mario Malic on 8 Nov 2020
I wouldn't know if it would be possible to do this in 25fps due to some technical difficulties with uifigure. This would probably do what you'd like to do. If you can do the calculations outside of this code, maybe it would be better
%Schleife zur Animation
q = 1;
time = 0;
tic;
while time < tu
% code
if app.ButtonState.Value % 1
while ~app.ButtonState.Value % 0
% do nothing, wait for off state
end
end
end
If these are constant, place them outside of loop.
Edit: If you are using figure for animation then nevermind about the first sentence.
xlim([-R 20+R]);
ylim([-H-Boden*3/2 R+2]);
Mario Malic
Mario Malic on 8 Nov 2020
Great! I'll post this as an answer and you can accept it. Thanks in advance.

Sign in to comment.

Answers (1)

Mario Malic
Mario Malic on 8 Nov 2020
%Schleife zur Animation
q = 1;
time = 0;
tic;
while time < tu
% code
if app.ButtonState.Value % 1
while ~app.ButtonState.Value % 0
% do nothing, wait for off state
end
end
end

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!