Timer Implementation using GUI | MATLAB Tutorial

The MATLAB software includes a timer object that you can use to schedule the execution of MATLAB commands. We’ll see how to create an objects, start a timer running, and specify the processing that are performed when it fires. ‘A timer is said to fire when the amount of time specified by an object elapses and the object executes the commands you specify.’

To use a timer, perform these steps:

  1. Create a timer object.

Use the timer function to create an object.

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

Use timer object properties to specify this information.

  1. Start the object.

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

  1. Delete the object, when you are done with it.

After finished using the object,  delete it from memory.

This is the User Interface for Function created in GUIDE.

MATLAB Code:

function varargout = Timer(varargin)
% MATLAB code for Timer.fig
% It creates a new TIMER or raises the existing
% singleton*.
%
% H = TIMER returns the handle to a new function or the handle to
% the existing singleton*.
%
% TIMER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in '*function.M' with the given input arguments.
%
% TIMER('Property','Value',...) creates a new function or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Timer_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Timer_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
 
% Edit the above text to modify the response to help file
 
% Last Modified by GUIDE v2.5 17-Jul-2017 13:45:07
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
 'gui_Singleton', gui_Singleton, ...
 'gui_OpeningFcn', @Timer_OpeningFcn, ...
 'gui_OutputFcn', @Timer_OutputFcn, ...
 'gui_LayoutFcn', [] , ...
 'gui_Callback', []);
if nargin && ischar(varargin{1})
 gui_State.gui_Callback = str2func(varargin{1});
end
 
if nargout
 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
 gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
 
% --- Executes just before function file is made visible.
function Timer_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Function (see VARARGIN)
 
% Choose default command line output for function
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes it wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
% --- Outputs from this function are returned to the command line.
function varargout = Timer_OutputFcn(hObject, eventdata, handles) 
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
 
% Get default command line output from handles structure
varargout{1} = handles.output; 
 
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
 
h1 = get(handles.edit2,'string');
n = str2num(h1);
 
t = timer('TimerFcn', 'state=false; disp(''Timer!'')', 'StartDelay',n);
start(t)
 
state=true;
if(state==true)
 for i=1:n-1;
 fprintf('%d\n',i);
 pause(1)
 end
 if(i>n)
 state = false;
 end
end
 
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
 
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
 
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
 
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
 set(hObject,'BackgroundColor','white');
end

Timer Example:

RESULT:

OUTPUT:

>> Timer
1
2
3
4
5
6
7
8
9
10
11
Timer!


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

Comments

  1. […] MATLAB software includes a timer object that can be used to schedule the execution of MATLAB commands. We’ll see how to create […]

Comments are closed.