Matlab GUI界面设计

article/2025/10/6 19:11:28

摘要:本篇博文基于MATLAB2014a进行GUI设计。

1.启动GUI设计

命令行输入guide,回车。

即可得到下面的对话框,进行相关的选择和设置

点击确定,得到两个文件,一个是.m文件,另一个是.fig文件,需要说明的是,.m文件用于编辑GUI中控件所需要的回调代码,.fig文件可以用鼠标拖拽等比较简单的操作进行初始的界面设计。

2..fig文件控件布局

本篇暂时涉及按钮、可编辑文本、静态文本、弹出式菜单、轴的实现,拖拽这些控件到界面中,可以点击绿色三角形运行GUI看看效果。

双击任意控件可以弹出控件的属性检查器,这里可以更改控件的初始属性,并且可以查看控件的tag值,用于回调程序的句柄调用。

3.GUI初始程序编写

3.1.初始程序是界面运行时最先执行的程序,用于对控件等的一些初始设置,该部分的代码应该添加在.m文件的test_OpeningFcn(hObject, eventdata, handles, varargin)函数中。

插入如下代码,使得可编辑文本失效

set(handles.edit1,'enable','off');

3.2.按钮程序编写,使得可编辑文本生效,右击按钮->查看回调->Callback,输入

set(handles.edit1,'enable','on');

3.3.可编辑文本程序编写,当可编辑文本框中输入文字后,在静态文本中显示出来

右击可编辑文本->查看回调->Callback,输入

set(handles.text1,'string',get(handles.edit1,'string'));

3.4.弹出式菜单编写

双击弹出式菜单,调出属性编辑器,进行以下操作

             

输入相应文字,点击确定。

该部分函数编写,右击弹出式菜单->查看回调->Callback,输入

3.5.轴部分程序编写

为了简单起见,这部分内容写在程序开头,作用是显示了一个正弦曲线

代码如下:

axes(handles.axes1)
t=0:0.001:4*pi;
f=sin(t);
plot(t,f,'g')
axis([0 4*pi -1 1])
grid on
xlabel('t')
ylabel('sin(t)')
title('正弦函数图像')
legend('f=sin(t)')

至此,基本功能均可实现

4.总结

①每个控件均可在属性编辑器里面设置初始值;

②如果需要全局变量,在定义和使用的时候都需要写关键字global。

5.附上全部代码。

function varargout = test(varargin)
% TEST MATLAB code for test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST.M with the given input arguments.
%
%      TEST('Property','Value',...) creates a new TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_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 test% Last Modified by GUIDE v2.5 26-Jan-2017 16:20:09% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @test_OpeningFcn, ...'gui_OutputFcn',  @test_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 test is made visible.
function test_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 test (see VARARGIN)
set(handles.edit1,'enable','off');
axes(handles.axes1)
t=0:0.001:4*pi;
f=sin(t);
plot(t,f,'g')
axis([0 4*pi -1 1])
grid on
xlabel('t')
ylabel('sin(t)')
title('正弦函数图像')
legend('f=sin(t)')
% Choose default command line output for test
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = test_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)
% 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)
set(handles.edit1,'enable','on');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)
set(handles.text1,'string',get(handles.edit1,'string'));
% 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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(handles.popupmenu1, 'String');
val = get(handles.popupmenu1,'Value');
switch str{val};
case '选项一'set(handles.text1,'string','选项一触发');
case '选项二'set(handles.text1,'string','选项二触发');
case '选项三'set(handles.text1,'string','选项三触发');
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu 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

相关下载如下:

test.fig

test.m


 

欢迎交流指正。。。
 


 


http://chatgpt.dhexx.cn/article/5evVZJ8T.shtml

相关文章

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;…

idea__MyBatis框架08——连接池(POOLED 跟 UNPOOLED )

一、继续上一节&#xff0c;把一些不用的注释给清理掉&#xff0c;看一下我们的mybatis主配置文件&#xff0c;重点看type。 二、Type详细介绍&#xff1a; mybatis连接池提供了3种方式的配置&#xff1a; type属性就是表示采用何种连接池方式。type属性的取值&#xff1a;POOL…

