MATLAB Answers

how to stop a loop with a button in GUI

1 view (last 30 days)
Adam on 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
while handles.stop == 0
[data] = f_Read(2,2)
function Stop_button_Callback(hObject, eventdata, handles)
%executes when the 'Stop' button is pressed
handles.stop = 1


Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 16 Jan 2012
Please see my answer to this question:

  1 Comment

Daniel Shub
Daniel Shub on 20 Mar 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by