]

January 28, 2013

HOW TO MAKE CALCULATOR WITH MATLAB GUI

Membuat program kalkulator pada sebuah bahasa pemrograman berbasis GUI merupakan tantangan yang menarik karena kreatifitas kita diuji dalam hal ini. Kemampuan dalam melogikakan proses eksekusi kalkulator merupakan sedikit banyak hal yang perlu dimiliki. Saya mencoba membuat program kalkulator dengan menggunakan matlab GUI. Silakan dipelajari dan dimodifikasi semaunya. berikut ini program yang bisa anda gunakan
function varargout = calculator(varargin) % CALCULATOR M-file for calculator.fig % CALCULATOR, by itself, creates a new CALCULATOR or raises the existing % singleton*. % % H = CALCULATOR returns the handle to a new CALCULATOR or the handle to % the existing singleton*. % % CALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in CALCULATOR.M with the given input arguments. % % CALCULATOR('Property','Value',...) creates a new CALCULATOR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before calculator_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to calculator_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 % Original Made By JANS HENDRY Butarbutar % Edit the above text to modify the response to help calculator % Last Modified by GUIDE v2.5 28-Jan-2013 11:30:40 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @calculator_OpeningFcn, ... 'gui_OutputFcn', @calculator_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end

[janshendry@gmail.com]

Page 1

January 28, 2013

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 calculator is made visible. function calculator_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 calculator (see VARARGIN) % Choose default command line output for calculator clc; handles.output = hObject; set(handles.samping, 'UserData', 0); set(handles.pbhasil, 'UserData', 0); set(handles.utama, 'UserData', 0); % Update handles structure guidata(hObject, handles); % UIWAIT makes calculator wait for user response (see UIRESUME) % uiwait(handles.figure1);

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

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

[janshendry@gmail.com]

Page 2

January 28, 2013

function utama_CreateFcn(hObject, eventdata, handles) % hObject handle to utama (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 samping_Callback(hObject, eventdata, handles) % hObject handle to samping (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 samping as text % str2double(get(hObject,'String')) returns contents of samping as a double

% --- Executes during object creation, after setting all properties. function samping_CreateFcn(hObject, eventdata, handles) % hObject handle to samping (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 input(bil) h = findobj(gcbf, 'Style', 'edit'); picu = get(h(1), 'UserData'); if (picu == 0) angka = get(h(2), 'String'); if ~strcmp(angka, '0') angka2 = strcat(angka, bil); else angka2 = bil; end set(h(2), 'String', angka2); else angka = '0'; set(h(2), 'String', angka); if ~strcmp(angka, '0') angka2 = strcat(angka, bil);

[janshendry@gmail.com]

Page 3

January 28, 2013

else angka2 = bil; end set(h(2), 'String', angka2); set(h(1), 'UserData', 0); end function pb0_Callback(hObject, eventdata, handles) input('0'); function pb1_Callback(hObject, eventdata, handles) input('1'); function pb2_Callback(hObject, eventdata, handles) input('2'); function pb3_Callback(hObject, eventdata, handles) input('3'); % --- Executes on button press in pbkurang. function pbkurang_Callback(hObject, eventdata, handles) tanda = 2; % artinya pengurangan operator(tanda);

% --- Executes on button press in pbhasil. function pbhasil_Callback(hObject, eventdata, handles) angka1 = get(handles.figure1, 'UserData'); angka2 = str2double(get(handles.utama, 'String')); op = get(handles.utama, 'UserData'); if op == 1 hasil = angka1 + angka2; temp1 = sprintf(' + '); elseif op == 2 hasil = angka1 - angka2; temp1 = sprintf(' - '); elseif op == 3 hasil = angka1 * angka2; temp1 = sprintf(' * '); elseif op == 4 hasil = angka1 / angka2; temp1 = sprintf(' / '); else hasil = num2str(angka2); end if op == 0 set(handles.samping, 'String', num2str(angka2)); else temp = strcat(num2str(angka1), temp1, sprintf(' %s',num2str(angka2))); set(handles.samping, 'String', temp); end set(handles.utama, 'String', hasil);

[janshendry@gmail.com]

Page 4

January 28, 2013

set(handles.samping, 'UserData', 1); function pbplus_Callback(hObject, eventdata, handles) tanda = 1; % artinya penjumlahan operator(tanda); function operator(tanda) h = findobj(gcbf, 'Style', 'edit'); angka = get(h(2), 'String'); h2 = findobj(gcbf, 'Tag', 'figure1'); switch(tanda) case 1 temp = sprintf(' + '); angka3 = strcat(angka, temp); set(h(1), 'String', angka3); case 2 temp = sprintf(' - '); angka3 = strcat(angka, temp); set(h(1), 'String', angka3); case 3 temp = sprintf(' * '); angka3 = strcat(angka, temp); set(h(1), 'String', angka3); case 4 temp = sprintf(' / '); angka3 = strcat(angka, temp); set(h(1), 'String', angka3); end set(h(1), 'UserData', 1); set(h(2), 'UserData', tanda); % 1 artinya tambah set(h2, 'UserData', str2double(angka)); function pbdot_Callback(hObject, eventdata, handles) angka = get(handles.utama, 'String'); [bar, kol] = find(angka == '.'); if isempty(kol) hasil = strcat(angka, '.'); else hasil = angka; end set(handles.utama, 'String', hasil); function pb4_Callback(hObject, eventdata, handles) input('4'); function pb5_Callback(hObject, eventdata, handles) input('5'); function pb6_Callback(hObject, eventdata, handles) input('6'); % --- Executes on button press in pbkali. function pbkali_Callback(hObject, eventdata, handles) tanda = 3; % artinya perkalian

[janshendry@gmail.com]

Page 5

January 28, 2013

operator(tanda);

% --- Executes on button press in pushbutton13. function pushbutton13_Callback(hObject, eventdata, handles) angka = str2double(get(handles.utama, 'String')); inve = (1/angka); set(handles.utama, 'String', num2str(inve)); temp = strcat('1/(', num2str(angka), ')'); set(handles.samping, 'String', temp); set(handles.samping, 'UserData', 1);

% --- Executes on button press in pb7. function pb7_Callback(hObject, eventdata, handles) input('7');

% --- Executes on button press in pb8. function pb8_Callback(hObject, eventdata, handles) input('8');

% --- Executes on button press in pb9. function pb9_Callback(hObject, eventdata, handles) input('9');

% --- Executes on button press in pbbagi. function pbbagi_Callback(hObject, eventdata, handles) tanda = 4; % artinyan pembagian operator(tanda);

function pbpersen_Callback(hObject, eventdata, handles) angka = str2double(get(handles.utama, 'String')); persen = angka / 100; set(handles.utama, 'String', num2str(persen)); temp = strcat('%(', num2str(angka), ')'); set(handles.samping, 'String', temp); set(handles.samping, 'UserData', 1); function pbhapus_Callback(hObject, eventdata, handles) angka = get(handles.utama, 'String'); if (~strcmp(angka, 'Inf')) && (~strcmp(angka, 'Nan')) if ~strcmp(angka, '0') angka = angka(1:length(angka)-1); if isempty(angka) angka = '0'; end set(handles.utama, 'String', angka); end

[janshendry@gmail.com]

Page 6