Stop plotting on MATLAB
조회 수: 36 (최근 30일)
이전 댓글 표시
Hello everyone,
I have a question about stopping a plot on matlab. I use given code to plot my trajectory. But the case here is that, I have a drop-down menu and I'm trying to control my plot speed using drop-down( pop-up) menu.
Pop-up menu works as given below:
function animation_speed_Callback(hObject, eventdata, handles)
% hObject handle to threeDOF_animation_speed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global plot_step
switch get(handles.animation_speed,'Value')
case 1
run('create_animation.m')
case 2
run('create_animation.m')
end
and here is the create_animation.m file :
if get(handles.animation_speed,'Value')==1
plot_step=5;
elseif get(handles.animation_speed,'Value')==2
plot_step=10;
end
for i = 1:plot_step:length(x)
addpoints(trajectory,y(i),x(i))
drawnow
hold on
end
The problem is when I select option '1' (plot_step=5) and then change it as '2' (plot_step=10) without waiting the finishing of the loop, it works like that: It starts to plot my points with case 1 and then it turns to case 2. ( Everything is normal untill now.) However, it continue to plot case 1. I do not want to see case 1 again. How can I fix this?
(PS: I attached a gif file I select 2x at the beginning and then I select 8x. After it finish to plot 8x it starts to continue 2x again.)
Thanks in advance!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/280420/image.gif)
댓글 수: 0
답변 (1개)
Adam Danz
2020년 3월 29일
The switch/case in your question does the same thing no matter what case was selected so why have it at all?
The reason you're seeing the animation twice is because the for-loop iterations are already defined when you're making the 2nd selection from your menu.
When you make the second selection, the code is executed again and when it finishes with the 2nd command, it goes back and finishes the first one.
If you want the dropdown selection to change the speed during the animation, you'll need to define the animation speed differently.
For example,
hold on % only needs executed once
i = 1;
while i <= length(x)
addpoints(trajectory,y(i),x(i))
% Get currently selected speed
currentSpeed = get(handles.animation_speed,'Value');
switch currentSpeed
case 5
plot_step = 5;
case 10
plot_step = 10;
otherwise
error('Undefined speed.')
end
i = i + plot_step;
end
*not tested
댓글 수: 16
Adam Danz
2020년 3월 30일
편집: Adam Danz
2020년 11월 3일
I'm asking about the purpose of the pause command. The pause command in your code is not controlling animation speed because it's outside of the while-loop.
The problems you're having with hold on/off are a mystery to me since there is no need to hold the plot of animated lines using addpoints unless you have preexisting content on the axes.
The exe problems you're having should be addressed in a new question.
If there's anything else not working related to your original question, I'd be happy to help out.
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!