Membuat GUI di MATLAB


Matlab menyediakan fasilitas untuk membuat suatu program yang dibuat agar tampilannya lebih menarik. Sebagai contoh adalah pembuatan simulasi fisika sederhana. Kita dapat membuat tampilan agar lebih interaktif dan menarik sehingga membantu kita dalam memahami apa yang sedang disimulasikan. Fasilitas ini disebut dengan GUI (Graphical User Interface).  Untuk lebih jelasnya mari kita buat sebuah simulasi sederhana tentang gerak peluru.

Untuk membuat GUI kita dapat mengetik ‘guide’ pada command window matlab

Kemudian akan muncul kotak dialog berikut ini

Kita pilih ‘Blank GUI (Default)’ dan akan muncul gambar berikut ini

Disinilah kita akan meletakkan komponen-komponen yang digunakan untuk membuat program. Sebelum kita membuat program sebaiknya kita mengenal dahulu apa saja komponen yang ada pada GUI tersebut. Berikut ini nama-nama komponen pada GUI di atas

Sekarang mari kita buat suatu tampilan untuk simulasi gerak peluru. Desain tampilan tidak bersifat mutlak, terserah kepada pembuat programnya. Berikut ini tampilan yang saya buat

Kode program yang akan kita buat nanti berhubungan dengan property dari masing-masing komponen yang kita gunakan. Untuk mengubah property klik dua kali pada komponen maka akan muncul tampilan berikut ini

Bagian yang dilingkari adalah bagian yang penting dalam penulisan kode program. Untuk itu kita langsung saja praktekkan. Pada tampilan yang saya buat klik kanan pada tombol hitung, kemudian pilih view callbacks, dan pilih callback. Maka akan tampak tampilan berikut ini

Selanjutnya ketikkan kode ini di bawah komen (komen ditandai dengan %) terakhir dari kode yang di highlight biru

————————————————————————————————————————————————————–

proyek=guidata(gcbo);

v0=str2double(get(proyek.kecepatan,’String’));

sudut=str2double(get(proyek.sudut,’String’));

waktu=str2double(get(proyek.waktu,’String’));

gravitasi=str2double(get(proyek.gravitasi,’String’));

v0x=v0*cos(sudut*pi/180);

v0y=v0*sin(sudut*pi/180);

vy=v0y-gravitasi*waktu;

tx=2*v0*sin(sudut*pi/180)/gravitasi;

if (waktu>tx)

msgbox(‘waktu melebihi maksimal,inputkan nilai waktu yang lain’);

else

v=sqrt(v0x^2+vy^2);

set(proyek.velocity,’String’,num2str(v));

x=v0x*waktu;

y=v0y*waktu-0.5*gravitasi*(waktu^2);

tinggi=v0^2*((sin(sudut*pi/180))^2)/(2*gravitasi);

jauh=v0^2*sin(2*sudut*pi/180)/gravitasi;

set (proyek.py,’String’,num2str(y));

set (proyek.px,’String’,num2str(x));

set(proyek.ymax,’String’,num2str(tinggi));

set(proyek.xmax,’String’,num2str(jauh));

xx=0:x;

yy=xx*(tan(sudut*pi/180))-0.5*gravitasi*(xx.^2)/(v0*cos(sudut*pi/180))^2;

for i=1:x

axes(handles.grafik);

plot(xx(i),yy(i),’*’,’linewidth’,2);

axis([0 (jauh+5) 0 (tinggi+5)]);

grid on

pause(0.01);

end

axes(handles.grafik);

plot(xx,yy,’*’,’linewidth’,2);

axis([0 (jauh+5) 0 (tinggi+5)]);

grid on

end

—————————————————————————————————————————————————————

Untuk nama variabel tidak mutlak, sehingga terserah pembaca jika membuat program. Kita perhatikan cuplikan kode berikut

v0=str2double(get(proyek.kecepatan,’String’));

tampak bahwa terdapat tulisan ‘kecepatan’ setelah ‘proyek’. ‘kecepatan’ disini adalah tag yang kita tuliskan pada property inspector yang telah dijelaskan tadi. Kode

for i=1:x

axes(handles.grafik);

plot(xx(i),yy(i),’*’,’linewidth’,2);

axis([0 (jauh+5) 0 (tinggi+5)]);

grid on

pause(0.01);

end

untuk menghasilkan animasi pada grafik yang kita buat.

program keseluruhan seperti berikut ini

—————————————————————————————————————————-

function varargout = peluru(varargin)

% PELURU M-file for peluru.fig

%      PELURU, by itself, creates a new PELURU or raises the existing

%      singleton*.

%

%      H = PELURU returns the handle to a new PELURU or the handle to

%      the existing singleton*.

%

%      PELURU(‘CALLBACK’,hObject,eventData,handles,…) calls the local

%      function named CALLBACK in PELURU.M with the given input arguments.

%

%      PELURU(‘Property’,’Value’,…) creates a new PELURU or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before peluru_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to peluru_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 peluru

 

% Last Modified by GUIDE v2.5 09-May-2012 09:49:00

 

% Begin initialization code – DO NOT EDIT

gui_Singleton = 1;

