I'd do a couple of things here.
First, you probably don't really need to pass the h
, m
, s
inputs, if you are displaying current time. Add this to the top of your function to auto set these variables.
if nargin == 0
[~,~,~,h,m,s] = datevec(now);
end
Then, it is pretty easy to use a time to call this function periodically. Something like this.
t = timer;
t.ExecutionMode = 'FixedSpacing'; %Use one of the repeating modes
t.Period = 1; %Fire on 1 second intervals
t.TimerFcn = @(~,~)raviClock; %When fired, call this function (ignoring 2 inputs)
start(t); %GO!
Use docsearch timer
for in depth documentation of the timer objects. But the code above should get you started.
To stop the timer, run
stop(t);
To stop the timer when the window is closed, put the stop command into the window deletion callback:
set(gcf,'DeleteFcn',@(~,~)stop(t)); %NOte: Better to explicitly use a figure number, rather than gcf.