Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pass a parameter to TimerFcn?

790 views
Skip to first unread message

jk Wang

unread,
Oct 30, 2007, 7:43:29 PM10/30/07
to
Hi guys, I need your expertise on this problem. I tried
several statements to pass a parameter to TimerFcn of a
timer obj 't', but all failed. Here are what I did,

First, I tried,

t = timer('TimerFcn','InfoUpdate
(handles)','ExecutionMode','fixedRate','Period',10);
% where InfoUpdate(handles) is a function declared in the
same .m file.

The warning was "undefined variable or
function 'handles'". Someone told me TimerFcn executes in
the base Matlab workspace, so it can't find function
defined inside '...'.

Then I tried

t = timer('TimerFcn',
{@InfoUpdate,handles},'ExecutionMode','fixedRate','Period',
10);
% where InfoUpdate(handles) is a function declared in the
same .m file

The warning was "too many input argument for TimerFcn".

But when I implemented InfoUpdate(handles) alone, it ran
well and no error occured.

The 'handles' parameter is required in my GUI .m file, and
can't be avoid using.

So how can I pass the parameter 'handles' into TimerFcn?
Thank you~


Yair Altman

unread,
Oct 31, 2007, 6:19:36 AM10/31/07
to
"jk Wang" <wjk...@gmail.com> wrote in message
<fg8fj1$l95$1...@fred.mathworks.com>...
> ...

> t = timer('TimerFcn',
> {@InfoUpdate,handles},'ExecutionMode','fixedRate','Period',
> 10);
> % where InfoUpdate(handles) is a function declared in the
> same .m file
>
> The warning was "too many input argument for TimerFcn".
>
> But when I implemented InfoUpdate(handles) alone, it ran
> well and no error occured.

Matlab automatically passes extra arguments to callback
functions, namely the callback object and an
object-dependent data structure. All user-specified args are
passed as additional ags, following the default callback
args. To see which args are passed in actuality:

1. modify your callback function signature as follows:
function InfoUpdate(varargin)

2. Now place a breakpoint on the first line of this function
using the editor or the dbstop() function

3. Now run your code and check varargin - it will contain
all the actual args passed by Matlab in run-time

4. Finally, modify your callback function's signature based
on what you found in step 3.

Note: I could have given you the correct function signature
but then you & other readers would not be able to solve
similar problems for other callbacks - the methodology above
gives a general-purpose way to solve this kind of problem.

Yair Altman
http://ymasoftware.com

jk Wang

unread,
Oct 31, 2007, 10:10:50 PM10/31/07
to
It works like a charm. Thanks!

JK Wang

Eva Ficici

unread,
Nov 19, 2010, 3:08:03 AM11/19/10
to
Hello,
I am curious to understand your solution to this problem,
because I am running into the same issue.

Specifically,
what do you mean by change the function's signature?

> 3. Now run your code and check varargin - it will contain
> all the actual args passed by Matlab in run-time
>
> 4. Finally, modify your callback function's signature based
> on what you found in step 3.


"Yair Altman" <altma...@gmailDEL.comDEL> wrote in message <fg9kro$4oh$1...@fred.mathworks.com>...

Chris

unread,
Aug 16, 2012, 3:55:13 PM8/16/12
to
This is a bit of a late response, but here is what I did to work through his suggestion today:

I started with a function that I had already written and decided I wanted to call it from a timer. The function declaration initially looked like this (only passing it 'handles' so that it can interact w/ the GUI elements):

function plotCalculatedData(handles)

When I defined the TimerFcn of my new timer I tried to do this:

set(CalcDataTimer,'TimerFcn',{@plotCalculatedData,handles})

and received this error message when trying to execute 'start(CalcDataTimer)'

??? Error while evaluating TimerFcn for timer 'timer-8'

Too many input arguments.

Upon changing the function definition to the below and placing a breakpoint at the first line of the function:

function plotCalculatedData(varargin)

I called it again by trying 'start(CalcDataTimer)'.

Matlab stops in the debugger and you can take a look at the value of varargin and, for me, it showed

>> varargin

varargin =

[1x1 timer] [1x1 struct] [1x1 struct]

The first item was the Timer itself. The Second was some information about the timer, and the third was the list of my GUI's handles. I changed my function definition to the below, and it worked perfectly:

plotCalculatedData(timer,timerInfo,handles)

Hope this helps

-Chris


"Eva Ficici" <koza...@rohan.sdsu.edu> wrote in message <ic5b93$e2s$1...@fred.mathworks.com>...

Steven_Lord

unread,
Aug 16, 2012, 5:28:13 PM8/16/12
to


"Chris " <chris...@aei.com> wrote in message
news:k0jj71$395$1...@newscl01ah.mathworks.com...
> This is a bit of a late response, but here is what I did to work through
> his suggestion today:
>
> I started with a function that I had already written and decided I wanted
> to call it from a timer. The function declaration initially looked like
> this (only passing it 'handles' so that it can interact w/ the GUI
> elements):
>
> function plotCalculatedData(handles)
>
> When I defined the TimerFcn of my new timer I tried to do this:
>
> set(CalcDataTimer,'TimerFcn',{@plotCalculatedData,handles})

Note that this will pass a copy of the handles structure AS IT EXISTS WHEN
THIS LINE OF CODE EXECUTES to the TimerFcn each time it is executed. Later
changes to the variable handles or to the "master" handles structure in your
GUI will NOT be reflected by this copy.

> and received this error message when trying to execute
> 'start(CalcDataTimer)'
>
> ??? Error while evaluating TimerFcn for timer 'timer-8'
> Too many input arguments.
>
> Upon changing the function definition to the below and placing a
> breakpoint at the first line of the function:
>
> function plotCalculatedData(varargin)
>
> I called it again by trying 'start(CalcDataTimer)'.
>
> Matlab stops in the debugger and you can take a look at the value of
> varargin and, for me, it showed
>
>>> varargin
>
> varargin =
> [1x1 timer] [1x1 struct] [1x1 struct]
>
> The first item was the Timer itself. The Second was some information
> about the timer, and the third was the list of my GUI's handles. I
> changed my function definition to the below, and it worked perfectly:
>
> plotCalculatedData(timer,timerInfo,handles)

Yes.

http://www.mathworks.com/help/techdoc/matlab_prog/f9-39541.html#f9-42494

First input is the timer object itself, second is event data, third and
later are user-specified.

If you couldn't change the signature of plotCalculatedData for some reason,
you could still use it as your TimerFcn by specifying the TimerFcn property
of the TIMER object as an anonymous function that serves as an "adapter"
between the signature TIMER expects your TimerFcn to have and the signature
that plotCalculatedData provides.

adapter = @(timerobject, eventdata) plotCalculatedData(handles);
t = timer('TimerFcn', adapter)


How would I handle the handles structure changing? I would pass the handle
of the main GUI figure (which probably shouldn't be changing) into the
TimerFcn and use that handle to retrieve the "master" handles structure each
time the TimerFcn executes.

mytimerfcn = {@plotCalculatedData, handles.mainGUIfigure};

Use GUIDATA inside plotCalculatedData to retrieve the up-to-date handles
structure.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

shuchita bahadur

unread,
Jan 19, 2017, 7:03:08 AM1/19/17
to
"Yair Altman" wrote in message <fg9kro$4oh$1...@fred.mathworks.com>...
thanks YAIR, it works
0 new messages