Sketch In Matlab GUI

5

Click here to load reader

Transcript of Sketch In Matlab GUI

Page 1: Sketch In Matlab GUI

[digital image processing]

April 11, 2012

[email protected] Page 1

SKETCH IN MATLAB GUI

Well, ini hanyalah artikel untuk membuat GUI Matlab yang bisa digunakan sebagai media

sketch. Memang tidak profesional, tapi code atau program di dalamnya bisa anda kembangkan

menjadi program yang lebih profesional pada aplikasi-aplikasi khusus yang anda inginkan.

Tidak ada teori khusus yang perlu diterangkan, karena fungsi-fungsi di dalamnya adalah buatan

kita sendiri. Instruksinya pun tidak terlalu sulit.

Perhatikan tampilan di bawah ini

Program ini merupakan demo untuk memanfaatkan matlab secara interaktif. Kita bisa memilih

warna yang diinginkan lalu dijadikan untuk menggambar kurva pada bidang gambar. Ini

hanyalah demo version dari saya, sehingga tidak profesional.

Ada beberapa bagian yang saya ambil dari sebuah website yang bagus. Saya terinspirasi dari

code yang telah dibuat olehnya. Saya jadikan referensi pada bagian akhir dari artikel ini. Bila

program dijalankan, maka akan dihasilkan:

Page 2: Sketch In Matlab GUI

[digital image processing]

April 11, 2012

[email protected] Page 2

Di bawah ini adalah programnya:

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text1. function text1_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % global done; % axes(handles.axes1); warna=1; callme(warna);

function callme(warna) global P; global wrn;

switch warna case 1 wrn='r'; % merah case 2 wrn='k'; % hitam case 3 wrn='b'; % biru case 4

Page 3: Sketch In Matlab GUI

[digital image processing]

April 11, 2012

[email protected] Page 3

wrn='y'; % kuning case 5 wrn='g'; % hijau case 6 wrn='c'; % cyan case 7 wrn='m'; % magenta end P=get_pencil_curve();

function P = get_pencil_curve() global p;

axis manual;

set(gcf,'windowbuttondownfcn',@ondown); set(gcf,'keypressfcn', @onkeypress); P = []; p = [];

function ondown(src,ev) set(gcf,'windowbuttonmotionfcn', @ondrag); set(gcf,'windowbuttonupfcn', @onup); append_current_point();

function ondrag(src,ev) append_current_point();

function onup(src,ev) finish(); %%

function append_current_point() global P; global p; global wrn;

try cp = get(gca,'currentpoint'); P = [P;cp(1,:)]; if isempty(p) % init plot hold on; p = plot(P(:,1),P(:,2),wrn); hold off; else % update plot set(p,'Xdata',P(:,1),'Ydata',P(:,2)); end catch end %%

function finish() global done; done = true; set(gcf,'windowbuttonmotionfcn',''); set(gcf,'windowbuttonupfcn',''); set(gcf,'windowbuttondownfcn','');

Page 4: Sketch In Matlab GUI

[digital image processing]

April 11, 2012

[email protected] Page 4

%%

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text2. function text2_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=2; callme(warna);

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text3. function text3_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=3; callme(warna);

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text4. function text4_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=4; callme(warna);

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text5. function text5_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=5; callme(warna);

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text6. function text6_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=6; callme(warna);

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text7.

Page 5: Sketch In Matlab GUI

[digital image processing]

April 11, 2012

[email protected] Page 5

function text7_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % axes(handles.axes1); warna=7; callme(warna);

Anda bisa melihat sendiri bahwa program ini cukup mudah untuk dipahami... ☺ (entah icon

smiley ini artinya mengejek atau menyemangati, entah...)

@ thanks...

This program is modified version from http://www.alecjacobson.com/weblog/?p=2473 .