Clear Filters
Clear Filters

stop animations with pause()

20 views (last 30 days)
feynman feynman
feynman feynman on 7 Feb 2024
Commented: feynman feynman on 8 Feb 2024
I wanna stop/break/suspend the following animation without using ctrl+c, how?
for i=1:10
plot(linspace(0,1),linspace(0,1))
hold on;pause(0.1);
end

Answers (2)

Shubham
Shubham on 7 Feb 2024
Hi Feynman,
To stop, break, or suspend an animation loop in MATLAB without using Ctrl+C, you can introduce a condition that checks for a specific event, such as a button press or a value in a figure's UserData being set. To actually stop the animation, you would need to set the UserData property of the figure to false. This could be done, for example, in a callback function from a UI control like a button. Here is an example of how you could add a button to the figure to stop the animation: Here's a simple example using a figure's UserData property to control the loop execution:
function StopAnimation
% Create a figure
hFig = figure;
% Add a button to the figure with a callback that stops the loop and closes the window
uicontrol('Style', 'pushbutton', 'String', 'Stop', ...
'Position', [20 20 60 20], ...
'Callback', @(src, event) stopAndClose(hFig));
% Set the UserData property of the figure to true
set(hFig, 'UserData', true);
for i = 1:10
% Check if the figure has been closed manually
if ~ishandle(hFig)
break;
end
% Plot the line
plot(linspace(0, 1), linspace(0, 1));
hold on;
% Pause for a short time to see the animation
pause(0.1);
% Check the UserData property of the figure to see if the Stop button was pressed
if ~get(hFig, 'UserData')
break; % Break the loop if UserData is set to false
end
end
% Define the stopAndClose function
function stopAndClose(figHandle)
% Stop the loop by setting UserData to false
set(figHandle, 'UserData', false);
% Close the figure window
close(figHandle);
end
end
In this modified script, clicking the 'Stop' button will set the UserData property of the figure to false, which the loop checks after each iteration. If UserData is false, the loop will break, effectively stopping the animation.
  1 Comment
feynman feynman
feynman feynman on 7 Feb 2024
many thanks really helpful! It'll be good to also include functions to suspend (stop without breaking) or continue a loop, then one can really control the progress. Finally, is it also possible to replay the whole animation after it finished running? If there's tutorial pages on them I'll also appreciate a lot!

Sign in to comment.


Walter Roberson
Walter Roberson on 7 Feb 2024
Create a uicontrol radio button to act as a stop button.
TheUIControl.Value = 0;
for i=1:10
if TheUIControl.Value == 1
break;
end
plot(linspace(0,1),linspace(0,1))
hold on;pause(0.1);
end
  5 Comments
Walter Roberson
Walter Roberson on 7 Feb 2024
TheUIControl=uicontrol('Style', 'togglebutton', 'String', 'Stop', ...
'Position', [20 20 60 20]);
TheUIControl.Value = 0;
for i=1:10
if TheUIControl.Value == 1
break;
end
plot(linspace(0,1),linspace(0,1))
hold on;pause(0.1);
end
feynman feynman
feynman feynman on 8 Feb 2024
many thanks really helpful! Finally, is it also possible to replay the whole animation after it finished running? If there's tutorial pages on them I'll also appreciate a lot!

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!