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

조회 수: 1 (최근 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에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by