MATLAB GUI实现计算器(设计)

article/2025/10/6 17:51:39

文章目录

      • 1.先打开matlab新建GUI文件
      • 2.选择路径(左边是默认的不用改)
      • 3.此时界面会弹出一个小框
      • 4.建立计算器界面(贴上我设计的界面,不许嘲笑我的设计)
      • 5.细致讲解一下,这里的按键和显示框的是怎么实现的
      • 6.把界面雏形做出之后,我们需要去实现计算器的操作
      • 7.初始化操作

1.先打开matlab新建GUI文件

新建GUI文件

2.选择路径(左边是默认的不用改)

选择路径

然后点击ok

3.此时界面会弹出一个小框

弹出框

4.建立计算器界面(贴上我设计的界面,不许嘲笑我的设计)

界面设计

5.细致讲解一下,这里的按键和显示框的是怎么实现的

A.显示框: 选择edit text
显示框
在右边屏幕拉取即可
显示框
如图所示,新建两个即可,左边作为输入屏,右边作为输入结果的显示屏
双击该框,弹出一个窗口,窗口下拉,里面有该显示框的属性
显示框的属性
string值为显示框显示的值
Tag为显示框的名字
(现在这个计算器需要显示框和输出框,如果没有新建其他的edit框,默认新建的第一个edit框的名字为edit1,第二个为edit2)

B.数字按钮和字符按钮
点击push button 新建按钮,需要几个建几个
按钮
双击之后在string那一栏改成你所需要的按钮
按钮值
回车之后,如图所示
改好的按钮值

6.把界面雏形做出之后,我们需要去实现计算器的操作

A.首先改0~9的数字键(需要点击后在edit1上显示该数字)
右键点击你需要改的button之后会出现一个窗口选择view callbacks->callback,然后会打开一个函数,蓝色字体所在的位置是该button所在的位置的代码
代码所在
按照这个方式将自己的代码改成自己所需要的数字
handles.后面加的是自己所需要将显示的文本框的名字

textString = get(handles.edit1,‘String’);
textString = strcat(textString,‘6’);
set(handles.edit1,‘String’,textString);
guidata(hObject, handles);

B.对于计算符号
计算符号

textString = get(handles.edit1,‘String’);
textString = strcat(textString,’+’);
set(handles.edit1,‘String’,textString);
guidata(hObject, handles);

加减乘除只需用换一下就好
C.对于AE操作
AE

textString = get(handles.edit1,‘String’);
textString = get(handles.edit2,‘String’);
set(handles.edit1,‘String’,’’);
set(handles.edit2,‘String’,’’);
guidata(hObject, handles);

D.对于back操作
back

textString=get(handles.edit1,‘String’);
as=char(textString);
n=length(textString);
textString=as(1:n-1);
set(handles.edit1,‘String’,textString)
guidata(hObject, handles);

E.退出操作
退出

close(gcf);

7.初始化操作

A.需要对edit1和edit2进行初始化,使代码运行时,文本框里为空白(需要在opening里改函数)
初始化

set(handles.edit1,‘string’,’’);
set(handles.edit2,‘string’,’’);

B.需要将edit1的结果在edit2中显示出来(需要改计算那个button的函数值)
计算

textString = get(handles.edit1,‘String’);
aa = eval(textString);
set(handles.edit2,‘String’,aa);
guidata(hObject, handles);

使其显示在edit2上
点击运行即可实现计算器。
计算器

