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

14 visualizaciones (últimos 30 días)
Den
Den el 20 de Jul. de 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!

Respuestas (3)

Sean de Wolski
Sean de Wolski el 20 de Jul. de 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 comentarios
Daniel Shub
Daniel Shub el 23 de Jul. de 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
Jan el 23 de Jul. de 2012
@Daniel: The callbacks are executed in the base workspace. I expect the outputs to appear there.

Iniciar sesión para comentar.


Star Strider
Star Strider el 20 de Jul. de 2012
Just a guess, but would inserting
asamplevalues = [];
before your ‘if’ statement solve the problem?

Jan
Jan el 20 de Jul. de 2012
Editada: Jan el 20 de Jul. de 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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by