From: Aleksandar Stojimirovic on
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??????? -
-------------------------------------------------------------------------------------------------------
From: Sean on
"Aleksandar Stojimirovic" <stojimirovic(a)yahoo.com> wrote in message <i40e8g$7tp$1(a)fred.mathworks.com>...
> 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??????? -
> -------------------------------------------------------------------------------------------------------


A few (very powerful) hints:
doc setappdata
doc getappdata
From: Steven_Lord on


"Aleksandar Stojimirovic" <stojimirovic(a)yahoo.com> wrote in message
news:i40e8g$7tp$1(a)fred.mathworks.com...
> 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.

Correct me if I'm wrong, but if you're bringing up a GUI to simulate a
"real-time" process, aren't you introducing a distinctly non-real-time
component into the mix (i.e. the user who's interacting with the GUI?) What
happens if your user gets a phone call or has to answer the call of nature
in the middle of using your GUI?

*snip*

> Explanation:
> Global variable var is supstitution for handles structure which can't be
> shared with TimerCallback function.

Sure the handles structure can be shared with a TimerFcn ... as long as you
do it right. There's no need for global variables in this application.

> For example, if we define handles.x, and change its walue in TimerCallback
> function it can't be remember there.

Pass a handle to some object in the GUI into the Timer's TimerFcn.
Use GUIDATA to obtain the handles structure using that GUI object.
Modify the handles structure as appropriate.
***IMPORTANT*** Use GUIDATA again to update the "master copy" of the handles
structure with the copy modified in the TimerFcn.
***IMPORTANT 2*** If a callback function already had a copy of the handles
structure in its workspace when the TimerFcn modified the master copy, that
callback will need to use GUIDATA to synchronize its copy with the master
copy to recognize any changes the TimerFcn made to the master copy.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com