function varargout = text2(varargin)
% TEXT2 MATLAB code for text2.fig
%      TEXT2, by itself, creates a new TEXT2 or raises the existing
%      singleton*.
%
%      H = TEXT2 returns the handle to a new TEXT2 or the handle to
%      the existing singleton*.
%
%      TEXT2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEXT2.M with the given input arguments.
%
%      TEXT2('Property','Value',...) creates a new TEXT2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before text2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to text2_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 text2% Last Modified by GUIDE v2.5 28-May-2018 19:45:49% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @text2_OpeningFcn, ...'gui_OutputFcn',  @text2_OutputFcn, ...'gui_LayoutFcn',  [] , ...'gui_Callback',   []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT% --- Executes just before text2 is made visible.
function text2_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 text2 (see VARARGIN)% Choose default command line output for text2
set(handles.edit1,'string','');
set(handles.edit2,'string','');handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes text2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = text2_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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
aa = eval(textString);
set(handles.edit2,'String',aa);
guidata(hObject, handles);function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'7');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'8');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'9');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'4');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'5');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'6');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'1');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'2');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'3');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'0');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'+');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = get(handles.edit2,'String');
set(handles.edit1,'String','');
set(handles.edit2,'String','');
guidata(hObject, handles);% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'-');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'*');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'/');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton17 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'4');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'7');
set(handles.edit1,'String',textString);
guidata(hObject, handles);% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton19 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcf);% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton20 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString=get(handles.edit1,'String');
as=char(textString);
n=length(textString);
textString=as(1:n-1);
set(handles.edit1,'String',textString)
guidata(hObject, handles);% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'.');
set(handles.edit1,'String',textString);
guidata(hObject, handles);

http://chatgpt.dhexx.cn/article/xJ5LQnR9.shtml

相关文章

MATLAB GUI笔记(一):按钮的使用

笔记一是简单地介绍按钮的使用,通过点击按钮实现传递字符串的功能来简单了解按钮的用法。 目录 笔记一是简单地介绍按钮的使用,通过点击按钮实现传递字符串的功能来简单了解按钮的用法。1. 打开GUI界面2. 选择Blank GUI3. 将按钮拖动出来4. 选择可编辑文…

带你从零开始,快速学会 Matlab GUI

本文来自作者 木木小迷哥 在 GitChat 上分享「Matlab GUI 零基础学员快速入门」,「阅读原文」查看交流实录 「文末高能」 编辑 | 洛肯 Matlab广泛地应用于数学分析、计算、自动控制、系统仿真、数字信号处理、图像处理、数理统计、人工智能、通信工程和金融系统等领…

如何用Matlab设计GUI?

如何用matlab设计gui? | TinTin 什么是GUI?matlab编程可以设计GUI吗? GUI定义:GUI指的是图形用户界面(Graphical User Interface,GUI),也称之为图形用户接口,GUI是用图形化方式显示…

Matlab GUI界面使用方法(一):打开GUIGUI常用控件(最基础)

