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});
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 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界面。

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于java的社区生活超市管理系统

开发语言:Java 框架:ssm 技术:JSP JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5.7(一定要5.7版本) 数据库工具:Navicat11 开发软件:eclipse/myeclip…

STM32之DHT11温湿度传感器

目录 一 DHT11温湿度传感器简介 1.1 传感器特点 1.2 传感器特性 1.3 传感器引脚说明 二 测量原理及方法 2.1 典型应用电路 2.2 单线制串行简介 2.2.1 串行接口 (单线双向) 2.2.2 数据示例 2.3 通信时序 三 单片机简介 3.1 STM32F103C8T6最小系统板 四 接线说明 …

计算机网络 实验指导 实验17

实验17 配置无线网络实验 1.实验拓扑图 Table PC0 和 Table PC1 最开始可能还会连Access Point0,无影响后面会改 名称接口IP地址网关地址Router0fa0/0210.10.10.1fa0/1220.10.10.2Tablet PC0210.10.10.11Tablet PC1210.10.10.12Wireless互联网220.10.10.2LAN192.16…

YOLOv9/YOLOv8算法改进【NO.117】 使用Wasserstein Distance Loss改进小目标的检测效果

前 言 YOLO算法改进系列出到这,很多朋友问改进如何选择是最佳的,下面我就根据个人多年的写作发文章以及指导发文章的经验来看,按照优先顺序进行排序讲解YOLO算法改进方法的顺序选择。具体有需求的同学可以私信我沟通: 首推…

Blender表面细分的操作

在使用Blender的过程中,刚开始创建的模型,都会比较少面,这样操作起来比较流畅,减少电脑的计算量,当设计快要完成时,就会增加表面细分,这样更加圆滑,看起来更加顺眼。 比如创建一个猴头,它会默认显示如下: 从上图可以看到,有一些表面会比较大,棱角很多。 这时候你…

02 MySQL 之 DQL专题

3. 数据库中仅有月薪字段(month_salary),要求查询所有员工的年薪,并以年薪(year_salary)输出: 分析: 查询操作中,字段可以参与数学运算as 起别名,但实际上可以省略 #以下两句效果…

Linux的文件操作中的静态库的制作

Linux操作系统支持的函数库分为: 静态库,libxxx.a,在编译时就将库编译进可执行程序中。 优点:程序的运行环境中不需要外部的函数库。 缺点:可执行程序大 (因为需要 编译) 动态库&#xff0c…

Unity 人形骨骼动画模型嘴巴张开

最近搞Daz3D玩,导入后挂上动画模型嘴巴张开,其丑无比。 Google了一下,得知原因是Unity没有对下巴那根骨骼做控制,动画系统就会把它放到默认的位置,嘴巴就张开了。找到了3种解决办法。 1.移除动画中对下巴这个骨骼的转…

stm32报错问题集锦

PS:本文负责记录本人日常遇到的报错问题,以及问题描述、原因以及解决办法等,解决办法百分百亲测有效。本篇会不定期更新,更新频率就看遇到的问题多不多了 更换工程芯片型号 问题描述 例程最开始用的芯片型号是STM32F103VE&#…

stm32开发之threadx+modulex组合开发使用记录

前言 参考博客 论坛官方资料: 微软开发板核心芯片使用的是stm32f407zgtx,烧录工具使用的是jlink模块的构建使用的是脚本进行构建网上针对modulex的资料较少,这里做个记录 项目结构 逻辑框架 主程序代码 主函数 /** Copyright (c) 2024-2024&#xff0…

【Web】VS Code 插件及快捷键

专栏文章索引:Web 有问题可私聊:QQ:3375119339 目录 一、安装步骤 二、插件 1.Chinese (Simplified) (简体中文) 2.open in browser 3.vscode-icons 4.Live Server 5.Live Server Preview 6.翻译(英汉词典) 三、快捷键 1.缩放代码…

【SERVERLESS】AWS Lambda上实操

通过Serverless的发展历程及带给我们的挑战,引出我们改如何改变思路,化繁为简,趋利避害,更好的利用其优势,来释放企业效能,为创造带来无限可能。 一 Serverless概述 无服务器计算近年来与云原生计算都是在…