how to stop a loop with a button in GUI

2 次查看(过去 30 天)
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