Matlab设计简易计算器

效果如如下:

整个工程还是挺简单的,之前一直都是用matlab做信号处理,由于要做课程设计,就学了一下matlab的GUI。下面总结几个关键的地方。

(1)控件拉到自己喜欢的位置,并将控件的Text和Tag改好(不改也行,看个人习惯)

(2)设置一个全局变量global TextString,用来记录用户按了什么按钮,把字符串拼接起来

(3)做好每个按钮的回调函数的工作

(4)eval()函数能够把字符串转换成matlab命令并返回结果(用在‘=’的回调函数)

function varargout = untitled2(varargin)
% UNTITLED2 MATLAB code for untitled2.fig
%      UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing
%      singleton*.
%
%      H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to
%      the existing singleton*.
%
%      UNTITLED2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED2.M with the given input arguments.
%
%      UNTITLED2('Property','Value',...) creates a new UNTITLED2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled2_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 untitled2

% Last Modified by GUIDE v2.5 09-Jul-2021 22:30:29

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

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes during object creation, after setting all properties.
function window_CreateFcn(hObject, eventdata, handles)
% hObject    handle to window (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
global TextString

% --- 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)
global TextString
textString =  get(handles.one,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)
% --- 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)
global TextString
textString = get(handles.two,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- 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)
global TextString
textString = get(handles.three,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)


% --- 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)
global TextString
textString = get(handles.four,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- 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)
global TextString
textString = get(handles.five,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- 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)
global TextString
textString = get(handles.six,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in seven.
function seven_Callback(hObject, eventdata, handles)
% hObject    handle to seven (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.seven,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- 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)
global TextString
textString = get(handles.eight,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- 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)
global TextString
textString = get(handles.nine,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)


% --- Executes on button press in addition.
function addition_Callback(hObject, eventdata, handles)
% hObject    handle to addition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.addition,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in subtraction.
function subtraction_Callback(hObject, eventdata, handles)
% hObject    handle to subtraction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.subtraction,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in multiplication.
function multiplication_Callback(hObject, eventdata, handles)
% hObject    handle to multiplication (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.multiplication,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject    handle to division (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.division,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in sqrt.
function sqrt_Callback(hObject, eventdata, handles)
% hObject    handle to sqrt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.sqrt,'String')
TextString = strcat(TextString , textString) 
set(handles.window,'string',textString)

% --- Executes on button press in sin.
function sin_Callback(hObject, eventdata, handles)
% hObject    handle to sin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.sin,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in cos.
function cos_Callback(hObject, eventdata, handles)
% hObject    handle to cos (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cos,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in tan.
function tan_Callback(hObject, eventdata, handles)
% hObject    handle to tan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.tan,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in square.
function square_Callback(hObject, eventdata, handles)
% hObject    handle to square (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.square,'String')
TextString = strcat(TextString ,'^2') 
set(handles.window,'string',textString)

% --- Executes on button press in cot.
function cot_Callback(hObject, eventdata, handles)
% hObject    handle to cot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cot,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)


% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject    handle to equal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
ans = eval(TextString)
a = strcat(TextString,'=')
aa = strcat(a,num2str(ans))
set(handles.window,'string',aa)
TextString = []


% --- Executes on button press in right.
function right_Callback(hObject, eventdata, handles)
% hObject    handle to right (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.right,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)

% --- Executes on button press in left.
function left_Callback(hObject, eventdata, handles)
% hObject    handle to left (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.left,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)


% --- Executes on button press in qingkong.
function qingkong_Callback(hObject, eventdata, handles)
% hObject    handle to qingkong (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
TextString = []

 

  • 9
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用MATLAB GUI设计简易计算器的步骤: 1. 打开MATLAB软件并创建一个新的GUI应用程序。 2. 在GUI设计器中,添加一个文本框用于显示计算结果。 3. 添加一组按钮,每个按钮代表一个数字或运算符。 4. 为每个按钮添加回调函数,以便在用户点击按钮时执行相应的操作。 5. 在回调函数中,根据用户的操作更新文本框中的内容。 6. 实现基本的加减乘除运算逻辑,根据用户的操作进行相应的计算。 7. 在GUI设计器中设置按钮和文本框的位置和大小,以使界面看起来更加美观。 8. 运行GUI应用程序,测试计算器的功能。 下面是一个简单的MATLAB GUI计算器的示例代码: ```matlab function calculator_GUI % 创建GUI窗口 fig = uifigure('Name', '简易计算器', 'Position', [100 100 300 400]); % 创建文本框用于显示计算结果 resultText = uitextarea(fig, 'Position', [50 300 200 50]); % 创建数字按钮 for i = 1:9 btn = uibutton(fig, 'Text', num2str(i), 'Position', [50+(i-1)*50 200 50 50], 'ButtonPushedFcn', @(btn,event) appendNumber(btn.Text)); end % 创建运算符按钮 btnPlus = uibutton(fig, 'Text', '+', 'Position', [50 150 50 50], 'ButtonPushedFcn', @(btn,event) setOperator(btn.Text)); btnMinus = uibutton(fig, 'Text', '-', 'Position', [100 150 50 50], 'ButtonPushedFcn', @(btn,event) setOperator(btn.Text)); btnMultiply = uibutton(fig, 'Text', '*', 'Position', [150 150 50 50], 'ButtonPushedFcn', @(btn,event) setOperator(btn.Text)); btnDivide = uibutton(fig, 'Text', '/', 'Position', [200 150 50 50], 'ButtonPushedFcn', @(btn,event) setOperator(btn.Text)); % 创建等号按钮 btnEqual = uibutton(fig, 'Text', '=', 'Position', [50 100 200 50], 'ButtonPushedFcn', @(btn,event) calculate()); % 初始化变量 num1 = ''; num2 = ''; operator = ''; % 追加数字到文本框 function appendNumber(number) if isempty(operator) num1 = [num1 number]; else num2 = [num2 number]; end resultText.Value = [num1 operator num2]; end % 设置运算符 function setOperator(op) operator = op; resultText.Value = [num1 operator]; end % 执行计算 function calculate() switch operator case '+' result = str2double(num1) + str2double(num2); case '-' result = str2double(num1) - str2double(num2); case '*' result = str2double(num1) * str2double(num2); case '/' result = str2double(num1) / str2double(num2); end resultText.Value = num2str(result); end end ``` 你可以将以上代码保存为.m文件并在MATLAB中运行,即可看到一个简易的计算器界面。你可以点击数字按钮输入数字,点击运算符按钮选择运算符,点击等号按钮进行计算,计算结果将显示在文本框中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑电信号要分类

你的鼓励是我创作的前进动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值