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!
No products are associated with this question.
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)
@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.
Just a guess, but would inserting
asamplevalues = [];
before your ‘if’ statement solve the problem?
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