Interrupting a for loop with a GUI Pushbutton

조회 수: 12 (최근 30일)
Casey Donnelly
Casey Donnelly 2015년 5월 28일
댓글: MINKYUNG KIM 2019년 10월 14일
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
Gabriel
Gabriel 2017년 9월 27일
Hi Casey Donnelly Thank you for your help it finally worked for me too.
MINKYUNG KIM
MINKYUNG KIM 2019년 10월 14일
THANK YOU A LOT!!

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2015년 5월 28일
편집: James Tursa 2015년 5월 28일

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by