科研菜鸟分享 http://blog.sciencenet.cn/u/sanshiphy 聚沙成塔,集腋成裘 leilphy@gmail.com

博文

Matlab:学习GUI(一个简单的例子)

已有 10938 次阅读 2015-2-28 21:43 |个人分类:技术备忘录|系统分类:科研笔记| MATLAB, gui

GUI是graphical user interface的简称,说白了就是我们常见的对话框。


下面是Matlab提供的一个简单例子,博主加上了自己的注释,供参考。


function hpopup=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.


%  Initialize and hide the GUI as it is being constructed.

% 生成图形面板,'visible'('on''off')是/否显示面板,'position' [left,bottom,width,height]

% 定义面板在显示屏上的位置(left,距离屏幕左边界的距离;bottom,距离屏幕下边界的距离)

% 以及面板的大小(width,面板宽;height,面板高,不包括title bar, menu bar, tool bars, and outer edges)

% The left and bottom elements can be negative on systems that have more

% than one monitor. 缺省单位是pixels.

f = figure('Visible','off','Position',[360,500,450,285]);


%%%%%%%%%与figure相关的控制语句%%%%%%%%%%%%%%

% Assign the GUI a name to appear in the window title.

set(f,'Name','Simple GUI')

% Move the GUI to the center of the screen.

movegui(f,'center')

% Make the GUI visible.

set(f,'Visible','on');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Construct the components.

% 利用uicontrol添加push button. 'Style',添加对象类型,还可添加pop-up menu,static text等类型.

% 'String'('Text'),push button 上要显示的文字. 'position'同figure.

% 'Callback',当用户按下button或选择pop-up menu时,Matlab执行的函数.

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});

hpopup = uicontrol('Style','popupmenu',...

          'String',{'Peaks','Membrane','Sinc'},...

          'Position',[300,50,100,25],'Callback',{@popup_menu_Callback});


htext  = uicontrol('Style','text','String','Select Data',...

          'Position',[325,90,75,15]);

% 利用uicontrol添加static text


ha = axes('Units','pixels','Position',[50,60,200,185]);

% 添加坐标轴以显示输出结果,注意用'Units'指定Position的单位是'pixels',以与其它对象保持一致.


align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');

% 水平中心对齐push button、pop-up mesh以及static text,垂直方向不变动

% align(HandleList,'HorizontalAlignment','VerticalAlignment')


% Initialize the GUI.

% Change units to normalized so components resize automatically.

set([ha,hsurf,hmesh,hcontour,htext,hpopup],'Units','normalized');

% Make the GUI behave properly when it is resized by changing the component

% and figure units to normalized. This causes the components to resize when the GUI is resized.

% Normalized units map the lower-left corner of the figure window to (0,0)

% and the upper-right corner to (1.0, 1.0).

% 将面板放大的时候,各对象(axesbuttonspop-up menustatic text等)同比例放大


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


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

%  当用户触发popup menu时,句柄hpopup的值传给source

%  str=get(source, 'String') str是3*1的cell,即pop-up menu的string{'Peaks','Membrane','Sinc'}

%  val = get(source,'Value') val的值记录了用户触发了那个按钮,1对应'peaks',2对应的是'Membrane',3对应的是'Sinc'

  function popup_menu_Callback(source,eventdata)

     % Determine the selected data set.

     str = get(source, 'String');

     val = get(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




https://blog.sciencenet.cn/blog-200199-870822.html

上一篇:Campbell 型超声风速仪的超声虚温
下一篇:Matlab: 学习GUI (使用GUIDE时需注意的几个问题)
收藏 IP: 123.150.208.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-1 07:05

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部