GUI界面能够为我们的matlab代码提供一些有好的交互界面,下面是matlab中gui界面的一些使用方法(因为我在使用gui时,常常因为找不到一些控件的使用方法或某些功能的实现方法,所以在此为大家提供一些我遇到过的问题的解决方法&#x…

【MATLAB学习笔记——GUI】

Matlab中GUI 学习笔记 虽然打开matlab后提示说GUI后续版本会取消(以后的版本中将会删除 GUIDE。请改用 APPDESIGNER。 ),但是老师布置了matlab GUI实验,所以说还得学习学习,所以记录学习过程,学习笔记。 …

MATLAB:简单GUI的设计流程

下面来介绍一下关于MATLAB中如何设计个性化GUI界面的流程: 1、首先打开MATLAB软件如下: 2、在 主页下的“新建”选项中选择新建一个图形用户界面(GUI),结果如下: 3、这是我们选择第一个“Blank GUI&#x…

图像处理 Matlab GUI系统(全)

目录 导读 数字图像处理系统 GUI实例设计 总结 【若觉文章质量良好且有用,请别忘了点赞收藏加关注,这将是我继续分享的动力,万分感谢!】 导读 以数字图像处理系统设计为例,介绍GUI系统的设计流程,同时…

MATLAB之GUI界面介绍与搭建

作为图形用户界面(GUI)的小白,先来了解一下GUI界面的内容: MATLAB中常用的是利用guide命令搭建GUI界面。 第一步: 命令行窗口输入>>guide 第二步:弹出GUIDE快速入门窗口,选择新建GUI(同理…

Matlab制作GUI

效果图 图1.主界面 图2.其中一个子界面 具体步骤 1.界面设置 输入guide并选中运行 2.菜单设置(图1上方) 3.背景图片 参考https://blog.csdn.net/leo0308/article/details/82694995 4.界面跳转 在自动生成的Untitled_5_Callback末尾加一句set&am…

Matlab写的第一个GUI程序(程序设计,界面设计)

写在前面:博主用的Matlab版本是2016b,属于旧版本,大概在2019版本开始属于新版本叭,新版本写程序在APP菜单有设计APP这个选项,旧版本的则是在主页的新建选项下有GUI选项 目录 1.新建项目 1.1找到入口 1.2项目模板选择…

Matlab GUI界面设计

摘要:本篇博文基于MATLAB2014a进行GUI设计。 1.启动GUI设计 命令行输入guide,回车。 即可得到下面的对话框,进行相关的选择和设置 点击确定,得到两个文件,一个是.m文件,另一个是.fig文件,需要…

MATLAB(6)GUI应用介绍

目录 GUI编辑器控件属性回调函数 MATLAB常见的控件普通按钮切换按钮可编辑文本字符获取字符显示 复选框单选按钮弹出式菜单滑动条列表框表坐标区 附录各文件共享数据保存获取 GUI编辑器 MATLAB的GUI编辑器在命令行窗口输入“guide”启动,选择模板并点击确定后创建对…

matlab设计GUI可视化界面全方位解析

如何使用matlab设计GUI及导出 一、GUI的基础知识开始生成GUI界面回调函数Handles结构体GUI中的参数传递 二、控件介绍一、普通按钮二、可编辑文本(edit)和静态文本(text)三、单选框(radiobutton)和复选框&a…

matlabGUI入门

matlabGUI入门 前言1 基础知识1.1 函数1.2 数据类型1.3 绘图1.4 其它 2 GUIDE2.1 创建GUI界面2.2 模板选择2.3 控件2.4 对象浏览器2.5 回调函数2.6 属性检查器2.7 数据传输 前言 由窗口、菜单、图标、光标、按键、对话框和文本等各种图形对象组成的用户界面叫作图形用户界面&a…

matlab gui编程教程,matlab如何使用gui

如何在Matlab中打开GUI工具 两种方法1、输入guide回车。2、在工具栏里点击带笔形的gui。 为Matlab的GUI添加启动画面:添加启动画面,启动画面中可以添加想要添加的图像……VisualC可以实现这个功能,Matlab也可以实现,具体如下&am…

Matlab系列之GUI设计基础

Matlab系列之GUI设计基础 简介编辑界面菜单设计控件设计控件描述个人理解Matlab转译 控件属性【1】外观与行为【2】控件对象的信息【3】回调函数【4】状态信息 结束更多精彩,等你发现~ 简介 GUI即图形用户界面(Graphical User Interface),人…

BERT的get_sequence_output与get_pooled_output方法

BERT的get_sequence_output方法获取token向量是如何得到的? 通过如下方法得到,实际上获取的是encoder端最后一层编码层的特征向量。 BERT的get_pooled_output方法获取的句子向量是如何得到的? 通过如下方法得到,实际上获取的是[…

mybatis 连接池POOLED分析

mybatis提供了三种连接池的配置方式: 配置的位置:主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。 type属性的取值: POOLED 采用传统的javax.sql.DataSource规范中的连接池,…

读论文:Pooled Contextualized Embeddings for Named Entity Recognition

最近在看命名实体识别方向的最新的paper。在这个方向&#xff0c;18年年底有一篇<contextual string embedding for sequence labeling>&#xff0c;在CoNLL03 数据集的F1值超过BERT达到了93.09。做法是弄了个预训练的character_embedding&#xff0c;用character_embedd…

pooled-jms_Hibernate隐藏的宝石:pooled-lo优化器

pooled-jms 介绍 在这篇文章中&#xff0c;我们将揭示一个序列标识符生成器&#xff0c;​​它结合了标识符分配效率和与其他外部系统的互操作性&#xff08;同时访问底层数据库系统&#xff09;。 传统上&#xff0c;有两种序列标识符策略可供选择。 序列标识符&#xff0c;…