Thursday, 13 August 2015

Cara membuat kalkulator dengan GUI MATLAB


Bagi yang masih bingung bagaimana membuat aplikasi kalkulator dengan GUI MATLAB brikut saya akan jelaskan langkah-langkahnya.

1. Yang pertama pastikan aplikasi Matlab terinstall di komputer
2. Yang kedua ketik guide pada command window
3. Pilih Blank gui ( Default)
4. Lalu Buat tombol kalkulator seperti gambar di atas, untuk layar hasil gunakan data stastic sedangkan untuk tombol yang lainnya gunakan push button. pastikan agar tag nya terisi agar saat memasukan sourcode tidak tertukar. misal untuk tombol 1 isi dengan tag pb1 dan seterusnya.
5. Masukan Sourcode seperti di bawah ini
function varargout = coba(varargin)
% COBA M-file for coba.fig
%      COBA, by itself, creates a new COBA or raises the existing
%      singleton*.
%
%      H = COBA returns the handle to a new COBA or the handle to
%      the existing singleton*.
%
%      COBA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COBA.M with the given input arguments.
%
%      COBA('Property','Value',...) creates a new COBA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before coba_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to coba_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 coba

% Last Modified by GUIDE v2.5 28-May-2015 14:05:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @coba_OpeningFcn, ...
                   'gui_OutputFcn',  @coba_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 coba is made visible.
function coba_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 coba (see VARARGIN)

% Choose default command line output for coba
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = coba_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 txthasil_Callback(hObject, eventdata, handles)
% hObject    handle to txthasil (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 txthasil as text
%        str2double(get(hObject,'String')) returns contents of txthasil as a double


% --- Executes during object creation, after setting all properties.
function txthasil_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txthasil (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 pb1.
function pb1_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','1');
else
textstring=strcat(textstring,'1');
set(handles.txthasil,'string',textstring)

end
jj=0;
   
% hObject    handle to pb1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb2.
function pb2_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','2');
else
textstring=strcat(textstring,'2');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb5.
function pb5_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','5');
else
textstring=strcat(textstring,'5');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb4.
function pb4_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','4');
else
textstring=strcat(textstring,'4');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb0.
function pb0_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','0');
else
textstring=strcat(textstring,'0');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb8.
function pb8_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','8');
else
textstring=strcat(textstring,'8');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb9.
function pb9_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','9');
else
textstring=strcat(textstring,'9');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb6.
function pb6_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','6');
else
textstring=strcat(textstring,'6');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pb3.
function pb3_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','3');
else
textstring=strcat(textstring,'3');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pbhasil.
function pbhasil_Callback(hObject, eventdata, handles)
textstring=get(handles.txthasil,'string');
ans=eval(textstring);
set(handles.txthasil,'string',ans)
% hObject    handle to pbhasil (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


% --- Executes on button press in pb7.
function pb7_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','7');
else
textstring=strcat(textstring,'7');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pb7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pbbagi.
function pbbagi_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','/');
else
textstring=strcat(textstring,'/');
set(handles.txthasil,'string',textstring)

end
jj=0;

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


% --- Executes on button press in pbkali.
function pbkali_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','*');
else
textstring=strcat(textstring,'*');
set(handles.txthasil,'string',textstring)

end
jj=0;

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


% --- Executes on button press in pbminus.
function pbminus_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','-');
else
textstring=strcat(textstring,'-');
set(handles.txthasil,'string',textstring)

end
jj=0;

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


% --- Executes on button press in pbplus.
function pbplus_Callback(hObject, eventdata, handles)
global jj
textstring=get(handles.txthasil,'string');
if(strcmp(textstring,'0')==1)&&(jj==0)
    set(handles.txthasil,'string','+');
else
textstring=strcat(textstring,'+');
set(handles.txthasil,'string',textstring)

end
jj=0;
% hObject    handle to pbplus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

6. lalu simpan sourcode di atas.. sebelum itu coba tes kalkulator dulu
7. kalkulator siap untuk digunakan.

No comments:

Post a Comment