Wednesday, March 25, 2020

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

No comments:

Post a Comment