Use of timer: what's the code to indicate that its period of time is expired?
Mostrar comentarios más antiguos
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)
It is not clear what you are asing, but this is a primer on timers.
1 comentario
Umberto
el 4 de Abr. de 2013
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
el 5 de Abr. de 2013
Jan
el 5 de Abr. de 2013
@Umberto: Simply try it by your own.
Umberto
el 5 de Abr. de 2013
Sean de Wolski
el 5 de Abr. de 2013
That's just a code analyzer warning telling you it thinks you're doing something weird. (It tends to be right)
Umberto
el 8 de Abr. de 2013
Jan
el 8 de Abr. de 2013
What does "it doesn't work" exactly mean? We cannot guess these details. We want to help, so please let us and explain the occurring problem.
Sean de Wolski
el 8 de Abr. de 2013
What I have above works, so what have you changed to make it not work?
Umberto
el 8 de Abr. de 2013
Umberto
el 9 de Abr. de 2013
Sean de Wolski
el 9 de Abr. de 2013
The problem looks like the calling syntax for setting up the timerfcn is different than the calling syntax for calculate().
Umberto
el 10 de Abr. de 2013
Sean de Wolski
el 10 de Abr. de 2013
Use the same calling syntax...
calculate(t, src, V, g, n, Vmin, Vmax)
v.
calculate(V, g, n, Vmin, Vmax),...
Umberto
el 11 de Abr. de 2013
Jan
el 11 de Abr. de 2013
@Umberto: Please do not let us guess, what your problem is. Formerly you told us, that there is the message, that "t" is unused. Now you have removed the "t" and get the same message? This is impossible. So please do not assume, that we use crystal balls, but explain the problem exactly and post the relevant part of the code.
Umberto
el 11 de Abr. de 2013
Sean de Wolski
el 11 de Abr. de 2013
So what you're trying to do here isn;t working for a few reasons.
First, t is scoped to the parent function simulation so that is why it does not exist in the subfunction calculate.
Second, I don't understand why you want to start the timer inside of its timerfcn. That doesn't make sense to me.
Third, you are not passing src into calculate(). Thus it will error, you need to pass in src like I did in my original.
Umberto
el 11 de Abr. de 2013
Sean de Wolski
el 11 de Abr. de 2013
src and t are the same thing. They're both handles to your timer.
Umberto
el 12 de Abr. de 2013
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.
Categorías
Más información sobre Performance and Memory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!