How to use Timer Callback in App Designer?

202 views (last 30 days)
Using the App Designer, I would like to use the Timer callback within a callback section of the code. However, I am unable to reference functions from within the app using the function handle. How do I go about this? 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Jun 2017
In order to use the Timer Callback in App Designer, please follow the rules as follows-
1. Update the callback function signature to have four input arguments: 'app', 'obj', 'event', 'string_arg' as shown below:
function my_callback_fcn(app, obj, event, string_arg)
Note 'app' is the handle to the app object (this is necessary because the function is a method of the app), 'obj' is the handle to the timer, 'event' is the event data for the timer event, and 'string_arg' is the additional input argument specified by you.
2. Because the function my_callback_fcn is a method on the app object, you need to reference it using the 'app.' prefix.
To set the Timer object's properties like "t.StartFcn" and "t.StopFcn", please specify the callback function using the 'app.' prefix as shown below:
t.StartFcn = {@app.my_callback_fcn, 'my start message'};
t.StopFcn = {@app.my_callback_fcn, 'my stop message'};

More Answers (3)

mansour torabi
mansour torabi on 1 Oct 2021
Edited: mansour torabi on 1 Oct 2021
In App Designer:
In Properties section, define a Variable to hold time object:
properties (Access = public)
MyTimer = 0;
Inside startupFcn(app) (callback function when UI starts), config the timer object:
function startupFcn(app)
app.MyTimer = timer('Period',0.5,'ExecutionMode','fixedSpacing',...
'TasksToExecute', Inf);
app.MyTimer.TimerFcn = @app.TimerCallback; % Your Timer Callback
Inside methods section, Define TimerCallback function, like bellows:
methods (Access = public)
function TimerCallback(app, obj, event)
UpdateSomeUIcomponent(app, someValue)
Start the timer, for example, after some button clicked:
function YourButtonPushed(app, event)
You should, call stop(app.MyTimer); to stop the timer object, when your are done!

mk_lab on 29 Jun 2017
Hey! I am trying exactly the same.
What i already have:
app.checkStatusTimer = timer('StartDelay', 4, 'Period', 500e-3, 'TasksToExecute', Inf, 'ExecutionMode', 'fixedRate');
app.checkStatusTimer.TimerFcn = @(x,y)disp('Hello World!');
This works, every 500 ms 'Hello World!' is displayed. But I need to call a function instead, as described above.
Can you maybe give a code example? Or explain in a bit more detail what you mean with "Update the callback function signature"
Thanks in advance!!
Kevin J. Delaney
Kevin J. Delaney on 27 Mar 2018
Many thanks for posting this! Just what I needed. Kevin

Sign in to comment.

John Lutz
John Lutz on 22 Jul 2022
Edited: John Lutz on 23 Jul 2022
App Designer Function I/O
For anyone wanting to use a timer to activate a callback function with I/O like a regular function in app designer.
Great way to not use pause in your code.
Other app code is not here. This will not work by itself. You need to create a new app and add a button, the buttons callback event, and the function.
methods (Access = private) % make a private (or public) function in your app
function mult(app, timer_obj, ~, num1, num2) % ~ is event which is not used. we have 2 extra input args
timer_obj.UserData(1) = num1 * num2 ; % do something and store the result
timer_obj.UserData(2) = toc ; % store elapsed time to complete
% my task is time sensitive so I need to know how long it took
Callbacks that handle component events
methods (Access = private)
% Button pushed function: TimerTestButton
function TimerTestButtonPushed(app, event)
UserData = [0 0] ; % initiate an array to store callback function outputs
num1 = 3 ; num2 = 5 ; % inputs to callback function
infinicount = 0 ; infinimax = 12000 ; % 'uses' ~ 10k tics, prevent infinite loop
timer_obj = timer(...
'Name', 'timer_mult', ...
'TimerFcn', {@app.mult, num1, num2}, ...
'StartDelay', 0.246, ... % works within a few 100'ths of a sec
'UserData', UserData ...
) ;
while UserData(1) == 0 && infinicount < infinimax
UserData = get(timer_obj, 'UserData') ; % is the job done yet ?
infinicount = infinicount + 1 ;
% pause(0.01) % previous diagnostic
% toc % slows this down a lot even without pause,
% probably because of display output
disp(UserData) % product from callback function and elapsed time
disp(infinicount) % how many loops did it take ?
% delete(timer_obj) % uncomment to remove timer object when complete

Community Treasure Hunt

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

Start Hunting!

Translated by