matlab实现带界面的科学计算器
时间: 2023-07-13 21:36:33 浏览: 312
以下是一个使用MATLAB GUIDE工具箱实现的简单的带界面的科学计算器。该计算器支持常见的算术、三角函数、指数函数、对数函数和常数π的计算。
1. 打开MATLAB,创建一个新的GUI窗口应用程序。
2. 在GUI窗口中,添加一个文本框和一些按钮,用于输入和显示计算结果。
3. 为按钮添加回调函数,实现对应的计算功能。
4. 在回调函数中,使用MATLAB内置函数计算结果,并将结果显示在文本框中。
以下是一个示例代码:
```matlab
function varargout = scientific_calculator(varargin)
% SCIENTIFIC_CALCULATOR MATLAB code for scientific_calculator.fig
% SCIENTIFIC_CALCULATOR, by itself, creates a new SCIENTIFIC_CALCULATOR or raises the existing
% singleton*.
%
% H = SCIENTIFIC_CALCULATOR returns the handle to a new SCIENTIFIC_CALCULATOR or the handle to
% the existing singleton*.
%
% SCIENTIFIC_CALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SCIENTIFIC_CALCULATOR.M with the given input arguments.
%
% SCIENTIFIC_CALCULATOR('Property','Value',...) creates a new SCIENTIFIC_CALCULATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before scientific_calculator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to scientific_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 scientific_calculator
% Last Modified by GUIDE v2.5 09-Dec-2021 14:23:56
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @scientific_calculator_OpeningFcn, ...
'gui_OutputFcn', @scientific_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 scientific_calculator is made visible.
function scientific_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 scientific_calculator (see VARARGIN)
% Choose default command line output for scientific_calculator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes scientific_calculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Initialize the calculator
handles.num1 = '';
handles.num2 = '';
handles.operator = '';
handles.result = '';
set(handles.textResult, 'String', '0');
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = scientific_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)
handles = addToNum(handles, '1');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '2');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '3');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '4');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '5');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '6');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '7');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '8');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '9');
guidata(hObject, handles);
% --- 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)
handles = addToNum(handles, '0');
guidata(hObject, handles);
% --- 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)
handles = setOperator(handles, '+');
guidata(hObject, 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)
handles = setOperator(handles, '-');
guidata(hObject, 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)
handles = setOperator(handles, '*');
guidata(hObject, 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)
handles = setOperator(handles, '/');
guidata(hObject, handles);
% --- Executes on button press in btnEquals.
function btnEquals_Callback(hObject, eventdata, handles)
% hObject handle to btnEquals (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = calculateResult(handles);
guidata(hObject, handles);
% --- Executes on button press in btnClear.
function btnClear_Callback(hObject, eventdata, handles)
% hObject handle to btnClear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.num1 = '';
handles.num2 = '';
handles.operator = '';
handles.result = '';
set(handles.textResult, 'String', '0');
guidata(hObject, handles);
% --- Executes on button press in btnSin.
function btnSin_Callback(hObject, eventdata, handles)
% hObject handle to btnSin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = addToNum(handles, 'sin('
阅读全文