MUHAMMAD AHMED
190319
BEEE-4B
EXPERIMENT # 08
Introduction to MATLAB GUI
Objective:
Familiarizing students with MATLAB guide (GUI Development Environment)
Lab Task:
1. Design a simple calculator using MATLAB GUI that can add, subtract, multiply
and divide two numbers and displays the result.
Code:
Here, I have paste the whole code because code is too long to be pasted as snaps here, so
I copy paste it here from matlab.
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input
arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises
the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 04-May-2021 13:39:53
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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;
function SCREEN_Callback(hObject, eventdata, handles)
% hObject handle to SCREEN (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 SCREEN as text
% str2double(get(hObject,'String')) returns contents of SCREEN as a
double
% --- Executes during object creation, after setting all properties.
function SCREEN_CreateFcn(hObject, eventdata, handles)
% hObject handle to SCREEN (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
% --- Executes on button press in NUMBER_7.
function NUMBER_7_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'7']);
% --- Executes on button press in NUMBER_8.
function NUMBER_8_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'8']);
% --- Executes on button press in NUMBER_4.
function NUMBER_4_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'4']);
% --- Executes on button press in NUMBER_5.
function NUMBER_5_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'5']);
% --- Executes on button press in NUMBER_9.
function NUMBER_9_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'9']);
% --- Executes on button press in NUMBER_6.
function NUMBER_6_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'6']);
% --- Executes on button press in NUMBER_1.
function NUMBER_1_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'1']);
% --- Executes on button press in NUMBER_2.
function NUMBER_2_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'2']);
% --- Executes on button press in NUMBER_3.
function NUMBER_3_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'3']);
% --- Executes on button press in NUMBER_0.
function NUMBER_0_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'0']);
% --- Executes on button press in EQUAL.
function EQUAL_Callback(hObject, eventdata, handles)
% hObject handle to EQUAL (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
set(handles.SCREEN,'string',
[get(handles.SCREEN,'string'),'=',num2str(eval(get(handles.SCREEN,'string')
))]);
catch
set(handles.SCREEN,'string','ERROR');
end
% --- Executes on button press in NUMBER_POINT.
function NUMBER_POINT_Callback(hObject, eventdata, handles)
% hObject handle to NUMBER_POINT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'.']);
% --- Executes on button press in DIVIDE.
function DIVIDE_Callback(hObject, eventdata, handles)
% hObject handle to DIVIDE (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'/']);
% --- Executes on button press in MULTIPLY.
function MULTIPLY_Callback(hObject, eventdata, handles)
% hObject handle to MULTIPLY (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'*']);
% --- Executes on button press in SUBTRACT.
function SUBTRACT_Callback(hObject, eventdata, handles)
% hObject handle to SUBTRACT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'-']);
% --- Executes on button press in ADD.
function ADD_Callback(hObject, eventdata, handles)
% hObject handle to ADD (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'+']);
% --- Executes on button press in pushbuttonclear.
function pushbuttonclear_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonclear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',' ');
% --- Executes on button press in pushbuttonBS.
function pushbuttonBS_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonBS (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Output:
2. Design a trigonometric calculator using MATLAB GUI that can take
• Natural log
• Factorial
• x10
• exp
• square root
• square
• sin
• cos
• tan
of any input number.
Code:
Here, I have paste the whole code because code is too long to be pasted as snaps here, so
I copy paste it here from matlab.
function varargout = TASK_2(varargin)
% TASK_2 MATLAB code for TASK_2.fig
% TASK_2, by itself, creates a new TASK_2 or raises the existing
% singleton*.
%
% H = TASK_2 returns the handle to a new TASK_2 or the handle to
% the existing singleton*.
%
% TASK_2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TASK_2.M with the given input arguments.
%
% TASK_2('Property','Value',...) creates a new TASK_2 or raises the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before TASK_2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to TASK_2_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 TASK_2
% Last Modified by GUIDE v2.5 05-May-2021 23:44:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @TASK_2_OpeningFcn, ...
'gui_OutputFcn', @TASK_2_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 TASK_2 is made visible.
function TASK_2_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 TASK_2 (see VARARGIN)
% Choose default command line output for TASK_2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes TASK_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = TASK_2_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;
function SCREEN_Callback(hObject, eventdata, handles)
% hObject handle to SCREEN (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 SCREEN as text
% str2double(get(hObject,'String')) returns contents of SCREEN as a
double
% --- Executes during object creation, after setting all properties.
function SCREEN_CreateFcn(hObject, eventdata, handles)
% hObject handle to SCREEN (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
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'7']);
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'8']);
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'9']);
% --- Executes on button press in pushbutton_NaturalLog.
function pushbutton_NaturalLog_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_NaturalLog (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',
['ln(',get(handles.SCREEN,'string'),')=',num2str(log(str2double(get(handles
.SCREEN,'string'))))]);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'4']);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'5']);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'6']);
% --- Executes on button press in pushbutton_Factorial.
function pushbutton_Factorial_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_Factorial (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'!
=',num2str(factorial(str2double(get(handles.SCREEN,'string'))))]);
% --- 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)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'1']);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'2']);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'3']);
% --- Executes on button press in pushbutton_Square.
function pushbutton_Square_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_Square (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'^2']);
% --- Executes on button press in pushbuttonDOT.
function pushbuttonDOT_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonDOT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'.']);
% --- Executes on button press in pushbutton0.
function pushbutton0_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'0']);
% --- Executes on button press in pushbutton_exp.
function pushbutton_exp_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_exp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',
['exp(',get(handles.SCREEN,'string'),')=',num2str(exp(str2double(get(handle
s.SCREEN,'string'))))]);
% --- Executes on button press in pushbutton_SquareRoot.
function pushbutton_SquareRoot_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_SquareRoot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',['?
(',get(handles.SCREEN,'string'),')=',num2str(sqrt(str2double(get(handles.SC
REEN,'string'))))]);
% --- Executes on button press in pushbutton_DIVIDE.
function pushbutton_DIVIDE_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_DIVIDE (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'/']);
% --- Executes on button press in pushbutton_MULTIPLY.
function pushbutton_MULTIPLY_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_MULTIPLY (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'*']);
% --- Executes on button press in pushbutton_SUBTRACT.
function pushbutton_SUBTRACT_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_SUBTRACT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'-']);
% --- Executes on button press in pushbutton_ADD.
function pushbutton_ADD_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_ADD (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'+']);
% --- Executes on button press in pushbutton_Sine.
function pushbutton_Sine_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_Sine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',
['sin(',get(handles.SCREEN,'string'),')=',num2str(sin(str2double(get(handle
s.SCREEN,'string'))))]);
% --- Executes on button press in pushbutton_cosine.
function pushbutton_cosine_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_cosine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',
['cos(',get(handles.SCREEN,'string'),')=',num2str(cos(str2double(get(handle
s.SCREEN,'string'))))]);
% --- Executes on button press in pushbutton_tangent.
function pushbutton_tangent_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_tangent (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',
['tan(',get(handles.SCREEN,'string'),')=',num2str(tan(str2double(get(handle
s.SCREEN,'string'))))]);
% --- Executes on button press in pushbutton_x10.
function pushbutton_x10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_x10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',[get(handles.SCREEN,'string'),'*10^']);
% --- Executes on button press in pushbutton_EQUAL.
function pushbutton_EQUAL_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_EQUAL (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
set(handles.SCREEN,'string',
[get(handles.SCREEN,'string'),'=',num2str(eval(get(handles.SCREEN,'string')
))]);
catch
set(handles.SCREEN,'string','ERROR');
end
% --- Executes on button press in pushbuttonClear.
function pushbuttonClear_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonClear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.SCREEN,'string',' ');
Output:
• NATURAL LOG
• x10
• SQUARE ROOT
• SQUARE
• FACTORIAL
• Exp
• SINE
• COSINE
• TANGENT
Conclusion:
In this lab we learned about Matlab guide(GUI development environment) and
also learned about how to design calculator on matlab.