matlab练习(创建GUI)

摘要:
GUI编程的主要思想是面向对象编程。每个对象都有自己的方法和属性,我们可以通过对象句柄进行操作

matlab创建GUI

方法1:使用GUIDE菜单式操作

在matlab中输入guide,可以打开guide创建GUI的图形界面,按菜单操作即可

注:matlab未来版本可能会取消掉这种方式

方法2:编写代码创建GUI
下面是一个简单的以代码方式创建GUI的例子,其中关键的一些点包括
1. 创建一个figure object作为container
2. 通过`uicontrol`创建container内的控件,通过`'Callback'`属性关联回调函数
3. 回调函数参数一般是由两部分组成`(source,eventdata,handles)`,分别表示引起回调产生的控件的handle和(点击)事件的数据

function simple_gui2
% SIMPLE_GUI2 Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.

% Create and then hide the UI as it is being constructed.
f = figure('Visible','off','Position',[360,200,450,285]);

% Construct the components.
hsurf = uicontrol('Style','pushbutton','String','Surf',...
'Position',[315,220,70,25],...
'Callback',{@surfbutton_Callback});
hmesh = uicontrol('Style','pushbutton',...
'String','Mesh','Position',[315,180,70,25],...
'Callback',@meshbutton_Callback);
hcontour = uicontrol('Style','pushbutton',...
'String','Contour','Position',[315,135,70,25],...
'Callback',@contourbutton_Callback);
htext = uicontrol('Style','text','String','Select Data',...
'Position',[325,90,60,15]);
hpopup = uicontrol('Style','popupmenu',...
'String',{'Peaks','Membrane','Sinc'},...
'Position',[300,50,100,25],...
'Callback',@popup_menu_Callback);
ha = axes('Units','pixels','Position',[50,60,200,185]);
align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');
% Initialize the UI.
% Change units to normalized so components resize automatically.
f.Units = 'normalized';
ha.Units = 'normalized';
hsurf.Units = 'normalized';
hmesh.Units = 'normalized';
hcontour.Units = 'normalized';
htext.Units = 'normalized';
hpopup.Units = 'normalized';
% Generate the data to plot.
peaks_data = peaks(35);
membrane_data = membrane;
[x,y] = meshgrid(-8:.5:8);
r = sqrt(x.^2+y.^2) + eps;
sinc_data = sin(r)./r;
% Create a plot in the axes.
current_data = peaks_data;
surf(current_data);
% Assign a name to appear in the window title.
f.Name = 'Simple GUI';
% Move the window to the center of the screen.
movegui(f,'center')
% Make the UI visible.
f.Visible = 'on';

% Pop-up menu callback. Read the pop-up menu Value property to
% determine which item is currently displayed and make it the
% current data. This callback automatically has access to
% current_data because this function is nested at a lower level.
  function popup_menu_Callback(source,eventdata)
  % Determine the selected data set.
  str = source.String;
  val = source.Value;
  % Set current data to the selected data set.
  switch str{val};
  case 'Peaks' % User selects Peaks.
    current_data = peaks_data;
  case 'Membrane' % User selects Membrane.
    current_data = membrane_data;
  case 'Sinc' % User selects Sinc.
    current_data = sinc_data;
  end
  end

% Push button callbacks. Each callback plots current_data in the
% specified plot type.
  function surfbutton_Callback(source,eventdata)
  % Display surf plot of the currently selected data.
  surf(current_data);
  end
  function meshbutton_Callback(source,eventdata)
  % Display mesh plot of the currently selected data.
  mesh(current_data);
  end
  function contourbutton_Callback(source,eventdata)
  % Display contour plot of the currently selected data.
  contour(current_data);
  end
end

GUI展示

matlab练习(创建GUI)第1张

matlab练习(创建GUI)第2张

方法3:利用AppDesigner
和方法一的Guide类似

总结
我们有三种不同方式在matlab中创建简单的GUI程序,其中比较推荐的是使用编程的方式。GUI编程的主要思想是面向对象编程,每个对象有自己的方法和属性,我们可以通过对象的句柄操作该对象,在GUI编程中还往往用到事件(比如点击事件)和回调函数,某一事件发生时,GUI内部调用相应的回调函数,从而展示新的输入,达到交互式的效果,当然在更复杂的GUI系统中还需要更多精细的设计

免责声明:文章转载自《matlab练习(创建GUI)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JS的"多线程"python开发模块基础:collections模块&paramiko模块下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

openlayers4 入门开发系列结合 echarts4 实现散点图(附源码下载)

前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子,这个也是学习 openlayers4 的好素材。 openlayers4 入门开发系列的地图服务基于 Geoserver 发布的,关于 Geoserver...

vue el-table 自适应表格内容宽度

由于表头和列是分开渲染的,通过el-table 设置fit属性,只能撑开表头,但是没有办法根据列的内容去适应宽度。网上找了一些使用根据表格内容计算表头宽度的文章,记个笔记。 代码逻辑是通过vue 的watch 监控表格的数据data,计算每列的内容和表头的最大宽度,计算的时候把表格内容使用span标签包裹,然后计算span标签的宽度width:px,然后再...

dom 绑定数据

一、绑定/修改 .jQuery修改属性值,都是在内存中进行的,并不会修改 DOM 1. 对象绑定$(selector).data(name) $("#form").data("name") 2. dom 绑定 $.data(element,name, val); jQuery.data($("#form")[0], "testing", 123); 3....

swf文件加密基础(转)

本来打算下班回来就写这个东西,一方面算是对今天学习的一个笔记记录,另外一方面,给一些朋友普及一些swf文件加密基础知识。之所以说是基础,那是因为我也是刚学习了一点,灰常的基础。不过晚上看了一会我是传奇的视频耽误了,话说郭德纲老是调戏谢楠,难道有基情?不解释,呵呵……       在说明加密解密方法之前,先解释一些理论方面的东西,很草根。     swf加...

如何使用php生成唯一ID的4种方法

php生成唯一ID的应用场景非常普遍,如临时缓存文件名称,临时变量,临时安全码等,uniqid()函数基于以微秒计的当前时间,生成一个唯一的 ID。由于生成唯一ID与微秒时间关联,因此ID的唯一性非常可靠。 生成的唯一ID默认返回的字符串有 13 个字符串长,如果再结合md5()函数,生成的唯一ID可靠性将更高,这种生成的ID比随机性的ID 最大优点在于可...

Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-利用GPU并行执行MATLAB程序

1  MATLAB原文:   如果所有你想使用的函数支持GPU,你能够使用gpuArray把输入的数据传输到GPU,也能够唤起gather命令把传输值GPU的数据回收。 2  通过gpuDevice命令观察当前电脑的GPU设备 >> gpuDevice ans = CUDADevice (具有属性):...