Anda di halaman 1dari 14

Fisika Komputasi

Pend. Fisika UIN Alauddin Makassar

LATIHAN 8

MERANCANG GUI MENGHITUNG KELILING PERSEGI PANJANG

Matlab menyediakan fasilitas dalam membuat antarmuka (interface) berbasis grafis yaitu
dengan memanfaatkan fasilitas GUIDE (Graphical User Interface and Developmen Enviroment).

Komponen-komponen yang terdapat pada toolbar adalah:

Komponen Keterangan

Select Untuk membatalkan pemilihan terhadap


komponen

Push button Komponen berupa tombol

Radio button Komponen berupa tombol radio

Edit text Komponen yang memungkinkan pemakai


memasukkan nilai dari keyboard

Pop up menu Berguna membuat menu

Toggle Button Tombol yang bersifat on/off

Axes Membuat grafik

Button group Komponen yang berisi sejumlah tombol radio

Slider Tombol penggeser

Check Box Kotak cek

Static text Berguna membuat label

List box Menyajikan sejumlah pilihan

Table Membuat tabel

1
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

Merancang Antarmuka

Data pengaturan komponen

Komponen Property Isi

Static text String Panjang =

Edit text Tag edit_panjang

String kosongkan

Push button Tag pushbutton_hitung

2
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

String Hitung

Static text String Lebar =

Edit text String kosongkan

Tag edit_lebar

Push button Tag pushbutton_keluar

String Keluar

Static text String Keliling =

Tag text_keliling

Simpan file Anda dengan nama file : keliling

Tambahkan beberapa kode pada program tersebut

function varargout = keliling(varargin)


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

% Last Modified by GUIDE v2.5 05-Jun-2018 13:39:36

3
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @keliling_OpeningFcn, ...
'gui_OutputFcn', @keliling_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 keliling is made visible.


function keliling_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 keliling (see VARARGIN)

% Choose default command line output for keliling


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes keliling wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = keliling_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 edit_panjang_Callback(hObject, eventdata, handles)


% hObject handle to edit_panjang (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

4
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% Hints: get(hObject,'String') returns contents of edit_panjang as text


% str2double(get(hObject,'String')) returns contents of edit_panjang
as a double

% --- Executes during object creation, after setting all properties.


function edit_panjang_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_panjang (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 edit_lebar_Callback(hObject, eventdata, handles)


% hObject handle to edit_lebar (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 edit_lebar as text


% str2double(get(hObject,'String')) returns contents of edit_lebar as
a double

% --- Executes during object creation, after setting all properties.


function edit_lebar_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_lebar (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 edit3_Callback(hObject, eventdata, handles)


% hObject handle to edit3 (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 edit3 as text


% str2double(get(hObject,'String')) returns contents of edit3 as a
double

5
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% --- Executes during object creation, after setting all properties.


function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 pushbutton_hitung.


function pushbutton_hitung_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_hitung (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Periksa panjang dan lebar berupa bilangan atau bukan
panjang=str2num(get(handles.edit_panjang,'String'));
if isempty(panjang)
set(handles.text_keliling,'String','Panjang harus berupa bilangan');
return;
end
lebar=str2num(get(handles.edit_lebar,'String'));
if isempty(lebar)
set(handles.text_keliling,'String','Lebar harus berupa bilangan');
return;
end
% Lakukan perhitungan dan tampilkan hasilnya
keliling=2*(panjang+lebar);
hasil=sprintf('Keliling=%d',keliling);
set(handles.text_keliling,'String',hasil);

% --- Executes on button press in pushbutton_keluar.


function pushbutton_keluar_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_keluar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;

Ujilah program Anda dengan meng klik tombol

6
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

LATIHAN 9

MEMBUAT GRAFIK FUNGSI KUADRAT

Simpan file Anda dengan nama file : axes

Tambahkan beberapa kode pada program tersebut

function varargout = axes(varargin)


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

7
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% Edit the above text to modify the response to help axes

% Last Modified by GUIDE v2.5 05-Jun-2018 14:38:05

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @axes_OpeningFcn, ...
'gui_OutputFcn', @axes_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 axes is made visible.


function axes_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 axes (see VARARGIN)

% Choose default command line output for axes


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes axes wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = axes_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;

8
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

function edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (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 edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a
double

% --- Executes during object creation, after setting all properties.


function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)


% hObject handle to edit2 (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 edit2 as text


% str2double(get(hObject,'String')) returns contents of edit2 as a
double

% --- Executes during object creation, after setting all properties.


function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

9
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% handles structure with handles and user data (see GUIDATA)


min=str2double(get(handles.edit1,'string'));
max=str2double(get(handles.edit2,'string'));
x=min:0.1:max;
y=x.^2;
axes(handles.axes1);
plot(x,y);

Ujilah program Anda dengan meng klik tombol

LATIHAN 10

MENGHITUNG LUAS DAN KELILING LINGKARAN

Data pengaturan komponen

Komponen String Property Tag Property

Push button 1 Luas btn_luas

Push button 2 Keliling btn_kel

Push button 3 Exit btn_exit

10
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

Simpan file Anda dengan nama file : lingkaran

Tambahkan beberapa kode pada program tersebut

function varargout = lingkaran(varargin)


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

% Last Modified by GUIDE v2.5 05-Jun-2018 15:00:31

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @lingkaran_OpeningFcn, ...
'gui_OutputFcn', @lingkaran_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

11
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

% --- Executes just before lingkaran is made visible.


function lingkaran_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 lingkaran (see VARARGIN)

% Choose default command line output for lingkaran


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes lingkaran wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = lingkaran_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 edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (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 edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a
double
%menginputkan jari-jari dengan menggunakan
str2num(get(hObject,'String'))
jari=str2num(get(handles.edit1,'String'));
%update jari ke struktur handles
handles.jari=jari;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.


function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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.

12
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

if ispc && isequal(get(hObject,'BackgroundColor'),


get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit2_Callback(hObject, eventdata, handles)


% hObject handle to edit2 (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 edit2 as text


% str2double(get(hObject,'String')) returns contents of edit2 as a
double

% --- Executes during object creation, after setting all properties.


function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 btn_luas.


function btn_luas_Callback(hObject, eventdata, handles)
% hObject handle to btn_luas (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
jari=handles.jari;
luas=pi*jari^2;
set(handles.edit2,'string',luas);
% --- Executes on button press in btn_kel.
function btn_kel_Callback(hObject, eventdata, handles)
% hObject handle to btn_kel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
jari=handles.jari;
kel=2*pi*jari;
set(handles.edit2,'string',kel);
% --- Executes on button press in btn_exit.
function btn_exit_Callback(hObject, eventdata, handles)
% hObject handle to btn_exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
delete(handles.figure1);

13
Fisika Komputasi
Pend. Fisika UIN Alauddin Makassar

14

Anda mungkin juga menyukai