Got Questions? Get Answers.
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:
timer function in GUIDE

Subject: timer function in GUIDE

From: Javier

Date: 16 Jun, 2010 13:00:24

Message: 1 of 4

Hello!

I'm really new and I program a lot in a try-error way so I have some stuff already working but unfortunately I don't understand it all in deep.

I'm trying to write a timer that begins when my GUI OpeningFcn starts [it is for continuously updating a static text display]. Inside the OpeningFcn I wrote:

     handles.output = hObject;
     refreshTimer=timer('StartDelay',1,'Period',2,'UserData',handles,...
            'TasksToExecute', 3 ,'ExecutionMode','FixedRate','BusyMode','drop');
    refresh.TimerFcn = 'refresh';
    start(refreshTimer);


Then, in other file called refresh.m I wrote the refresh function:

function refresh
   some stuff using handles, updating display etc
end

The question is how to pass the GUI handles to the refresh functions and eventually how to get new values of handles from the refresh function to the GUI [and also how to define precisely all this things, the timer and the first line of function refresh].

Hope my english is understandable.

Many thanks in advance

Subject: timer function in GUIDE

From: Steven Lord

Date: 16 Jun, 2010 13:31:21

Message: 2 of 4


"Javier " <jagon@postal.uv.es> wrote in message
news:hvaht8$ls7$1@fred.mathworks.com...
> Hello!
>
> I'm really new and I program a lot in a try-error way so I have some stuff
> already working but unfortunately I don't understand it all in deep.
>
> I'm trying to write a timer that begins when my GUI OpeningFcn starts [it
> is for continuously updating a static text display]. Inside the OpeningFcn
> I wrote:
>
> handles.output = hObject;
> refreshTimer=timer('StartDelay',1,'Period',2,'UserData',handles,...
> 'TasksToExecute', 3
> ,'ExecutionMode','FixedRate','BusyMode','drop');
> refresh.TimerFcn = 'refresh';
> start(refreshTimer);

You should store the timer object somewhere (say inside the handles
structure for your GUI, or in the UserData of the GUI figure, etc.) so you
have easy access to it when you need to stop it.

I would also specify the TimerFcn as a function handle rather than a string
containing the name of the function to be invoked.

> Then, in other file called refresh.m I wrote the refresh function:
>
> function refresh
> some stuff using handles, updating display etc
> end
>
> The question is how to pass the GUI handles to the refresh functions and
> eventually how to get new values of handles from the refresh function to
> the GUI [and also how to define precisely all this things, the timer and
> the first line of function refresh].

Well, you stored a _copy_ of the handles structure as it existed when the
timer was created in the timer's UserData, but that may get out of date
quickly. I would instead store the handle of the GUI figure in the UserData
and use GUIDATA inside your timer callback to retrieve an up-to-date copy of
the handles structure. You can also use that to update the figure's "master
copy" if you make any changes inside your timer callback.

As for the precise signature of the refresh function? The documentation for
TIMER should tell you what signature the timer expects its TimerFcn to have.
The first input to the TimerFcn will be the handle to the timer, with which
you will be able to access its UserData property.

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

Subject: timer function in GUIDE

From: Javier

Date: 16 Jun, 2010 14:13:06

Message: 3 of 4

"Steven Lord" <slord@mathworks.com> wrote in message <hvajn9$lt2$1@fred.mathworks.com>...
>
> "Javier " <jagon@postal.uv.es> wrote in message
> news:hvaht8$ls7$1@fred.mathworks.com...
> > Hello!
> >
> > I'm really new and I program a lot in a try-error way so I have some stuff
> > already working but unfortunately I don't understand it all in deep.
> >
> > I'm trying to write a timer that begins when my GUI OpeningFcn starts [it
> > is for continuously updating a static text display]. Inside the OpeningFcn
> > I wrote:
> >
> > handles.output = hObject;
> > refreshTimer=timer('StartDelay',1,'Period',2,'UserData',handles,...
> > 'TasksToExecute', 3
> > ,'ExecutionMode','FixedRate','BusyMode','drop');
> > refresh.TimerFcn = 'refresh';
> > start(refreshTimer);
>
> You should store the timer object somewhere (say inside the handles
> structure for your GUI, or in the UserData of the GUI figure, etc.) so you
> have easy access to it when you need to stop it.