gui_State = struct(‘gui_Name’,       mfilename, …

                   ‘gui_Singleton’,  gui_Singleton, …

                   ‘gui_OpeningFcn’, @peluru_OpeningFcn, …

                   ‘gui_OutputFcn’,  @peluru_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 peluru is made visible.

function peluru_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 peluru (see VARARGIN)

 

% Choose default command line output for peluru

handles.output = hObject;

 

% Update handles structure

guidata(hObject, handles);

 

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

% uiwait(handles.figure1);

 

 

% — Outputs from this function are returned to the command line.

function varargout = peluru_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 kecepatan_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of kecepatan as a double

 

 

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

function kecepatan_CreateFcn(hObject, eventdata, handles)

% hObject    handle to kecepatan (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 sudut_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of sudut as a double

 

 

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

function sudut_CreateFcn(hObject, eventdata, handles)

% hObject    handle to sudut (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 waktu_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of waktu as a double

 

 

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

function waktu_CreateFcn(hObject, eventdata, handles)

% hObject    handle to waktu (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 gravitasi_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of gravitasi as a double

 

 

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

function gravitasi_CreateFcn(hObject, eventdata, handles)

% hObject    handle to gravitasi (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 velocity_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of velocity as a double

 

 

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

function velocity_CreateFcn(hObject, eventdata, handles)

% hObject    handle to velocity (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 posisi_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of posisi as a double

 

 

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

function posisi_CreateFcn(hObject, eventdata, handles)

% hObject    handle to posisi (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 ymax_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of ymax as a double

 

 

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

function ymax_CreateFcn(hObject, eventdata, handles)

% hObject    handle to ymax (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 xmax_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of xmax as a double

 

 

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

function xmax_CreateFcn(hObject, eventdata, handles)

% hObject    handle to xmax (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 px_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of px as a double

 

 

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

function px_CreateFcn(hObject, eventdata, handles)

% hObject    handle to px (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 py_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of py as a double

 

 

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

function py_CreateFcn(hObject, eventdata, handles)

% hObject    handle to py (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

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

proyek=guidata(gcbo);

v0=str2double(get(proyek.kecepatan,’String’));

sudut=str2double(get(proyek.sudut,’String’));

waktu=str2double(get(proyek.waktu,’String’));

gravitasi=str2double(get(proyek.gravitasi,’String’));

v0x=v0*cos(sudut*pi/180);

v0y=v0*sin(sudut*pi/180);

vy=v0y-gravitasi*waktu;

tx=2*v0*sin(sudut*pi/180)/gravitasi;

if (waktu>tx)

    msgbox(‘waktu melebihi maksimal,inputkan nilai waktu yang lain’);

else

v=sqrt(v0x^2+vy^2);

set(proyek.velocity,’String’,num2str(v));

x=v0x*waktu;

y=v0y*waktu-0.5*gravitasi*(waktu^2);

tinggi=v0^2*((sin(sudut*pi/180))^2)/(2*gravitasi);

jauh=v0^2*sin(2*sudut*pi/180)/gravitasi;

set (proyek.py,’String’,num2str(y));

set (proyek.px,’String’,num2str(x));

set(proyek.ymax,’String’,num2str(tinggi));

set(proyek.xmax,’String’,num2str(jauh));

xx=0:x;

yy=xx*(tan(sudut*pi/180))-0.5*gravitasi*(xx.^2)/(v0*cos(sudut*pi/180))^2;

for i=1:x

    axes(handles.grafik);

    plot(xx(i),yy(i),’p’,’linewidth’,2);

    axis([0 (jauh+5) 0 (tinggi+5)]);

    grid on

    pause(0.01);

end

 

    axes(handles.grafik);

    plot(xx,yy,’p’,’linewidth’,2);

    axis([0 (jauh+5) 0 (tinggi+5)]);

    grid on

end

 

 

 

% — Executes on button press in pushbutton2.

function pushbutton2_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton2 (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

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

close;

 

 

% ——————————————————————–

function Untitled_1_Callback(hObject, eventdata, handles)

% hObject    handle to Untitled_1 (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

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

 Ingin tau hasilnya? cekidot 🙂

Suatu ketika benda yang bergerak tersebut akan mencapai jarak terjauh pada waktu tertentu. Jika waktu yang kita inputkan lebih dari waktu yang dibutuhkan untuk mencapai jarak terjauh maka hasilnya akan seperti ini

Kalau ada yang perlu didiskusikan, komen aja ya 🙂

Semoga bermanfaat

35 thoughts on “Membuat GUI di MATLAB

  1. kepada saudara Rizki..terimakasih atas artikel tutorialnya sgt membantu dlm tugas saya..,adakah simulasi selain diatas seperti simulasi kontrol PID misalnya …!

  2. gan bisa tolong ane nggak…..hehe
    ane dikasih tugas nih untuk memprakirakan debu pada luas/jarak dari cerobong asap.
    ane punya modul dan kode GUI matlapnya gan, cuma ane baru belajar, sehingga memasukan tambahan kode itu kliru atau apa ane nggak tahu. Kode tambahan nanti bisa ane kirim kemana gan? Kode ini ane dapat dari dosen
    tolong bales ane gan di :
    wijeck32@gmail.com

  3. keren!!!
    tapi aku masih ndag ngerti..
    😦 padahal rabu esok d kumpulin..
    maksud dr proyek=guidata(gcbo)
    gcbo itu ap?

  4. kalo gerak parabolanya inputnya g,v0 dan sudut, tanpa t (waktu)….gmn ya programnya…dicoba2 terus kok gagal….adakah referensinya? bantuin donk

  5. gan, mau tanya di matlab apa bisa digunakan untuk simulasi gerakan pelampung oleh gelombang laut ?

  6. Gcbo itu maksudnya apa ya ???
    Maaf mas, gimana caranya supaya ketika di run kan persamaan yang kita masukkan di edit bisa diproses . Persaannya itu persamaan acak.

    Mohon penjelasannya . 🙂
    Terimakasih

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s