Use of timer: what's the code to indicate that its period of time is expired?

2 次查看(过去 30 天)
I created a timer with a period of 30 seconds: the program has to execute some tasks if this period is passed. How do i write the code for expressing the fact that the period is passed?

回答(2 个)

Doug Hull
Doug Hull 2013-4-3
编辑:Doug Hull 2013-4-3
It is not clear what you are asing, but this is a primer on timers.
  1 个评论
Umberto
Umberto 2013-4-4
I didnt't find the answer. Here is my code
V = xlsread('data.xls', 1, 'C2:C1002'); %import data from Excel
t = timer('Period', 30, 'TimerFcn', @(~,~)calculate(V, g, n, Vmin, Vmax));
function calculate(V, g, n, Vmin, Vmax)
for i=1:n
if V(i) < Vmin
start(t);
if %30 seconds have passed
V(i) = V(i)+ g;
end
elseif V(i) > Vmax
start(t);
if %30 seconds have passed
V(i) = V(i)- g;
end
elseif (V(i) >= Vmin && V(i) <= Vmax)
stop(t);
end
end
how do I express in code the sentence "30 seconds have passed"?

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2013-4-4
One thing you could be do would be to toc a tic that was started earlier. However, the best approach would be to get the InstantPeriod from the timer object
function timerNSeconds
T = timer('Period',10,... %period
'ExecutionMode','fixedRate',... %{singleShot,fixedRate,fixedSpacing,fixedDelay}
'BusyMode','drop',... %{drop, error, queue}
'TasksToExecute',2,...
'StartDelay',0,...
'TimerFcn',@(src,evt)tfcn(src,evt,pi),...
'StartFcn',[],...
'StopFcn',[],...
'ErrorFcn',[]);
start(T);
end
function tfcn(src,evt,piapprox)
pause(10) %comment this line to have it not exceed
pi
if get(src,'InstantPeriod') > 10
disp('Period exceeded 10');
end
end
Uncomment the pause to see it working with not exceeding the 10s.
  20 个评论
Umberto
Umberto 2013-4-12
But if I pass src to calculate() then why I get the "Undefined function or variable 't'" message?
Sean de Wolski
Sean de Wolski 2013-4-12
Because you don't pass t!!!! You've renamed it to src even though it is the same thing. You call it t instead if you wish; it's just a good programming practice to call the handle of the source object of a callback src or similar.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by