Digital Clock Using GUI | MATLAB Tutorial

Digital clock using GUI :

The MATLAB software includes a timer object that can be used to schedule the execution of MATLAB commands. We’ll see how to create timer objects, start a timer running, and specify the processing that are to be performed when a timer fires. ‘A timer is said to fire when the amount of time specified by the timer object elapses and the timer object executes the commands you specify.’ With the help of this timer we’ll create a digital clock, which will be synchronized with real time.

To use a timer in digital clock, perform these steps:

  1. Create a timer object.

Use the timer function to create a timer object.

  1. Specify which MATLAB commands you want executed when the timer fires and control other aspects of timer object behaviour.

Use timer object properties to specify this information.

  1. Start the timer object.

After creation of the timer object, start it using the Push Button.

4. Use of timer 

Use the object to update time after every sec for clock.

5. Delete the timer object when you are done with it.

After finished using a timer object, delete it from memory.

MATLAB Code: –

function [] = Digital_Clock()
% Demonstrate how to have a running clock in a GUI, and timer use.
% Creates a small little GUI which displays the correct time and is updated
% every minute according to the system clock.
S.fh = figure('units','pixels',...
 'position',[300 300 180 50],...
S.tx = uicontrol('style','text',...
 'position',[35 10 110 30],...
 'foregroundcolor',[.9 .1 .1]);
STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute. 
tmr = timer('Name','Reminder',...
 'Period',60,... % Update the time every 60 seconds.
 'StartDelay',STRT,... % In seconds.
 'TasksToExecute',inf,... % number of times to update
start(tmr); % Start the timer object.
set(S.fh,'deletefcn',{@deleter}) % Kill timer if fig is closed.
 function [] = updater(varargin)
 % timerfcn for the timer. If figure is deleted, so is timer..
 if ~str2double(datestr(now,'MM'))
 X = load('gong'); % At the hour, sound a gong.
 clear X
 delete(S.fh) % Close it all down.
 function [] = deleter(varargin)
 % If figure is deleted, so is timer.

Result: –

pic1 - clock

Aspiring Electronics Engineer. Walchandite, MATLAB Trainee, Aptitude Head(ELESA), Badminton Player.