Wednesday, March 25, 2020

Mat lab - Tutorial 1

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

Mat Lab - Tutorial 2

 Simple add, sub, Mul, Div, calculator functions using Mat lab 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 simple Matlab GUI to create the simple Arithmetic calculator




Matlab Code:

function varargout = add_sub_fun(varargin)
% ADD_SUB_FUN MATLAB code for add_sub_fun.fig
%      ADD_SUB_FUN, by itself, creates a new ADD_SUB_FUN or raises the existing
%      singleton*.
%
%      H = ADD_SUB_FUN returns the handle to a new ADD_SUB_FUN or the handle to
%      the existing singleton*.
%
%      ADD_SUB_FUN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ADD_SUB_FUN.M with the given input arguments.
%
%      ADD_SUB_FUN('Property','Value',...) creates a new ADD_SUB_FUN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before add_sub_fun_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to add_sub_fun_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 add_sub_fun

% Last Modified by GUIDE v2.5 21-Dec-2019 12:59:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @add_sub_fun_OpeningFcn, ...
                   'gui_OutputFcn',  @add_sub_fun_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 add_sub_fun is made visible.
function add_sub_fun_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 add_sub_fun (see VARARGIN)

% Choose default command line output for add_sub_fun
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes add_sub_fun wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = add_sub_fun_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 X_Callback(hObject, eventdata, handles)
% hObject    handle to X (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 X as text
%        str2double(get(hObject,'String')) returns contents of X as a double


% --- Executes during object creation, after setting all properties.
function X_CreateFcn(hObject, eventdata, handles)
% hObject    handle to X (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



function Y_Callback(hObject, eventdata, handles)
% hObject    handle to Y (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 Y as text
%        str2double(get(hObject,'String')) returns contents of Y as a double


% --- Executes during object creation, after setting all properties.
function Y_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Y (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 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)
a=str2num(get(handles.X,'string'));
b=str2num(get(handles.Y,'string'));
c=a+b;
set(handles.Z,'string',c);




% --- Executes on button press in SUBB.
function SUBB_Callback(hObject, eventdata, handles)
% hObject    handle to SUBB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=str2num(get(handles.X,'string'));
b=str2num(get(handles.Y,'string'));
c=a-b;
set(handles.Z,'string',c);


% --- Executes on button press in MUL.
function MUL_Callback(hObject, eventdata, handles)
% hObject    handle to MUL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=str2num(get(handles.X,'string'));
b=str2num(get(handles.Y,'string'));
c=a*b;
set(handles.Z,'string',c);


% --- Executes on button press in DIV.
function DIV_Callback(hObject, eventdata, handles)
% hObject    handle to DIV (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=str2num(get(handles.X,'string'));
b=str2num(get(handles.Y,'string'));
c=a/b;
set(handles.Z,'string',c);


Video File:
https://youtu.be/r0rJaZjhAQQ?list=PLpqFpcNEVirH1VJd7PDXgmwi8jYA_2tmb