How to assign a timer callback to call a function whose input arguments do not include "obj" and "event" but include "arg1" and "arg2"?

17 views (last 30 days)
How can I use a timer callback function (e.g "timertestfunction"), whose input arguments do not include "obj" and "event" but include "arg1" and "arg2".
For example, if my function definition is as follows, then how can I assign this to the timer callback function?
function timertestfunction(arg1,arg2)
disp([arg1,arg2])
end

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Aug 2019
You can use an anonymous function (for the "TimerFcn" callback) that take two input arguments (for "obj" and "event") and calls your other function with its corresponding input arguments (e.g. "arg1" and "arg2"). Here is an example to illustrate this, based on the "timertestfunction" example that you provided:
% Define some parameters to be passed in as input arguments to "timertestfunction"
x = 'hello '; y=' goodbye';
% Define the Timer
t = timer( ...
'TimerFcn', @(~,~)timertestfunction(x,y), ...
'StartDelay', 5, ...
'ExecutionMode', 'fixedRate', 'Period', 5, ...
'TasksToExecute', 2, 'BusyMode', 'drop', ...
'Name', 'timetestfunction', 'Tag', 'timetestfunction');
% Execute the Timer
start(t)
For other examples/syntax for creating/assigning timer function callbacks, please refer to the following documentation page.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!