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!

回答(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 个评论
Hülya Sukas
2020-3-30
Adam Danz
2020-3-30
What didn't work? Did you get an error message? Did the plot not behavior predictably?
Hülya Sukas
2020-3-30
Adam Danz
2020-3-30
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.
Hülya Sukas
2020-3-30
Adam Danz
2020-3-30
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
2020-3-30
These GIFs are helpful to show the symptoms, by the way. Kudos to you. Which method did you use? getframe() with imwrite()?
Hülya Sukas
2020-3-30
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.
Hülya Sukas
2020-3-30
Adam Danz
2020-3-30
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
2020-3-30
Adam Danz
2020-3-30
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
2020-3-30
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
2020-4-1
类别
在 帮助中心 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
