Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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?

Respuestas (2)

Doug Hull
Doug Hull el 3 de Abr. de 2013
Editada: Doug Hull el 3 de Abr. de 2013
It is not clear what you are asing, but this is a primer on timers.
  1 comentario
Umberto
Umberto el 4 de Abr. de 2013
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"?

Iniciar sesión para comentar.


Sean de Wolski
Sean de Wolski el 4 de Abr. de 2013
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 comentarios
Umberto
Umberto el 12 de Abr. de 2013
But if I pass src to calculate() then why I get the "Undefined function or variable 't'" message?
Sean de Wolski
Sean de Wolski el 12 de Abr. de 2013
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by