How to get a push button to end a loop in a GUI?

15 views (last 30 days)
I have created a GUI using GUIDE. The purpose is to plot an animation of a very simple projectile motion equation. I have edit text fields to enter the initial angle and velocity, drop downs to select the units, and two push buttons, one to plot and one to stop. Everything works well except for the stop button. I cannot seem to get it to interrupt the loop. I unchecked the interruptible box in the editor for the plot button. I don't have a very good grasp on the callback functions or hObjects or handles, and I would like a simple resolution. Can someone give me an idea of what I am doing wrong in the code below?
if stop==1
This is homework, but adding stop functionality is not part of the assignment, this is just something I would like to do. We are only required to create a simple GUI; the animation is not even necessary.

Accepted Answer

Ingrid on 6 May 2015
Edited: Ingrid on 6 May 2015
you need to set a flag when you press the pusbutton to stop so in your pushbutton4 callback you should put this instead of the code you have there now:
% Update handles structure
guidata(hObject, handles);
then in the for loop you should put this
if isappdata(handles.figure1,'stopPlot')
to be able to rerun your animation you should also put this after your for-loop
You also need to set the Interruptibility of pushbutton1 to on (change this in guide)
it also seems that you are missing a call to
right before the end of your for-loop because otherwise you do not see your animation move
and finally may I ask why you do not use a semi-colon to surpriss output being shown in the command window?
zeytun on 1 Dec 2016
Edited: zeytun on 1 Dec 2016
Hi Ingrid. Thanks for your answer, it helped me too to stop the "for" loop in my GUI (which is a measurement from a real device and plotting the result). However, I have one question. During the loop, when I am applying the "Stop" button, everything is fine: it finishes the current measurement and plots the result. Now, if I don't apply the "Stop" button and wait until the loop is done on its own, it still plots the final result but gives the following error:
Error using rmappdata
Invalid user property: stopPlot
The part in the m-file responsible for stopping the loop is as follows:
for K = 1:handles.kk
axes(handles.axes11); % plot current result inside the loop
cla reset;
plot(K1(1:K),T1(1:K),'o -')
if isappdata(handles.axes11,'stopPlot')
figure(1); % plot final result outside the loop
plot(K1,T1,'o -')
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Update handles structure
guidata(hObject, handles);
Can you please help me to resolve this issue?
Thanks, Aram

Sign in to comment.

More Answers (0)


Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!