using callback function in timer with a function generated within script

1 view (last 30 days)
Hi all,
I think I have a reasonably straightforward question. I am trying to use a timer function in matlab but I am getting a consistent error. I have generated a gui and I want it to execute a function periodically when a button is pressed. The troublesome part of the code looks like this
acquisitionTimer = timer;
acquisitionTimer.ExecutionMode = 'fixedRate';
acquisitionTimer.TasksToExecute = sweepsPerTrigger;
acquisitionTimer.Period = inter_stimulus_interval;
acquisitionTimer.StopFcn = @acquisitionTimerCleanup;
acquisitionTimer.TimerFcn = @(myTimerObj, thisEvent)startAcquisition;
start(acquisitionTimer);
function acquisitionTimerCleanup(acquisitionTimer,~)
disp('Done Acquiring');
delete(acquisitionTimer)
function startAcquisition
(this starts my aquisition code)
--- So I think my problem is with the callback function line
acquisitionTimer.TimerFcn = @(myTimerObj, thisEvent)startAcquisition;
I get the impression this is not how you start a function when linking it with a timer. Any advice would be greatly appreciated. I looked at the documention, but unfortunately, I can't seem to figure it out right now.
Thanks, Quentin

Accepted Answer

per isakson
per isakson on 25 Feb 2014
Edited: per isakson on 25 Feb 2014
I put your code into one m-file and it worked. I cannot see any problem. With R2013a
>> cssm
start Acquisition
start Acquisition
Done Acquiring
>>
where cssm is
function cssm
acquisitionTimer = timer;
acquisitionTimer.ExecutionMode = 'fixedRate';
acquisitionTimer.TasksToExecute = 2;
acquisitionTimer.Period = 1;
acquisitionTimer.StopFcn = @acquisitionTimerCleanup;
acquisitionTimer.TimerFcn = @(myTimerObj, thisEvent)startAcquisition;
start(acquisitionTimer);
end
function acquisitionTimerCleanup(acquisitionTimer,~)
disp('Done Acquiring');
delete(acquisitionTimer)
end
function startAcquisition
% (this starts my aquisition code)
disp('start Acquisition')
end

More Answers (1)

Quentin
Quentin on 25 Feb 2014
Thank you for the help. Once I put it into one code and saw it work, I realized the problem was most likely in the gui. I'm trouble shooting the gui now but your input helped me locate the troublesome code.
Thanks again, Quentin

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!