How to create a GUI based calculator

10 次查看(过去 30 天)
Hello everyone, I'v tried to create a GUI simple calculator with the operators: plus, minus, multiplication and division. Unfortunately, I'v some struggles and it does not working! Can someone help me figure what is the problem in my code?
Thank you!
function varargout = GUIcalculator(varargin)
% GUICALCULATOR MATLAB code for GUIcalculator.fig
% GUICALCULATOR, by itself, creates a new GUICALCULATOR or raises the existing
% singleton*.
%
% H = GUICALCULATOR returns the handle to a new GUICALCULATOR or the handle to
% the existing singleton*.
%
% GUICALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUICALCULATOR.M with the given number arguments.
%
% GUICALCULATOR('Property','Value',...) creates a new GUICALCULATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUIcalculator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUIcalculator_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 GUIcalculator
% Last Modified by GUIDE v2.5 03-Oct-2020 19:43:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUIcalculator_OpeningFcn, ...
'gui_OutputFcn', @GUIcalculator_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 GUIcalculator is made visible.
function GUIcalculator_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 GUIcalculator (see VARARGIN)
% Choose default command line output for GUIcalculator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUIcalculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUIcalculator_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 number_Callback(hObject, eventdata, handles)
% hObject handle to number (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 number as text
% str2double(get(hObject,'String')) returns contents of number as a double
% --- Executes during object creation, after setting all properties.
function number_CreateFcn(hObject, eventdata, handles)
% hObject handle to number (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 plus.
function plus_Callback(hObject, eventdata, handles)
% hObject handle to plus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'+');
set(handles.number,'String',str);
% --- Executes on button press in minus.
function minus_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'-');
set(handles.number,'String',str);
% --- Executes on button press in minus.
function multiply_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)str= get(hendles.number,'string');
str= get(handles.number,'string');
str=strcat(str,'*');
set(handles.number,'String',str);
% --- Executes on button press in minus.
function divide_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)str= get(hendles.number,'string');
str= get(handles.number,'string');
str=strcat(str,'/');
set(handles.number,'String',str);
% --- Executes on button press in minus.
function period_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)str= get(hendles.number,'string');
str= get(handles.number,'string');
str=strcat(str,'.');
set(handles.number,'String',str);
% --- Executes on button press in minus.
function clear_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)str= get(hendles.number,'string');
set(handles.number,'String','');
% --- Executes on button press in zero.
function zero_Callback(hObject, eventdata, handles)
% hObject handle to zero (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'0');
set(handles.number,'String',str);
% --- Executes on button press in one.
function one_Callback(hObject, eventdata, handles)
% hObject handle to one (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'1');
set(handles.number,'String',str);
% --- Executes on button press in two.
function two_Callback(hObject, eventdata, handles)
% hObject handle to two (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'2');
set(handles.number,'String',str);
% --- Executes on button press in three.
function three_Callback(hObject, eventdata, handles)
% hObject handle to three (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'3');
set(handles.number,'String',str);
% --- Executes on button press in four.
function four_Callback(hObject, eventdata, handles)
% hObject handle to four (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'4');
set(handles.number,'String',str);
% --- Executes on button press in five.
function five_Callback(hObject, eventdata, handles)
% hObject handle to five (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'5');
set(handles.number,'String',str);
% --- Executes on button press in six.
function six_Callback(hObject, eventdata, handles)
% hObject handle to six (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'6');
set(handles.number,'String',str);
% --- Executes on button press in eight.
function seven_Callback(hObject, eventdata, handles)
% hObject handle to eight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'7');
set(handles.number,'String',str);
% --- Executes on button press in eight.
function eight_Callback(hObject, eventdata, handles)
% hObject handle to eight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'8');
set(handles.number,'String',str);
% --- Executes on button press in nine.
function nine_Callback(hObject, eventdata, handles)
% hObject handle to nine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number,'string');
str=strcat(str,'9');
set(handles.number,'String',str);
% --- Executes on button press in solve.
function solve_Callback(hObject, eventdata, handles)
% hObject handle to solve (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str= get(handles.number, 'string');
answer= eval(str);
set(handles.number,'double',answer);

回答(1 个)

Altaïr
Altaïr 2025-5-28
The set function inside the solve_callback is trying to update the double property of the handles.number object. But the previous callback functions suggest that you need to set the String property instead. Use the num2str function to convert the answer variable into a string.
You can read more about num2str using the following command:
web(fullfile(docroot, 'matlab/ref/num2str.html'))
Apart from this make sure to end all the function definitions with the end keyword. This is to prevent the error arising from functions being nested too deeply as discussed in the following answer:

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by