Interrupting a for loop with a GUI Pushbutton

5 views (last 30 days)
Hi everyone,
I have a GUI set up and among other things, there is a 'start' pushbutton and a 'stop' pushbutton. Upon pushing the start, data is plotted on an adjacent axes and calculations are performed on the data. The plot is drawn using drawnow so that it plots in real time. What I am trying to do is while the data is plotting and calculations are being run, if the stop button is pressed, the plotting should stop and calculations should halt. This should be accomplished if I can just break the for loop, but everything I've tried hasn't succeeded. Here is what my code looks like:
What happens when the start button is pushed:
function startopen_Callback(hObject, eventdata, handles)
% hObject handle to startopen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
checkmachinef(handles,2);
Which calls checkmachinef and the code in question is
if command == 2; % Activates when 'Start' is pressed
for iz = first_turn:195
plot(handles.openaxes, Position(iz),Torque(iz),Position(first_turn:iz),Torque(first_turn:iz));
if Torque(iz)<0;
area(handles.openaxes, Position(first_turn:iz), Torque(first_turn:iz), 'FaceColor', [1 0 0]);
elseif Torque(iz)>0;
area(handles.openaxes, Position(first_turn:iz), Torque(first_turn:iz), 'FaceColor', [0 1 0]);
end
set(get(handles.openaxes,'XLabel'),'String','Position ({\circ})')
set(get(handles.openaxes,'YLabel'),'String','Torque (N-m)')
axis(handles.openaxes,[Position(first_turn), Position(1), minvalue, maxvalue])
drawnow;
pause(0.1);
stop_state = get(handles.stopopen, 'Value');
if stop_state
break
end
end
end
As you can see, I try to get the value of the stop pushbotton and if true then it breaks the loop, however nothing happens when I run the program, start the plot, then click stop. Any ideas? Thank you in advance
  3 Comments
Gabriel
Gabriel on 27 Sep 2017
Hi Casey Donnelly Thank you for your help it finally worked for me too.

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 28 May 2015
Edited: James Tursa on 28 May 2015

Categories

Find more on Startup and Shutdown 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!