stop animations with pause()
조회 수: 26 (최근 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!