Stop plotting on MATLAB

126 vues (au cours des 30 derniers jours)
Hülya Sukas
Hülya Sukas le 29 Mar 2020
Modifié(e) : Adam Danz le 3 Nov 2020
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!

Réponses (1)

Adam Danz
Adam Danz le 29 Mar 2020
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 commentaires
Adam Danz
Adam Danz le 30 Mar 2020
Modifié(e) : Adam Danz le 3 Nov 2020
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.
Hülya Sukas
Hülya Sukas le 1 Avr 2020
I tried a few different ways using your suggestions but it did not worked. Anyway, thank you for your helps.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by