MATLAB GUI图形化界面设计计算器

MATLAB GUI界面设计教程可以帮助用户创建交互式的图形用户界面,以简化与MATLAB程序的交互过程。以下是一个简化的教程,指导你如何进行MATLAB GUI界面设计:

1. 启动GUIDE或App Designer

  • GUIDE:在MATLAB命令窗口中输入guide命令,然后按Enter键启动GUIDE。
  • App Designer:在MATLAB的“Apps”标签下选择“App Designer”来启动。

2. 选择模板或新建空白GUI

  • 在GUIDE或App Designer中,你可以选择现有的模板作为基础,或者选择新建一个空白GUI开始设计,其中GUIDE给我们提供了以下四种模板。

  •  App Designer我们提供了以下五种模板。

3. 添加和布局组件 

  • 从组件面板中选择所需的控件,如按钮、文本框、滑动条等,并拖拽到GUI界面上。
  • 调整控件的大小和位置,以创建所需的界面布局。
  • 常见的控件有以下10种:可编程文本是动态文本,静态文本不会变化;axes1是坐标区,用于绘制图像;滑块用于查看长文本或者长图形。
  •  将所需控件组装成以下模样,最上方的文本框是可编辑文本,下方的按钮都是普通按钮:

4. 设置组件属性

  • 双击控件或选择它,并在属性编辑器中设置其属性,如字体、颜色、标签文本等。
  1. BackgroundColor——背景颜色
  2. FontAngle——字体倾斜角度
  3. FontName——字体名称
  4. FontSize——字体大小
  5. FontUnits——字体单元
  6. ForegroundColor——字体颜色
  7. Position——控件位置
  8. String——控件显示名称
  9. Tag——控件真实名称 

5. 编写回调函数

  • 回调函数定义了当用户与GUI中的控件交互时应该执行的操作。
  • 在GUIDE中,你可以双击控件并选择“Create Callback”来生成一个空的回调函数框架。
  • 在App Designer中,选择控件并在右侧的代码编辑器中编写或修改回调函数。
%清空功能
set(handles.edit1,'String','');
%标签功能(0-9,小数点,+-*/)
textString = get(handles.edit1,'String'); %获取可编辑文本的字符串
textString =strcat(textString,'1');%拼接
set(handles.edit1,'String',textString);
%等号功能
textString = get(handles.edit1,'String'); 
answer=eval(textString);%求解表达式
set(handles.edit1,'String',answer);	
  • 将上述代码写入回调函数可获得完整代码,可以根据需求添加小数点、开方等操作。
function varargout = myapp2(varargin)
% MYAPP2 MATLAB code for myapp2.fig
%      MYAPP2, by itself, creates a new MYAPP2 or raises the existing
%      singleton*.
%
%      H = MYAPP2 returns the handle to a new MYAPP2 or the handle to
%      the existing singleton*.
%
%      MYAPP2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MYAPP2.M with the given input arguments.
%
%      MYAPP2('Property','Value',...) creates a new MYAPP2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before myapp2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to myapp2_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 myapp2

% Last Modified by GUIDE v2.5 11-Apr-2024 12:06:28

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = myapp2_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'1');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'2');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'4');
set(handles.edit1,'String',textString);

% 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)


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'5');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'7');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'8');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'0');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
set(handles.edit1,'String','');
% 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)


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'3');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'6');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'9');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
answer = eval(textString,'3');%计算表达式
set(handles.edit1,'String',answer);
% 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)


% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'+');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'-');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'*');
set(handles.edit1,'String',textString);
% 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)


% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
textString = get(handles.edit1,'String');
textString = strcat(textString,'/');
set(handles.edit1,'String',textString);
% 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)



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

6. 保存和运行GUI

  • 在GUIDE中,保存你的GUI,它将生成一个.fig文件(保存布局信息)和一个.m文件(包含初始化代码和回调函数)。
  • 在App Designer中,直接保存并运行你的App。
  • 运行.m文件或App,以查看和测试你的GUI。

