Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Working with timers in GUIDE - An solution and explanation

Subject: Working with timers in GUIDE - An solution and explanation

From: Aleksandar Stojimirovic

Date: 12 Aug, 2010 09:18:08

Message: 1 of 3

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??????? -
-------------------------------------------------------------------------------------------------------

Subject: Working with timers in GUIDE - An solution and explanation

From: Sean

Date: 12 Aug, 2010 12:55:09

Message: 2 of 3

"Aleksandar Stojimirovic" <stojimirovic@yahoo.com> wrote in message <i40e8g$7tp$1@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

Subject: Working with timers in GUIDE - An solution and explanation

From: Steven_Lord

Date: 12 Aug, 2010 15:00:08

Message: 3 of 3



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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us