how to stop a loop with a button in GUI

2 Ansichten (letzte 30 Tage)
Adam
Adam am 16 Jan. 2012
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

Antworten (1)

Sean de Wolski
Sean de Wolski am 16 Jan. 2012
Please see my answer to this question:
  1 Kommentar
Daniel Shub
Daniel Shub am 20 Mär. 2012
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by