7. 调试和优化

  • 使用MATLAB的调试工具来识别和修复任何错误或问题。
  • 根据需要调整布局、颜色、字体等,以优化GUI的用户体验。 

gui视频

注意事项:

  • 命名规范:为控件和回调函数选择描述性的名称,以提高代码的可读性。
  • 注释:在代码中添加注释,解释每个控件和回调函数的作用,以便于后期维护和修改。
  • 用户体验:考虑界面的易用性和美观性,确保用户能够轻松理解和使用你的GUI。

通过遵循以上步骤和注意事项,你可以使用MATLAB创建功能强大且用户友好的GUI界面。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/535869.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

微服务demo(四)nacosfeigngateway(2)gatewayspringsercurity

一、思路 1、整体思路 用户通过客户端访问项目时,前端项目会部署在nginx上,加载静态文件时直接从nginx上返回即可。当用户在客户端操作时,需要调用后端的一些服务接口。这些接口会通过Gateway网关,网关进行一定的处理&#xff0…

4.12学习总结·(MySQL学习总结)

1.MySQL对数据库的操作 1.展示所有数据库 show databases; 这种就是将我navicat上的所有数据库调用出来; 2.创建数据库 create database 数据库名; 很明显,我多创建了一个Teacher 的数据库 3.使用某个数据库 use 数据库名 use test1; 切换到test…

WEB漏洞-文件上传之WAF绕过及安全修复

#上传参数解析: Content-disposition:一般不可更改 Name:表单参数值,不能更改(更改需要达到统一) Filename:文件名,可以更改 Content-type:文件MIME,视情…

Linux 函数学习 poll

1、Linux poll 函数 int poll(struct pollfd *fds, nfds_t nfds, int timeout); fds&#xff1a; 需要轮询的fd集合 nfds&#xff1a;需要轮询的fds数量 timeout&#xff1a;超时时间 返回值&#xff1a;0 超时&#xff0c;<0 发生异常&#xff0c;> 0 存在数据变化 …

Unity单个物体绑定多个相机在轨道上移动,录制不同角度视频

环境搭建 下载Cinemachine插件安装 打开包管理器 下载cinemachine插件 创建轨道 使用dolly track 创建轨道 右侧可以删减关键点&#xff0c;注意调整y坐标 创建cart 把前面的轨道拖到path中&#xff0c;注意这里的speed要设定不为0才会动 设置VItual Camera 根据需…

分布式锁-redission可重入锁原理

5.3 分布式锁-redission可重入锁原理 在Lock锁中&#xff0c;他是借助于底层的一个voaltile的一个state变量来记录重入的状态的&#xff0c;比如当前没有人持有这把锁&#xff0c;那么state0&#xff0c;假如有人持有这把锁&#xff0c;那么state1&#xff0c;如果持有这把锁的…

ubuntu 20.04 更新显卡驱动

1. 问题描述 $ watch -n 1 nvidia-smi画面不动 而且运行 pytorch 代码时出现问题&#xff1a; UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 11070). Please update your GPU driver by downloading and installing a new…

《web应用技术》第三次课后练习

实验目的&#xff1a; 1、springboot入门程序撰写并启动 2、使用postman练习参数的获取。 参考&#xff1a;Day04-10. Web入门-SpringBootWeb-快速入门_哔哩哔哩_bilibili

基于Springboot的网上商品订单转手系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的网上商品订单转手系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系…

基于级联H桥的多电平逆变器PWM控制策略的simulink建模与仿真

目录 1.课题概述 2.系统仿真结果 3.核心程序与模型 4.系统原理简介 5.完整工程文件 1.课题概述 级联H桥&#xff08;CHB&#xff09;多电平逆变器是一种通过多个H桥单元级联实现更高电压等级和更高质量输出波形的电力电子转换装置。这种逆变器在高压大功率场合应用广泛&am…

开源模型应用落地-chatglm3-6b-zero/one/few-shot-入门篇(五)

