File: ef230-2023-08/www/examples/timer_demo.m Download
%! Example of using a timer to simulate a clock ticking
% University of Tennessee : EF 230 Fall, 2009 : Will Schleter
function main
clear all; close all; clc;
global g
g.tictocflag=false;
g.count=0;
g.timer=timer('TimerFcn',@updateclock,'period',1,'executionmode','fixedrate');
start(g.timer)
return

function updateclock(obj,event)
global g
g.count=g.count+1;
if g.count>10
    stop(g.timer);
    disp('Stopped timer');
    return;
end
if g.tictocflag
    msg='tic';
else
    msg='toc';
end
event_time = datestr(event.Data.time);
fprintf('%u %s %s\n',g.count,msg,event_time);
g.tictocflag = ~g.tictocflag;
return