Interrupting a for loop with a GUI Pushbutton

8 visualizaciones (últimos 30 días)
Casey Donnelly
Casey Donnelly el 28 de Mayo de 2015
Comentada: MINKYUNG KIM el 14 de Oct. de 2019
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 comentarios
Gabriel
Gabriel el 27 de Sept. de 2017
Hi Casey Donnelly Thank you for your help it finally worked for me too.
MINKYUNG KIM
MINKYUNG KIM el 14 de Oct. de 2019
THANK YOU A LOT!!

Iniciar sesión para comentar.

Respuestas (1)

Sean de Wolski
Sean de Wolski el 28 de Mayo de 2015
Editada: James Tursa el 28 de Mayo de 2015

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by