How to assign a timer callback to call a function whose input arguments do not include "obj" and "event" but include "arg1" and "arg2"?
2 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2019-8-7
编辑: MathWorks Support Team
2024-8-29
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
采纳的回答
MathWorks Support Team
2024-7-29
编辑:MathWorks Support Team
2024-8-29
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)
Please run the below command in the command window of installed MATLAB R2019a version to get release specific documentation for other examples/syntax for creating/assigning timer function callbacks:
>> web(fullfile(docroot, 'matlab/matlab_prog/timer-callback-functions.html'))
Please follow the below link to search for the required information regarding the current release:
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!