Stop plotting on MATLAB

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

0 votes

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

Hülya Sukas
Hülya Sukas le 30 Mar 2020
Thank you for your suggestion. I tried your suggestion but it did not work.
Adam Danz
Adam Danz le 30 Mar 2020
What didn't work? Did you get an error message? Did the plot not behavior predictably?
Hülya Sukas
Hülya Sukas le 30 Mar 2020
No there is no error message. But it did not make any difference.
Adam Danz
Adam Danz le 30 Mar 2020
Then it wasn't implemented correctly. The dropdown list should not have a callback function.
Please share the section of your code where this is implemented.
Sure, here it is : As you can see in the gif file, it plot first case again. But here it plot first choice with second selection speed:
hold on % only needs executed once
i = 1;
while i <= length(x)
addpoints(trajectory,y(i),x(i))
drawnow
hold on
uu = [ xout(i,6) xout(i,5) 0 0 xout(i,7)*180/pi 0 xout(i,1)];
drawShip_V3(uu);
hold off
% Get currently selected speed
currentSpeed = get(handles.threeDOF_animation_speed,'Value');
switch currentSpeed
case 1
plot_step = 5;
case 2
plot_step = 10;
case 3
plot_step = 20;
case 4
plot_step = 40;
case 5
plot_step = 80;
case 6
plot_step = 160;
case 7
plot_step = 320;
case 8
plot_step = 640;
otherwise
error('Undefined speed.')
end
i = i + plot_step;
end
end
Adam Danz
Adam Danz le 30 Mar 2020
It looks like the animation is evoked by the dropdown selection callback function which I wasn't aware of. If that's the case, you can add a variable animationON that keeps track of when the animation is already being executed. When animationON is true and the callback function is evoked again, it will do nothing (the animation will continue). If animationON is false or empty, the animation will start and will continue to the end.
function func(___) % The start of your callback function
persistent animationON % Declare animationON as persistent
if ~isempty(animationON) && animationON
% if animation is running, return and do nothing
return
else
animationON = true; % <---- turn animationON to ON
end
hold on % only needs executed once
i = 1;
while i <= length(x)
addpoints(trajectory,y(i),x(i))
drawnow
% hold on % <---- no need for this
uu = [ xout(i,6) xout(i,5) 0 0 xout(i,7)*180/pi 0 xout(i,1)];
drawShip_V3(uu); %<---- not sure what this is
% hold off % <---- no need for this, if you want, move it to the very end
% Get currently selected speed
currentSpeed = get(handles.threeDOF_animation_speed,'Value');
switch currentSpeed
case 1
plot_step = 5;
case 2
plot_step = 10;
case 3
plot_step = 20;
case 4
plot_step = 40;
case 5
plot_step = 80;
case 6
plot_step = 160;
case 7
plot_step = 320;
case 8
plot_step = 640;
otherwise
error('Undefined speed.')
end
i = i + plot_step;
end
animationON = false; % Turn animationON off at the end
end
Adam Danz
Adam Danz le 30 Mar 2020
These GIFs are helpful to show the symptoms, by the way. Kudos to you. Which method did you use? getframe() with imwrite()?
Thanks, I used a software to save these .gif files. But sometime I also save them with imwrite() command on matlab.
Sorry but I could not understand this part:
function func(___) % The start of your callback function
persistent animationON % Declare animationON as persistent
if ~isempty(animationON) && animationON
% if animation is running, return and do nothing
return
else
animationON = true; % <---- turn animationON to ON
end
Where should I define animationON ?
By the way I did not define any function to plot. So I dont have function func(___) part in my code.
Adam Danz
Adam Danz le 30 Mar 2020
Modifié(e) : Adam Danz le 30 Mar 2020
On the very first call of the function after opening the GUI, animationON will be empty because it's not defined, but it exists because it's a persistent variable.
The conditional statement contains an isempty() check and if it's empty, the 2nd part of the condition (the consequent) will define animationON as true. At the very end of the function, it's switched back to false and will continue to be either true (when the animation is running) or false (when the animation is not running) until the GUI is closed.
Note: if the function every breaks before animationON is set back to false, you'll need to either manually set it to false in debug mode or clear persistent variables. If you're frequently dealing with that problem, you could wrap a section of the code in a try, catch statement where any errors would switch the value back to false before exiting the function.
"By the way I did not define any function to plot. So I dont have function func(___) part in my code."
Yeah, I know, but I don't know what your callback function is named or how it's defined because that part wasn't included in the code you provided. I included that line so you can see that the persistent and the conditional statement happens at the beginning of the function.
Thank you for you detail explaination. I tried but now it does not plot anything.
By the way I tried something different as given below. It worked like that. But when I create an .exe file from this gui, .exe file is closed after animation is completed. I guess it closes because of the "pause" command. Do you have any suggestion for this? Can we solve this issue using "pause" command?
hold on % only needs executed once
i = 1;
while i <= length(x)
addpoints(trajectory,y(i),x(i))
drawnow
hold on
uu = [ xout(i,6) xout(i,5) 0 0 xout(i,7)*180/pi 0 xout(i,1)];
drawShip_V3(uu);
hold off
% Get currently selected speed
currentSpeed = get(handles.threeDOF_animation_speed,'Value');
switch currentSpeed
case 1
case 2
plot_step = 5;
case 3
plot_step = 10;
case 4
plot_step = 20;
case 5
plot_step = 40;
case 6
plot_step = 80;
case 7
plot_step = 160;
case 8
plot_step = 640;
otherwise
error('Undefined speed.')
end
i = i + plot_step;
end
pause
end
Adam Danz
Adam Danz le 30 Mar 2020
My guess is that you need to specify the axes in my version of the code,
hold(trajectory, 'on')
% ^^^^^^^^^^ I'm assuming this is your axis handle
The reason nothing happens (if my prognosis is correct) is because when you call hold on without specifying the axes, it 'holds' the wrong axes.
I'm assuming you're also including the animationON stuff, too.
Hülya Sukas
Hülya Sukas le 30 Mar 2020
Yes I included and now tried with hold(trajectory, 'on'), it did not plot anything.
I try also plot it in a separte figure, it did not plot it too.
Adam Danz
Adam Danz le 30 Mar 2020
Hmmm, OK. You can remove the hold off, though.
About the exe problem, I have less experience with this issue. What is the purpose of the pause command?
Hülya Sukas
Hülya Sukas le 30 Mar 2020
Sorry but hold off did not worked too.
Pause actually pause for awhile. For instance pause(100) pauses the plot for 100 second. But if I made a new selection for animation speed it works. It did not continue previous selection. Everything seems OK in MATLAB environment. When I create .exe file the program closes.
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 Graphics Performance dans Centre d'aide 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