infinite loop problem in gui

6 次查看(过去 30 天)
pradeep kumar
pradeep kumar 2014-8-18
评论: Adam 2014-8-20
hi i am new to matlab. i am making gui for serial communication. i have following doubts, so i request seniors to please help me...
1) if i have two buttons(1 and 2) in my gui, and a infinite loop in button 1 callback function, then will it execute button 2 callback function on pressing button 2 ?
2) if yes, will it return back to button 1 infinite loop after executing button 2 callback ?
please explain me with some examples. thanks in advance.....
  3 个评论
Geoff Hayes
Geoff Hayes 2014-8-20
Pradeep - are you using GUIDE to write your GUI? The yourGuiName_OpeningFcn executes just before the GUI is made visible. I typically do any initializations of widgets (text, slider, toggles, etc.) here, or perhaps read data from file, or even start a timer. The yourGuiName_CloseRequestFcn executes when the user attempts to close the GUI (i.e. presses the x in the top left corner of the GUI). I sometimes put code here to pop up a message asking if the user really wants to exit the app. The yourGuiName_DeleteFcn executes during object deletion. This can be a good place to put any code to terminate timers.
A handle is just a reference to an object. This object can be a widget within your GUI, a function, a graphics object (result of calls to plot, scatter), etc.
Adam
Adam 2014-8-20
OpeningFcn and DeleteFcn execute on opening and closing a GUI.
Both are appended to your GUI name as function calls in the .m file and if you want a DeleteFcn you have to explicitly add it by right clicking on your figure in guide and selecting it from the callbacks list (or a similar method).

请先登录,再进行评论。

回答(2 个)

Geoff Hayes
Geoff Hayes 2014-8-18
Pradeep - I'll answer your questions first, but I will recommend that you use an alternative approach to an infinite loop (use a timer).
For your first question, no there will be no response to the second button callback function so long as the first button callback has initiated an infinite loop that does not pause to allow other threads to be processed. For example, add the following push button callbacks to a simple GUI with two buttons
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
lastTime = now;
while true
% write message to console every 5 second
if (now-lastTime)*86400>=5
fprintf('push button 1: it has been five seconds\n');
lastTime = now;
end
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
fprintf('push button 2: has been pressed!\n');
The callback for the first push button prints a message to the console at (roughly) every five seconds; the callback for the second push button prints a message when pressed. Trying this out you will observe that the message for the second push button is written to the console only prior to the first push button being pressed. Once the first is pressed, there is no longer a response to pressing the second push button.
To get around this, you can add a pause in the infinite while loop
while true
% write message to console every 5 second
if (now-lastTime)*86400>=5
fprintf('push button 1: it has been five seconds\n');
lastTime = now;
end
% halt execution for 10 milliseconds
pause(0.01);
end
Now re-run, and press the second push button after pressing the first one. You should observe the second push button message appear in the console, and you will still note that every five seconds, the first push button still writes out its message.
------------
But, rather than using the infinite loop with a pause try using a timer instead. If you have some idea of how often you receive data via the serial connection, then you can periodically invoke a callback to check for new data.
Your first push button code then becomes something like
function pushbutton1_Callback(hObject, eventdata, handles)
% create a timer (pass gcf - the GUI (or current figure) handle)
handles.timer = timer('Name','ExampleTimer', ...
'Period',1.0, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,gcf});
% save the timer handle to the handles structure
guidata(hObject,handles);
% start the timer
start(handles.timer);
So the above will create a timer that will call the timerCallback function every 1.0 seconds. By saving it to the handles structure, we can terminate/end the timer when we desire (say from another push button, or when the GUI closes, etc.) We then immediately start the timer.
The timerCallback signature would look something like
function [] = timerCallback(~,~,guiHandle)
% do stuff periodically
The guiHandle input parameter can be used to get or set GUI app data (to the handles structure) or to the app. See guidata, setappdata, and getappdata.

Adam
Adam 2014-8-18
should help. It's not something I'm familiar with myself as I don't generally do interrupting callbacks (not deliberately anyway).

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by