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

조회 수: 15 (최근 30일)
Den
Den 2012년 7월 20일
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!

답변 (3개)

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

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2012년 7월 20일
Just a guess, but would inserting
asamplevalues = [];
before your ‘if’ statement solve the problem?

Jan
Jan 2012년 7월 20일
편집: Jan 2012년 7월 20일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by