I use a Matlab timer object to rotate some log files at a fixed interval (every 1800 seconds):
rotateTimer = timer( ...
'Name', 'Log Rotator', ...
'Period', 1800, ...
'StartDelay', 1800, ...
'ExecutionMode', 'fixedSpacing', ...
'TimerFcn', {@RotateLogs});
start(rotateTimer);
This method works very well for automatic rotations, but I need to allow the user to manually rotate the log files.
When a manual rotation occurs, I want the next automatic rotation to occur 1800 seconds later; this requires that I "reset" the timer object to begin counting up from 0 at the moment of manual rotation.
I don't see a property or method I can invoke that will zero the timer. Starting and stopping the timer does not reset it, it just continues counting from where it was. How can I accomplish this?
An alternative way which does not require to recreate the timer object:
per = 2;
t = timer('Period' , per, ...
'StartDelay' , per, ...
'ExecutionMode', 'fixedSpacing',...
'TimerFcn' , 'disp(toc);tic;',...
'StopFcn' , {@mystop,per},...
'StartFcn' , 'tic;');
% Suppress warning from millisecond precision of StartDelay
warning('off','MATLAB:TIMER:STARTDELAYPRECISION')
start(t)
stop(t)
delete(t)
where mystop() is
function mystop(obj,event,in)
t = toc;
set(obj,'StartDelay',in-t);
disp(t) % Not necessary, just to check
tic % Not necessary, just to check
end
The idea here, is that, you can reset the StartDelay after each stop and to do that you need to time the elapsed time between each timer execution. This means that you have to reset at each execution.
REMARKs:
StartFcn() is to prevent negative StartDelay from previously called tic (if applies).disp(toc) in TimerFcn is not necessary and is just to check that the timings are ok. mystop(). If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With