stop animations with pause()
31 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (2 件)
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.
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
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
参考
カテゴリ
Help Center および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!