how to stop a loop with a button in GUI

조회 수: 2 (최근 30일)
Adam
Adam 2012년 1월 16일
I have a have that reads data from a device. I have made a GUI with GUIDE with a 'Read' and 'Stop Read" button.
The program contains a callback function for the Read, and the stop button and the Read callback uses the f_Read() function to read the data. I want the program to stop executing f_Read when i press the stop button. I was thinking that the while loop could evaluate handles.stop == 0, so that while handles.stop = 0, f_Read() will be executed, and then when i press the stop button handles.stop =1 and the while loop would stop. But it seems like it doesnt discover that handles.stop = 1, when i press the stop button, and therefore it just keeps executing f_Read(). What am i doing wrong? Or should i do it in a different kind of way?
function Read_button_Callback(hObject, eventdata, handles)
%Executes when the 'Read data' button is pressed, and lets the program
%read data from the smartbox
handles.stop=0
while handles.stop == 0
[data] = f_Read(2,2)
end
display('stopped')
return
function Stop_button_Callback(hObject, eventdata, handles)
%executes when the 'Stop' button is pressed
handles.stop = 1

답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 1월 16일
Please see my answer to this question:
  댓글 수: 1
Daniel Shub
Daniel Shub 2012년 3월 20일
Depending on how f_Read is implemented, the timer may not be able to break into such a simple loop. It is also not clear that handles is really a handle object, so the timer callback setting handles.stop might not break the loop.

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by