在matlab用gui写一个计算机
时间: 2023-12-12 15:03:22 浏览: 32
好的,我可以为您提供一个简单的 MATLAB GUI 计算器示例代码。此代码将创建一个包含数字按钮和运算符按钮的 GUI 窗口,用户可以通过单击这些按钮来构建算术表达式,并通过单击“计算”按钮计算表达式结果。
首先,创建一个名为“calculator.fig”的 GUI 窗口,将数字按钮和运算符按钮添加到窗口上,并将它们分别命名为“btn1”、“btn2”、“btn3”、“btn4”、“btn5”、“btn6”、“btn7”、“btn8”、“btn9”、“btn0”、“btnPlus”、“btnMinus”、“btnMultiply”、“btnDivide”和“btnCalculate”。然后,使用以下代码将计算器窗口与 MATLAB 代码关联:
```
function varargout = calculator(varargin)
% CALCULATOR MATLAB code 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
% Edit the above text to modify the response to help calculator
% Last Modified by GUIDE v2.5 25-Jun-2021 13:28:30
% 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
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
handles.output = hObject;
% 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;
% --- Executes on button press in btn1.
function btn1_Callback(hObject, eventdata, handles)
% hObject handle to btn1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '1');
% --- Executes on button press in btn2.
function btn2_Callback(hObject, eventdata, handles)
% hObject handle to btn2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '2');
% --- Executes on button press in btn3.
function btn3_Callback(hObject, eventdata, handles)
% hObject handle to btn3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '3');
% --- Executes on button press in btn4.
function btn4_Callback(hObject, eventdata, handles)
% hObject handle to btn4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '4');
% --- Executes on button press in btn5.
function btn5_Callback(hObject, eventdata, handles)
% hObject handle to btn5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '5');
% --- Executes on button press in btn6.
function btn6_Callback(hObject, eventdata, handles)
% hObject handle to btn6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '6');
% --- Executes on button press in btn7.
function btn7_Callback(hObject, eventdata, handles)
% hObject handle to btn7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '7');
% --- Executes on button press in btn8.
function btn8_Callback(hObject, eventdata, handles)
% hObject handle to btn8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '8');
% --- Executes on button press in btn9.
function btn9_Callback(hObject, eventdata, handles)
% hObject handle to btn9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '9');
% --- Executes on button press in btn0.
function btn0_Callback(hObject, eventdata, handles)
% hObject handle to btn0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '0');
% --- Executes on button press in btnPlus.
function btnPlus_Callback(hObject, eventdata, handles)
% hObject handle to btnPlus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '+');
% --- Executes on button press in btnMinus.
function btnMinus_Callback(hObject, eventdata, handles)
% hObject handle to btnMinus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '-');
% --- Executes on button press in btnMultiply.
function btnMultiply_Callback(hObject, eventdata, handles)
% hObject handle to btnMultiply (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '*');
% --- Executes on button press in btnDivide.
function btnDivide_Callback(hObject, eventdata, handles)
% hObject handle to btnDivide (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '/');
% --- Executes on button press in btnCalculate.
function btnCalculate_Callback(hObject, eventdata, handles)
% hObject handle to btnCalculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
expression = get(handles.txtExpression, 'String');
try
result = eval(expression);
set(handles.txtResult, 'String', num2str(result));
catch
set(handles.txtResult, 'String', 'Error');
end
% --- Helper function to append a character to the expression text box
function append_expression(handles, char)
expression = get(handles.txtExpression, 'String');
if strcmp(expression, '0')
expression = char;
else
expression = strcat(expression, char);
end
set(handles.txtExpression, 'String', expression);
```
此代码将创建一个名为“calculator.m”的 MATLAB 脚本,其中包含了许多回调函数,用于处理用户与 GUI 窗口的交互。
最后,将以下代码添加到脚本的末尾以启动 GUI 窗口:
```
% --- Executes on button press in btn1.
function btn1_Callback(hObject, eventdata, handles)
% hObject handle to btn1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
append_expression(handles, '1');
% 创建 GUI 窗口并等待用户交互
handles.output = hObject;
guidata(hObject, handles);
uiwait(handles.figure1);
```
现在,运行脚本并单击“运行”按钮,将显示计算器 GUI 窗口。用户可以单击数字和运算符按钮来构建算术表达式,然后单击“计算”按钮计算表达式结果。