matlab練習(創建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展示

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

總結
我們有三種不同方式在matlab中創建簡單的GUI程序,其中比較推薦的是使用編程的方式。GUI編程的主要思想是面向對象編程,每個對象有自己的方法和屬性,我們可以通過對象的句柄操作該對象,在GUI編程中還往往用到事件(比如點擊事件)和回調函數,某一事件發生時,GUI內部調用相應的回調函數,從而展示新的輸入,達到交互式的效果,當然在更復雜的GUI系統中還需要更多精細的設計


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM