What is wrong with my timer function? (Too many input arguments?)

Asked by Den on 20 Jul 2012
Latest activity Commented on by Jan Simon on 23 Jul 2012

Hi all, amateur Matlab user here, so I apologize if this question seems silly.

I have a constantly changing variable called 'a', and I want to create an array called 'asamplevalues' that contains the value of 'a' sampled at 4 second intervals.

Here is the part of the script that describes and calls up the timer function:

handles.samplingtimer = timer('Period', 4.0, 'ExecutionMode','fixedRate','TasksToExecute', 10e6);
handles.samplingtimer.TimerFcn = { @samplinga, handles };
start (handles.samplingtimer);

Here is the timer function itself:

function [asamplevalues] = samplinga(a)
    if isempty(asamplevalues) == 1
    asamplevalues = [100];
    else asamplevalues = [asamplevalues, a];
    end
end

When the program gets to the timer function I get this error message:

??? Error while evaluating TimerFcn for timer 'timer-3' 
Too many input arguments.

Any help or suggestions are greatly appreciated! Thanks!

0 Comments

Den

Products

No products are associated with this question.

3 Answers

Answer by Sean de Wolski on 20 Jul 2012

The TimerFcn is essentially a callback that receives the two default input argunments: source, eventdata. You are passing it a thrid input in the form of handles. Thus your samplinga function must account for this:

function [asamplevalues] = samplinga(src,evt,a)

Or, since you aren't counting on using these values:

function [asamplevalues] = samplinga(~,~,a)

5 Comments

Sean de Wolski on 20 Jul 2012

Or to whatever object in handles uses it

Daniel on 23 Jul 2012

@Sean, is that true that callbacks cannot return outputs? I would think that you might want to reuse a function as a callback that does return something (e.g., PLOT). That said I cannot find an example in which one of my callbacks is a function that returns something.

Jan Simon on 23 Jul 2012

@Daniel: The callbacks are executed in the base workspace. I expect the outputs to appear there.

Sean de Wolski
Answer by Star Strider on 20 Jul 2012

Just a guess, but would inserting

asamplevalues = [];

before your ‘if’ statement solve the problem?

0 Comments

Star Strider
Answer by Jan Simon on 20 Jul 2012
Edited by Jan Simon on 20 Jul 2012
handles.samplingtimer = timer('Period', 4.0, ...
             'ExecutionMode','fixedRate','TasksToExecute', 10e6, ...
             'TimerFcn', @samplinga, ...
             'UserData', zeros(1e6, 0));  % Pre-allocate?!
start(handles.samplingtimer);
function samplinga(TimerH, EventData)
  u = get(TimerH, 'UserData');
  k = get(TimerH, 'TasksExecuted');
  if k == 1
    u(1) = 100;
  else
    u(k) = a;   % But where does this "a" come form?!
  end
end

The constantly changing variable "a" is not visible inside the timer callback. Therefore this function will not work without defining where "a" should be taken from. Note, that you cannot simply copy a value from another thread: It is possible that if "a" is a 64 bit value (e.g. a DOUBLE), that the main thread (where "a" is constantly changed) has updated the first 32 bit, but not the sendond one, such that reading the value from the timer's thread will create nonsense.

It seems to be more promissing to include the acquisition of "a" directly in the timer's callback function.

0 Comments

Jan Simon

Contact us