7

我想创建一个选项卡式 GUI,其中第一个选项卡用于读取输入,然后输入显示在 GUI 上。用户应该能够从 GUI 中选择数据,然后作为算法的输入。用户也可以在另一个选项卡中选择算法的参数。在第三个选项卡中,用户可以看到结果图。

如何以编程方式或使用 GUIDE 在 MatLab 中创建选项卡式 GUI?

4

2 回答 2

14

这是一个使用半文档化函数 UITAB 创建选项卡的简单示例:

function tabbedGUI()
    %# create tabbed GUI
    hFig = figure('Menubar','none');
    s = warning('off', 'MATLAB:uitabgroup:OldVersion');
    hTabGroup = uitabgroup('Parent',hFig);
    warning(s);
    hTabs(1) = uitab('Parent',hTabGroup, 'Title','Data');
    hTabs(2) = uitab('Parent',hTabGroup, 'Title','Params');
    hTabs(3) = uitab('Parent',hTabGroup, 'Title','Plot');
    set(hTabGroup, 'SelectedTab',hTabs(1));

    %# populate tabs with UI components
    uicontrol('Style','pushbutton', 'String','Load data...', ...
        'Parent',hTabs(1), 'Callback',@loadButtonCallback);
    uicontrol('Style','popupmenu', 'String','r|g|b', ...
        'Parent',hTabs(2), 'Callback',@popupCallback);
    hAx = axes('Parent',hTabs(3));
    hLine = plot(NaN, NaN, 'Parent',hAx, 'Color','r');

    %# button callback
    function loadButtonCallback(src,evt)
        %# load data
        [fName,pName] = uigetfile('*.mat', 'Load data');
        if pName == 0, return; end
        data = load(fullfile(pName,fName), '-mat', 'X');

        %# plot
        set(hLine, 'XData',data.X(:,1), 'YData',data.X(:,2));

        %# swithc to plot tab
        set(hTabGroup, 'SelectedTab',hTabs(3));
        drawnow
    end

    %# drop-down menu callback
    function popupCallback(src,evt)
        %# update plot color
        val = get(src,'Value');
        clr = {'r' 'g' 'b'};
        set(hLine, 'Color',clr{val})

        %# swithc to plot tab
        set(hTabGroup, 'SelectedTab',hTabs(3));
        drawnow
    end
end

tab1 tab2 tab3

于 2012-06-18T18:34:01.583 回答
3

您还可以借助我编写的Matlab File Exchange提供的实用程序从 GUIDE 创建的 GUI 创建选项卡。

用法相当简单:

  1. Create a pane with tag set to Tab? where ? is any letter or number (e.g. TabA). This main pane should be left empty and determines the size and location of the tab group (uitabgroup).
  2. Create additional panes with a tag name that starts with the name of the main pane. All other controls should be added to these panes.
  3. In the Guide generated function xxx_OpeningFcn add the following:

    handles.tabManager = TabManager( hObject );

The location of the additional panes is not important but it is generally easier to edit the GUI if they are in the same location as the main pane. You can edit the panes even if they are overlaid by cycling through the panes with the "Send to back" command from the Guide pop up menu.

Tab Group Place holderMain Tab Supplementary TabResulting GUI

于 2016-01-19T08:19:04.323 回答