This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test_GUI | |
clc; | |
% pelajari kembali penggunaan perintah 'struct' di matlab sehingga anda | |
% paham maksud perintah 'M.f', 'M.ax', dan lain-lain. | |
M.f=figure('menubar', 'none'); | |
M.ax = axes('units','pix' ,'position',[60,110, 470,280]); | |
t =0:.1:10; | |
plot(t,sin(t)); | |
xlabel('t'); | |
ylabel('sin(t)'); | |
M.xmin = get(M.ax, 'xlim'); | |
M.ymin = get(M.ax, 'ylim'); | |
M.xmax = M.xmin(2); | |
M.ymax = M.ymin(2); | |
M.xmin = M.xmin(1); | |
M.ymin = M.ymin(1); | |
M.keterangan(1) = uicontrol('units', 'pix','style', 'tex', ... | |
'position',[40,14, 150,20],'string', 'Posisi klik:' , ... | |
'fontsize', 14 , 'fontweight', 'bold' ,... | |
'backg' , get(M.f, 'color'), 'HorizontalAlignment', 'left'); | |
M.keterangan(2) = uicontrol('units', 'pix','style', 'tex', ... | |
'position',[200,14, 190,23],'string', '' , ... | |
'fontsize', 14 , 'fontweight', 'bold' ,... | |
'backg' , get(M.f, 'color'),'HorizontalAlignment', 'right'); | |
M.keterangan(3) = uicontrol('units', 'pix','style', 'tex', ... | |
'position',[40,40, 150,20],'string', 'Posisi pointer:' , ... | |
'fontsize', 14 , 'fontweight', 'bold' ,... | |
'backg' , get(M.f, 'color'), 'HorizontalAlignment', 'left'); | |
M.keterangan(4) = uicontrol('units', 'pix','style', 'tex', ... | |
'position',[200,40, 190,23],'string', '' , ... | |
'fontsize', 14 , 'fontweight', 'bold' ,... | |
'backg' , get(M.f, 'color'), 'HorizontalAlignment', 'right'); | |
set(M.ax,... | |
'buttondownfcn',...% menanggapi jika mouse di klik di atas 'axes'/daerah plot | |
{@point_location, ... % panggil fungsi ini | |
M}); % masukkan struktur M sebagai parameter | |
% untuk mendeteksi pergerakan mouse di atas axes, karena secara default | |
% MATLAB g' menyediakan pendeteksi pergerakan untuk axes jadi kita gunakan | |
% pendeteksi pergerekan dari figure, dan membandingkan nilainya dengan | |
% lokasi axes. | |
set(M.f, 'windowbuttonmotionfcn', {@lokasi_pointer , M}); % end function | |
% 'varargin' itu dalam MATLAB ditulis dengan huruf kecil, untuk lebih | |
% jelasnya silakan pelajari lebih lanjut. | |
function [] = point_location(varargin) | |
% struktur (atau 'struct') dalam 'varargin' ditempatkan pada item ke-3 | |
M = varargin{3}; | |
xx=get(M.ax,'currentpoint'); | |
klik_mana =get(M.f, 'selectiontype'); | |
xx = xx(1,:); | |
% jika tidak lewat batas daerah | |
% plot. Ingat axes itu terdiri dari sumbu ('tickmark') dan daerah | |
% plotnya. Jadi walaupun sudah di luar daerah plot, akan tetapi masih | |
% di atas sumbu ('tickmark'), maka itu masih dideteksi di dalam axes. | |
% kemudian jika mouse diklik kiri bukan diklik kanan. | |
if ~(xx(1)<M.xmin)&&~(xx(2) < M.ymin)&& ... | |
(strcmp('normal', klik_mana)==1) | |
% tampilkan dalam dua angka di belakang koma dan 1 angka di depan koma | |
set(M.keterangan(2), 'str', strcat('x= ', num2str(xx(1), '%2.3f'), ... | |
' y= ' ,num2str(xx(2), ' %2.3f') )); | |
end %end if | |
function lokasi_pointer(varargin) | |
C = varargin{3}; | |
xx = get(C.ax, 'currentpoint'); | |
xx = xx(1,:); | |
if ~(xx(1)<C.xmin) && ~(xx(2)< C.ymin) ... | |
&& ~(xx(1) > C.xmax) && ~(xx(2) > C.ymax) | |
set(C.keterangan(4), 'str', strcat('x= ', num2str(xx(1), '%2.3f'), ... | |
' y= ' ,num2str(xx(2), ' %2.3f') )); | |
end %end if | |