|
MATLAB documentation don't address question how to work with timers in GUIDE in a proper way. As I saw, many users have this problem. Timers in GUIDE are important component if we want to design or simulate real time processess. Here is an solution, if you have better idea pleas respond to this messsage.........
Here is an solution:
GUI specification:
a) Develope GUI with two components 1) Popup menu 2) Static text
b) In popup menu it can be choosen between 1) timer on, or 2) timer off
c) When 'timer on' is selected, numbers in static text start to increse in intervals of 1sec
d) When 'timer off' is selected, timer is of and Static text hold last displayed value.
Solution:
>>>First it is defined global variable var in gui_OpeningFcn
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
....
global var;
var.x=0;
var.tmr=timer('TimerFcn',{@TimerCallback,hObject,handles},'Period',1,'ExecutionMode','FixedRate');
>>> It is defined timer Callback function
function TimerCallback(obj,event,hObject,handles)
global var;
set(handles.text1,'String',var.x);
var.x=var.x+1;
>>> It is defined Popup manu Callback function
function popupmenu1_Callback(hObject, eventdata, handles)
global var;
z=get(hObject,'Value');
if z==1;
start(var.tmr);
else
stop(var.tmr);
end
>>> It is defined figure1_DeleteFcn
function figure1_DeleteFcn(hObject, eventdata, handles)
global var;
stop(var.tmr);
delete(var.tmr);
Explanation:
Global variable var is supstitution for handles structure which can't be shared with TimerCallback function. For example, if we define handles.x, and change its walue in TimerCallback function it can't be remember there. From same reason function set(handles.text1,'String',var.x); work properely in TimerCallback. Global variable var is reachible from each Callback function.
-------------------------------------------------------------------------------------------------------
- SOLUTION IS NOT THE BEST BUT WORKS, ANY BETTER IDEA??????? -
-------------------------------------------------------------------------------------------------------
|