一、前言 Zero-Shot、One-Shot和Few-Shot是机器学习领域中重要的概念&#xff0c;特别是在自然语言处理和计算机视觉领域。通过Zero-Shot、One-Shot和Few-Shot学习&#xff0c;模型可以更好地处理未知的情况和新任务&#xff0c;减少对大量标注数据的依赖&#xff0c;提高模型的…

Mac M2安装 Windows

由于需要在 Windows 上使用一些软件&#xff0c;今天在 Mac M2 上安装了 Windows 11。以前在 X86 Mac 上安装很容易&#xff0c;都是 X86 架构随便找个镜像安装上就可以用了。到了 M1/M2 Arm 架构就会麻烦一些&#xff0c;先在网上找到 Windows 10 Arm 架构的安装镜像&#xff…

3. WPF应用程序启动时StartUp事件和重写OnStartup方法有什么不同

文章目录 一. 目标二. 技能介绍① OnStartup方法介绍② Startup事件介绍 三. 结论① 不同点1: 设计意图② 不同点2: 执行时机 一. 目标 搞明白WPF应用中OnStartup() 方法的作用和用法搞明白WPF应用中StartUp事件的作用和用法搞明白为什么同时需要这两个功能?它们设计上有所重叠…

程序猿之路

我接触计算机算对自己来说是比较晚的了&#xff0c;上初中的时候就有微机课&#xff0c;但是在那个小县城&#xff0c;上课也只是3个人共用一个电脑&#xff0c;我初中整个过程只会开关机&#xff0c;哈哈&#xff0c;虽然学过word&#xff0c;但是无奈&#xff0c;我插不上手呀…

vue中预览docx、xlsx、pptx、pdf

前言&#xff1a;其实本来是要做全类型文件预览的&#xff0c;但是一直找不到合适的doc,xlx,ppt预览插件。要是有可以使用的&#xff0c;可以评论推荐给我 我使用的node版本&#xff1a;v18.19.1 参考官网&#xff1a;preview 文件预览 | ran 引入方式&#xff1a; //安装组…

20232831 2023-2024-2 《网络攻防实践》第5次作业

目录 20232831 2023-2024-2 《网络攻防实践》第5次作业1.实验内容&#xff08;1&#xff09;防火墙配置&#xff08;具体IP配置参考自己的IP设置&#xff09;&#xff08;2&#xff09;动手实践&#xff1a;Snort&#xff08;3&#xff09;分析配置规则 2.实验过程3.学习中遇到…

【论文阅读——SplitFed: When Federated Learning Meets Split Learning】

级别CCFA 1.摘要 联邦学习&#xff08;FL&#xff09;和分割学习&#xff08;SL&#xff09;是两种流行的分布式机器学习方法。两者都采用了模型对数据的场景&#xff1b;客户端在不共享原始数据的情况下训练和测试机器学习模型。由于机器学习模型的架构在客户端和服务器之间…

HWOD:二维数组下标合法性判断

一、知识点 1、停止读取的判定条件有两种写法 (1)&#xff1a;while(scanf()!EOF) (2)&#xff1a;while(scanf()>0) 在代码提交平台(1)和(2)都可以用 在本地&#xff0c;只能用(2)&#xff0c;且要有非法输入的配合。比如要读取的是整数&#xff0c;但输入了字符串&am…

微信小程序自定义关闭按钮在弹窗下面的效果

效果图: 我之前用vant 的popup的弹窗写&#xff0c;会出现close图标移动到弹窗内容外部不可见。 自定义代码&#xff1a; popup.JS/*** 生命周期函数--监听页面初次渲染完成*/onReady() {//自定义弹窗 动态获取屏幕高度var that this;wx.getSystemInfo({success: (result) &…

如何排查k8s集群中Pod内mysqld进程占用内存消耗过高?

文章目录 1. **查看容器资源使用情况**&#xff1a;2. **进入容器内部**&#xff1a;3. **检查进程内存使用**&#xff1a;4. **MySQL服务器状态检查**&#xff1a;5. **MySQL日志分析**&#xff1a;6. **使用专门的MySQL监控工具**&#xff1a;7. **配置文件检查**&#xff1a…