Fun Examples with GUI | MATLAB Tutorial

MATLAB Helper Quiz

Learn examples with GUI building using guide in MATLAB. Learn about Pointer Manipulation and Calculator in MATLAB using GUI.

Examples:-

Calculator using GUI

User Interface:

Calc1-examples

MATLAB Code:

function varargout = Calc(varargin)
%%Examples - 1
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Calc_OpeningFcn, ...
'gui_OutputFcn', @Calc_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

% --- Executes just before Calc is made visible.
function Calc_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 Calc (see VARARGIN)
% Examples
% Choose default command line output for Calc
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = Calc_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)
str=get(handles.text1,'String');
str=strcat(str,'1');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton17 (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.text1,'String');
str=strcat(str,'4');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton18 (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.text1,'String');
str=strcat(str,'/');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton19 (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.text1,'String');
str=strcat(str,'3');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton20 (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.text1,'String');
str=strcat(str,'5');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton21 (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.text1,'String');
str=strcat(str,'.');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton22 (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.text1,'String');
str=eval(str);
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton23 (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.text1,'String');
str=strcat(str,'7');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton24 (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.text1,'String');
str=strcat(str,'6');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton25 (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.text1,'String');
str=strcat(str,'8');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton26 (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.text1,'String');
str=strcat(str,'*');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton27 (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.text1,'String');
str=strcat(str,'-');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton28.
function pushbutton28_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton28 (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.text1,'String');
str=strcat(str,'0');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton29 (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.text1,'String');
str=strcat(str,'2');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton30 (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.text1,'String');
str=strcat(str,'9');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton31 (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.text1,'String');
str=strcat(str,'+');
set(handles.text1,'String',str);

% --- Executes on button press in pushbutton33.
function pushbutton33_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton33 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.text1,'String','');

% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton34 (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.text1,'String');
s=str2double(str);
sqr=sqrt(s); %For sqrt.
t=num2str(sqr);
set(handles.text1,'String',t);

% --- Executes on button press in pushbutton35.
function pushbutton35_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton35 (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.text1,'String');
s=str2double(str);
e=-(s); %For +/-.
t=num2str(e);
set(handles.text1,'String',t);

% --- Executes on button press in pushbutton36.
function pushbutton36_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton36 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% Examples
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
percent=s/100; %For percentage
t=num2str(percent);
set(handles.text1,'String',t);

% --- Executes on button press in pushbutton37.
function pushbutton37_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton37 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% Examples
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(1/s); %For reciprocal
t=num2str(div);
set(handles.text1,'String',t);

Result:

gif1

gif2-examplesManipulation of Mouse Pointer using program in GUI.

User Interface:

ptr1-examples

MATLAB Code:

function [] = Mouse_Pointer()
%% Examples - 2
S.fh = figure('units','pixels',...
'position',[300 300 300 160],...
'menubar','none',...
'name','Mouse_Pointer',...
'numbertitle','off',...
'resize','off');
S.UN = get(0,'units');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[20 30 260 100],...
'string', 'Send Me to a different Location',...
'fontsize',12,'fontweight','bold',...
'callback',{@pb_call,S});
set(S.fh,'WindowButtonMotionFcn',{@fh_wbmf,S})


function [] = fh_wbmf(varargin)
S = varargin{3}; % Get the structure.
set(0,'units','normalized')
% Move the mouse pointer to a random position.
% Examples
set(0, 'PointerLocation', [rand rand])
set(0,'units',S.UN)

Leave a Comment


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