Simple Calculator Development Using Matlab GUI
Graphical user interfaces (GUIs), also known as apps, provide point-and-click control of your software applications, eliminating the need for others to learn a language or type commands in order to run the application. You can share apps both for use within MATLAB and also as standalone desktop or web apps.
Here we use the same GUI app to create the same Calculator with some additional function such as square root, percentage,etc..
Mat Lab Code:
function varargout = Simple_Calculator(varargin)
% SIMPLE_CALCULATOR MATLAB code for Simple_Calculator.fig
% SIMPLE_CALCULATOR, by itself, creates a new SIMPLE_CALCULATOR or raises the existing
% singleton*.
%
% H = SIMPLE_CALCULATOR returns the handle to a new SIMPLE_CALCULATOR or the handle to
% the existing singleton*.
%
% SIMPLE_CALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SIMPLE_CALCULATOR.M with the given input arguments.
%
% SIMPLE_CALCULATOR('Property','Value',...) creates a new SIMPLE_CALCULATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Simple_Calculator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Simple_Calculator_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 Simple_Calculator
% Last Modified by GUIDE v2.5 21-Dec-2019 22:53:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Simple_Calculator_OpeningFcn, ...
'gui_OutputFcn', @Simple_Calculator_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 Simple_Calculator is made visible.
function Simple_Calculator_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 Simple_Calculator (see VARARGIN)
% Choose default command line output for Simple_Calculator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Simple_Calculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Simple_Calculator_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 RB.
function RB_Callback(hObject, eventdata, handles)
% hObject handle to RB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,')');
set(handles.inputs,'String',str);
% --- Executes on button press in Inverse.
function Inverse_Callback(hObject, eventdata, handles)
% hObject handle to Inverse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
s=str2double(str);
div=(1/s);
t=num2str(div);
set(handles.outputs,'String',t);
% --- Executes on button press in Square_Root.
function Square_Root_Callback(hObject, eventdata, handles)
% hObject handle to Square_Root (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
s=str2double(str);
sqr=sqrt(s);
t=num2str(sqr);
set(handles.outputs,'String',t);
% --- Executes on button press in LB.
function LB_Callback(hObject, eventdata, handles)
% hObject handle to LB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'(');
set(handles.inputs,'String',str);
% --- Executes on button press in Percentage.
function Percentage_Callback(hObject, eventdata, handles)
% hObject handle to Percentage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.outputs,'String');
s=str2double(str);
per=s*100;
t=num2str(per);
set(handles.outputs,'String',t);
% --- Executes on button press in Sign_Change.
function Sign_Change_Callback(hObject, eventdata, handles)
% hObject handle to Sign_Change (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.outputs,'String');
s=str2double(str);
e=-(s);
t=num2str(e);
set(handles.inputs,'String',t);
% --- 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)
str=get(handles.inputs,'String');
str=eval(str);
set(handles.outputs,'String',str);
% --- Executes on button press in Answer.
function Answer_Callback(hObject, eventdata, handles)
% hObject handle to Answer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=get(handles.inputs,'String');
Ans=get(handles.outputs,'String');
set(handles.inputs,'String',strcat(a,Ans));
% --- Executes on button press in All_Clear.
function All_Clear_Callback(hObject, eventdata, handles)
% hObject handle to All_Clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.inputs,'string','');
set(handles.outputs,'string','');
% --- Executes on button press in Delete.
function Delete_Callback(hObject, eventdata, handles)
% hObject handle to Delete (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=str(1:end-1);
set(handles.inputs,'String',str);
% --- Executes on button press in Division.
function Division_Callback(hObject, eventdata, handles)
% hObject handle to Division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'/');
set(handles.inputs,'String',str);
% --- 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)
str=get(handles.inputs,'String');
str=strcat(str,'*');
set(handles.inputs,'String',str);
% --- Executes on button press in Minus.
function Minus_Callback(hObject, eventdata, handles)
% hObject handle to Minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'-');
set(handles.inputs,'String',str);
% --- Executes on button press in Plus.
function Plus_Callback(hObject, eventdata, handles)
% hObject handle to Plus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'+');
set(handles.inputs,'String',str);
% --- Executes on button press in Exponentional.
function Exponentional_Callback(hObject, eventdata, handles)
% hObject handle to Exponentional (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
E='*10^';
str=strcat(str,'E');
set(handles.inputs,'String',str);
% --- Executes on button press in Dot.
function Dot_Callback(hObject, eventdata, handles)
% hObject handle to Dot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'.');
set(handles.inputs,'String',str);
% --- Executes on button press in Zero.
function Zero_Callback(hObject, eventdata, handles)
% hObject handle to Zero (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'0');
set(handles.inputs,'String',str);
% --- Executes on button press in Three.
function Three_Callback(hObject, eventdata, handles)
% hObject handle to Three (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'3');
set(handles.inputs,'String',str);
% --- Executes on button press in Two.
function Two_Callback(hObject, eventdata, handles)
% hObject handle to Two (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'2');
set(handles.inputs,'String',str);
% --- Executes on button press in One.
function One_Callback(hObject, eventdata, handles)
% hObject handle to One (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'1');
set(handles.inputs,'String',str);
% --- Executes on button press in Six.
function Six_Callback(hObject, eventdata, handles)
% hObject handle to Six (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'6');
set(handles.inputs,'String',str);
% --- Executes on button press in Five.
function Five_Callback(hObject, eventdata, handles)
% hObject handle to Five (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'5');
set(handles.inputs,'String',str);
% --- Executes on button press in Four.
function Four_Callback(hObject, eventdata, handles)
% hObject handle to Four (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'4');
set(handles.inputs,'String',str);
% --- Executes on button press in Nine.
function Nine_Callback(hObject, eventdata, handles)
% hObject handle to Nine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'9');
set(handles.inputs,'String',str);
% --- Executes on button press in Eight.
function Eight_Callback(hObject, eventdata, handles)
% hObject handle to Eight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'8');
set(handles.inputs,'String',str);
% --- Executes on button press in Seven.
function Seven_Callback(hObject, eventdata, handles)
% hObject handle to Seven (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'7');
set(handles.inputs,'String',str);
Video File:
https://youtu.be/NWFvIjcrFUs?list=PLpqFpcNEVirH1VJd7PDXgmwi8jYA_2tmb
Here we use the same GUI app to create the same Calculator with some additional function such as square root, percentage,etc..
Mat Lab Code:
function varargout = Simple_Calculator(varargin)
% SIMPLE_CALCULATOR MATLAB code for Simple_Calculator.fig
% SIMPLE_CALCULATOR, by itself, creates a new SIMPLE_CALCULATOR or raises the existing
% singleton*.
%
% H = SIMPLE_CALCULATOR returns the handle to a new SIMPLE_CALCULATOR or the handle to
% the existing singleton*.
%
% SIMPLE_CALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SIMPLE_CALCULATOR.M with the given input arguments.
%
% SIMPLE_CALCULATOR('Property','Value',...) creates a new SIMPLE_CALCULATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Simple_Calculator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Simple_Calculator_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 Simple_Calculator
% Last Modified by GUIDE v2.5 21-Dec-2019 22:53:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Simple_Calculator_OpeningFcn, ...
'gui_OutputFcn', @Simple_Calculator_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 Simple_Calculator is made visible.
function Simple_Calculator_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 Simple_Calculator (see VARARGIN)
% Choose default command line output for Simple_Calculator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Simple_Calculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Simple_Calculator_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 RB.
function RB_Callback(hObject, eventdata, handles)
% hObject handle to RB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,')');
set(handles.inputs,'String',str);
% --- Executes on button press in Inverse.
function Inverse_Callback(hObject, eventdata, handles)
% hObject handle to Inverse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
s=str2double(str);
div=(1/s);
t=num2str(div);
set(handles.outputs,'String',t);
% --- Executes on button press in Square_Root.
function Square_Root_Callback(hObject, eventdata, handles)
% hObject handle to Square_Root (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
s=str2double(str);
sqr=sqrt(s);
t=num2str(sqr);
set(handles.outputs,'String',t);
% --- Executes on button press in LB.
function LB_Callback(hObject, eventdata, handles)
% hObject handle to LB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'(');
set(handles.inputs,'String',str);
% --- Executes on button press in Percentage.
function Percentage_Callback(hObject, eventdata, handles)
% hObject handle to Percentage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.outputs,'String');
s=str2double(str);
per=s*100;
t=num2str(per);
set(handles.outputs,'String',t);
% --- Executes on button press in Sign_Change.
function Sign_Change_Callback(hObject, eventdata, handles)
% hObject handle to Sign_Change (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.outputs,'String');
s=str2double(str);
e=-(s);
t=num2str(e);
set(handles.inputs,'String',t);
% --- 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)
str=get(handles.inputs,'String');
str=eval(str);
set(handles.outputs,'String',str);
% --- Executes on button press in Answer.
function Answer_Callback(hObject, eventdata, handles)
% hObject handle to Answer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=get(handles.inputs,'String');
Ans=get(handles.outputs,'String');
set(handles.inputs,'String',strcat(a,Ans));
% --- Executes on button press in All_Clear.
function All_Clear_Callback(hObject, eventdata, handles)
% hObject handle to All_Clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.inputs,'string','');
set(handles.outputs,'string','');
% --- Executes on button press in Delete.
function Delete_Callback(hObject, eventdata, handles)
% hObject handle to Delete (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=str(1:end-1);
set(handles.inputs,'String',str);
% --- Executes on button press in Division.
function Division_Callback(hObject, eventdata, handles)
% hObject handle to Division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'/');
set(handles.inputs,'String',str);
% --- 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)
str=get(handles.inputs,'String');
str=strcat(str,'*');
set(handles.inputs,'String',str);
% --- Executes on button press in Minus.
function Minus_Callback(hObject, eventdata, handles)
% hObject handle to Minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'-');
set(handles.inputs,'String',str);
% --- Executes on button press in Plus.
function Plus_Callback(hObject, eventdata, handles)
% hObject handle to Plus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'+');
set(handles.inputs,'String',str);
% --- Executes on button press in Exponentional.
function Exponentional_Callback(hObject, eventdata, handles)
% hObject handle to Exponentional (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
E='*10^';
str=strcat(str,'E');
set(handles.inputs,'String',str);
% --- Executes on button press in Dot.
function Dot_Callback(hObject, eventdata, handles)
% hObject handle to Dot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'.');
set(handles.inputs,'String',str);
% --- Executes on button press in Zero.
function Zero_Callback(hObject, eventdata, handles)
% hObject handle to Zero (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'0');
set(handles.inputs,'String',str);
% --- Executes on button press in Three.
function Three_Callback(hObject, eventdata, handles)
% hObject handle to Three (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'3');
set(handles.inputs,'String',str);
% --- Executes on button press in Two.
function Two_Callback(hObject, eventdata, handles)
% hObject handle to Two (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'2');
set(handles.inputs,'String',str);
% --- Executes on button press in One.
function One_Callback(hObject, eventdata, handles)
% hObject handle to One (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'1');
set(handles.inputs,'String',str);
% --- Executes on button press in Six.
function Six_Callback(hObject, eventdata, handles)
% hObject handle to Six (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'6');
set(handles.inputs,'String',str);
% --- Executes on button press in Five.
function Five_Callback(hObject, eventdata, handles)
% hObject handle to Five (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'5');
set(handles.inputs,'String',str);
% --- Executes on button press in Four.
function Four_Callback(hObject, eventdata, handles)
% hObject handle to Four (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'4');
set(handles.inputs,'String',str);
% --- Executes on button press in Nine.
function Nine_Callback(hObject, eventdata, handles)
% hObject handle to Nine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'9');
set(handles.inputs,'String',str);
% --- Executes on button press in Eight.
function Eight_Callback(hObject, eventdata, handles)
% hObject handle to Eight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'8');
set(handles.inputs,'String',str);
% --- Executes on button press in Seven.
function Seven_Callback(hObject, eventdata, handles)
% hObject handle to Seven (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputs,'String');
str=strcat(str,'7');
set(handles.inputs,'String',str);
Video File: