How to implement a timer in AppDesigner

Hello,
i hope somebody may can help me. I try to implement a timer (periodic 2ms) in Matlab AppDesiner (2016a). When the timer executes i will read data from a USB buffer. My problem is to set up the timer correctly so that the callback function TimerFcn is started.
Thanks
Best Regards Hannes

 Accepted Answer

Chris Portal
Chris Portal on 5 Aug 2016
Edited: per isakson on 27 Jul 2017
There are a few ways of doing this, but here's one way...
1) Create a custom property called myTimer, which would look like this:
properties (Access = private)
myTimer % Description
end
2) In your startup function, create your timer object, assign it to the custom property, and configure your timer callback. Something like this:
app.myTimer = timer('Period',2,...
'ExecutionMode', 'fixedSpacing', ...
'TasksToExecute', Inf);
app.myTimer.TimerFcn = @(x,y)disp('do something');
Note, if you need to wire your timer callback to a function inside your app, the TimerFcn syntax would be a little different. You can reference this post for a few options for that:

6 Comments

Thanks for the excellent answer. Just a couple of minor remarks which I ran into upon trying to apply this example:
  1. The Period of the timer function is given in seconds, so the first parameter of the timer function should be 0.002 in order to reach 2ms.
  2. After setting up the timer, it needs to be started via
start(app.myTimer)
Additionally, here is the easiest implementation of a callback to a function inside your app:
app.myTimer.TimerFcn = @(~,~) app.myFunction;
could you please explain @(~,~)?
TimerFcn has to be a function. This syntax creates an anonymous function you can assign to TimerFcn and will be executed by the timer after the designated period, once it is started.
Hello,
I've read the solutions suggested above and have tried to implement a timer within my app (R2019b) created with App Designer. My app will update a photo display every 15 seconds.
I seem to have not properly registered my TimerFcn because I receive the following error when I try to start the timer with "start(app.photoTimer);"
"Error while evaluating TimerFcn for timer 'timer-1'
Unrecognized method, property, or field 'photoTimerFcn' for class 'photoViewer'. "
Could someone please look over my code chunk below and comment on what I have missed? Thank you, David
-------------------
My startup code for the timer includes:
function startupFcn(app)
period=15; %seconds; period for timer
app.photoTimer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', period, ... % Period is in seconds
'BusyMode', 'queue');
app.photoTimer.TimerFcn = @(~,~) app.photoTimerFcn;
start(app.photoTimer);
end
---
My properties and methods include:
properties (Access = private)
photoTimer % Timer object
end
methods (Access = private)
function app.photoTimerFcn(app,~,~)
%code to change photo here
end
end
Were you able to solve this problem?
Best,
Pedro
Yes. I had made some minor errors and it is working now. Thanks all for your help. David

Sign in to comment.

More Answers (1)

Adee
Adee on 23 Sep 2023
Edited: Adee on 23 Sep 2023
The previous answer notes that "if you need to wire your timer callback to a function inside your app, the TimerFcn syntax would be a little different. You can reference this post for a few options (...)".
The refecenced post is long and It took me some experiments to find a clean way to do that, so I thought it's worth a separate answer.
The value provided for TimerFcn should be a function that accepts a timer and an event, and calls the callback method; but the callback method expects app as the first argument, so some translation is required.
In addition, the callback should be assigned separately after app has been properly created, in a startupFcn.
This can be done as follows:
properties (Access = private)
timer = timer('Name', 'App timer', 'Period', 15, 'ExecutionMode', 'fixedSpacing')
% This would create a delay of 15 seconds from end of callback invocation to next execution
end
methods (Access = private)
function timer_callback(app, timer_obj, event)
% Do whatever is needed
end
function startupFcn(app)
app.timer.TimerFcn = @(timer_obj, event) app.timer_callback(timer_obj, event);
end
end
Then, start and stop the timer using
app.timer.start;
and
app.timer.stop;
.

Categories

Asked:

on 27 Jul 2016

Edited:

on 23 Sep 2023

Community Treasure Hunt

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

Start Hunting!