Sorry Steven, as I said all this terminology is not so clear to me. I'm really a noob also in programming.
You mean something like:
handles.refreshTimer=timer(.....
and then
start(handles.refreshTimer)

>
> I would also specify the TimerFcn as a function handle rather than a string
> containing the name of the function to be invoked.
>

Could you please write the codeline? It's not that I want you to do my job but maybe looking it I understand it better :)

> > Then, in other file called refresh.m I wrote the refresh function:
> >
> > function refresh
> > some stuff using handles, updating display etc
> > end
> >
> > The question is how to pass the GUI handles to the refresh functions and
> > eventually how to get new values of handles from the refresh function to
> > the GUI [and also how to define precisely all this things, the timer and
> > the first line of function refresh].
>
> Well, you stored a _copy_ of the handles structure as it existed when the
> timer was created in the timer's UserData, but that may get out of date
> quickly. I would instead store the handle of the GUI figure in the UserData

How to do that?

> and use GUIDATA inside your timer callback to retrieve an up-to-date copy of
> the handles structure. You can also use that to update the figure's "master
> copy" if you make any changes inside your timer callback.
>
> As for the precise signature of the refresh function? The documentation for
> TIMER should tell you what signature the timer expects its TimerFcn to have.
> The first input to the TimerFcn will be the handle to the timer, with which
> you will be able to access its UserData property.
>
> --
> 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
>

Thanks a lot.

Subject: timer function in GUIDE

From: FMC

Date: 29 Aug, 2010 16:58:03

Message: 4 of 4

Another way to do this is to store stuff in the appdata. To store stuff in the appdata, you need a figure. This example creates a dummy figure at the start that allows you to store things in the figure handle appdata and then gets used at the end to plot the frequency.
For how to create functions and pass arguments, see the Matlab help on:
Specifying the Value of Callback Function Properties

function myfileset(h, 'StartFcn', 'myfile')
function myfile(obj, event)set(h, 'StartFcn', @myfile)
function myfile(obj, event, arg1, arg2)set(h, 'StartFcn', {'myfile', 5, 6})
function myfile(obj, event, arg1, arg2)set(h, 'StartFcn', {@myfile, 5, 6})

Here's an example:

function fast_timer_test

% create a figure so we can store things in the figure handle appdata
hF = figure(1);
% pre allocate the time_stamp storage
time_stamps = zeros(5000, 1);
count = 1;

% set the time_stamps and the counter in the appdata
% so that these are available in the timer function
setappdata(hF, 'TimeStampData', time_stamps);
setappdata(hF, 'Counter', count);

% create the timer function
t = timer('ExecutionMode','fixedRate', 'Period',... 0.1, 'StartDelay',1, 'BusyMode', 'queue');
% set the timetFcn and pass the figure handle as a parameter
t.TimerFcn = {@timer_fcn, hF};

% get the start time
tic
% start the timer
start(t);
% wait 10 seconds for the timer to do its thing
pause(10);
% stop the timer
stop(t);
delete(t);
% get the the counter that was updated in the timer function
% out of the appdata to use it here
count = getappdata(hF, 'Counter');
% take the difference to find the frequency
a = diff(time_stamps(1:count));
% calculate the frequency
freq = 1./a;
% plot the frequency
plot(freq);

%%
%-------------------------------------------------------------
function timer_fcn(obj, event, hF)

% having passed the figure handle, we can now get the
% counter out of the figure handle appdata
count = getappdata(hF, 'Counter');
t_stamp = toc;
% store the current time in the time_stamp array
time_stamps(count) = t_stamp;
% incrument the counter
count = count + 1;
% now set the counter and the time stamp back into the appdata
% so that they are available in the main function
setappdata(hF, 'Counter', count);
setappdata(hF, 'TimeStampData', time_stamps);

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