PODNet: Pooled Outputs Distillation for Small-Tasks Incremental Learning论文详解ECCV2020

ECCV2020 论文地址&#xff1a;https://doi.org/10.1007/978-3-030-58565_6 代码地址&#xff1a;https://github.com/arthurdouillard/incremental learning.pytorch 目录 1.贡献点 2.方法 2.1 pool类型 2.2 POD&#xff08;Pooled Outputs Distillation&#xff09;方…

mybatis数据源(JNDI、POOLED、UNPOOLED)源码详解

一、概述 二、创建 mybatis数据源的创建过程稍微有些曲折。 1. 数据源的创建过程&#xff1b; 2. mybatis支持哪些数据源&#xff0c;也就是dataSource标签的type属性可以写哪些合法的参数&#xff1f; 弄清楚这些问题&#xff0c;对mybatis的整个解析流程就清楚了&#xff0c;…

XAConnectionFactory: failed to create pooled connection - DBMS down or unreachable 的解决方法

问题描述 项目启动出现报错&#xff1a;XAConnectionFactory: failed to create pooled connection - DBMS down or unreachable? 原因分析&#xff1a; Druid连接池问题&#xff0c;当Druid与Atomikos搭配时&#xff0c;如果MySQL版本高于8.0.11则不被支持 查看数据库使用…

没有手动提交事务,Mybatis 的 POOLED 连接池炸了

错误原因&#xff1a; 事务不关&#xff0c;并且非事务交替进行 总的来说&#xff0c;就是先开启了事务连接&#xff0c;未提交或关闭&#xff0c;导致连接池连接全部占满。 此时进行一次非事务连接操作&#xff0c;但是因为此时已经没有可以空闲的连接&#xff0c;并且创建的连…

【Flink】报错 No pooled slot available and request to ResourceManager for new slot failed

文章目录 1.场景11.1 概述1.2 问题1.场景1 1.1 概述 改报错请参考:【Flink】Flink 1.9 升级 到 flink 1.12.4 报错 shaded netty4 AbstractChannel AnnotatedConnectException 错误描述 报错信息: java.util.concurrent.CompletionException:

Oracle 关于Pooled connection request timed out

发生场景&#xff1a; 系统异常卡死&#xff0c;报错&#xff1a; 通过查找日志和业务接口定位&#xff0c;是因为数据库连接池溢出导致链接不上&#xff0c;系统卡死 异常测试代码如下格式&#xff1a; 测试了一个1000次的链接&#xff0c;每次连接都持续30秒&#xff0c;链…

MyBatis POOLED连接池深入了解

往期内容&#xff0c;如下 一、MyBatis简介 二、MyBatis环境搭建 三、MyBatis入门案例 四、MyBatis自定义 五、MyBatis CRUD操作 六、Mybatis中参数和返回值的深入了解 七、MyBatis 配置文件标签 我们在实际开发中都会使用连接池&#xff0c;因为它可以减少我们获取连接所消耗的…

unpooled与pooled

unpooled每次都是重新获取一个连接&#xff0c;底层源码如下 pooled去判断有没有&#xff0c;有就拿出来用&#xff0c;没有就创建新的&#xff0c;每次用完再还回去 mybatis poolde连接池原理 先去看空闲的有没&#xff0c;有就直接用&#xff0c;没有就去活动连接池里把最老…

.NET性能优化-推荐使用Collections.Pooled

简介 性能优化就是如何在保证处理相同数量的请求情况下占用更少的资源&#xff0c;而这个资源一般就是CPU或者内存&#xff0c;当然还有操作系统IO句柄、网络流量、磁盘占用等等。但是绝大多数时候&#xff0c;我们就是在降低CPU和内存的占用率。 之前分享的内容都有一些局限性…

使用 TFDConnection 的 pooled 连接池

使用 TFDConnection 的 pooled 连接池 从开始看到这个属性&#xff0c;就一直认为他可以提供一个连接池管理功能&#xff0c; 苦于文档资料太少&#xff0c; 甚至在帮助中对该属性的使用都没有任何介绍&#xff0c;如果你搜索百度&#xff0c;也会发现基本没资料。 最后终于在…