フィルターのクリア

stop animations with pause()

20 ビュー (過去 30 日間)
feynman feynman
feynman feynman 2024 年 2 月 7 日
コメント済み: feynman feynman 2024 年 2 月 8 日
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

回答 (2 件)

Shubham
Shubham 2024 年 2 月 7 日
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 件のコメント
feynman feynman
feynman feynman 2024 年 2 月 7 日
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!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2024 年 2 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2024 年 2 月 7 日
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 2024 年 2 月 8 日
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!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by