Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting a Matlab timer object to time 0

Tags:

timer

matlab

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?

like image 991
Ryan Edwards Avatar asked Jan 30 '26 04:01

Ryan Edwards


1 Answers

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:

  • the reset at StartFcn() is to prevent negative StartDelay from previously called tic (if applies).
  • the disp(toc) in TimerFcn is not necessary and is just to check that the timings are ok.
  • same checking purpose serve the two commented lines in mystop().
like image 188
Oleg Avatar answered Feb 01 '26 18:02

Oleg