stop animations with pause()

39 Ansichten (letzte 30 Tage)
feynman feynman
feynman feynman am 7 Feb. 2024
Kommentiert: feynman feynman am 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

Antworten (2)

Shubham
Shubham am 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 Kommentar
feynman feynman
feynman feynman am 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!

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 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 Kommentare
Walter Roberson
Walter Roberson am 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 am 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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by