Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Programmatically Create a User Interface with Tabbed Panels

This example shows how to create a user interface with tab panels in MATLAB®.

Objects Used to Create Tab Panels

The tabgroup and tab objects are used to build user interfaces with tab panels. A tabgroup object is created using the uitabgroup function. A tab object is created using the uitab function with a tabgroup as its parent. The tab objects appear in the order in which they are created.

f = figure;
tgroup = uitabgroup('Parent', f);
tab1 = uitab('Parent', tgroup, 'Title', 'Loan Data');
tab2 = uitab('Parent', tgroup, 'Title', 'Amortization Table');
tab3 = uitab('Parent', tgroup, 'Title', 'Principal/Interest Plot');

Like all graphics objects, tabgroups and tabs have properties that you can view and modify. These properties have default values. The display of a tabgroup or a tab shows its most commonly used properties.

tgroup
tgroup = 
  TabGroup with properties:

            SelectedTab: [1x1 Tab]
    SelectionChangedFcn: ''
            TabLocation: 'top'
               Position: [0 0 1 1]
                  Units: 'normalized'

  Show all properties

tab1
tab1 = 
  Tab (Loan Data) with properties:

              Title: 'Loan Data'
    BackgroundColor: [0.9400 0.9400 0.9400]
           Position: [0.0036 0.0071 0.9911 0.9357]
              Units: 'normalized'

  Show all properties

Get Individual Tabgroup and Tab Properties

To access individual properties, use the dot notation syntax object.Property. For example, return the TabLocation property of the tabgroup.

tgroup.TabLocation
ans = 
'top'

Change Tabgroup and Tab Properties

To customize the look of a tabgroup or tab object, change any property value using the dot notation syntax object.Property.

tab3.ForegroundColor = 'blue';
tgroup.SelectedTab = tab1;

Add Components to Tab

Tabs contain user interface components such as labels, buttons, and edit boxes. To add a component to a tab, create the component and set its parent to the tab.

lblLoanAmount = uicontrol('Parent', tab1, 'Style', 'text', 'String', 'Loan Amount', ...
  'HorizontalAlignment', 'left', 'Position', [80 320 170 25]) ;
edtLoanAmount = uicontrol('Parent', tab1, 'Style', 'edit', ...
 'Position', [224 320 200 30]) ;

Create a Callback

The tabgroup object has one callback called SelectionChangedFcn. This callback function is called when the user changes the currently selected tab.

tgroup.SelectionChangedFcn = @tabChangedCB;

For example, if you want the application to recalculate the amortization table and update the plot when the user leaves the "Loan Data" tab, then set the SelectionChangeCallback to a function with this format:

function tabChangedCB(src, eventdata)

% Get the Title of the previous tab

tabName = eventdata.OldValue.Title;

% If 'Loan Data' was the previous tab, update the table and plot

if strcmp(tabName, 'Loan Data')

% <insert code here to update the amortization table and plot>

end

end

Get All Tabgroup and Tab Properties

Graphics objects in MATLAB have many properties. To see all the properties of a tabgroup or tab object, use the get command.

get(tgroup)
           BeingDeleted: 'off'
             BusyAction: 'queue'
          ButtonDownFcn: ''
               Children: [3x1 Tab]
              CreateFcn: ''
              DeleteFcn: ''
       HandleVisibility: 'on'
          InnerPosition: [0 0 1 1]
          Interruptible: 'on'
          OuterPosition: [0 0 1 1]
                 Parent: [1x1 Figure]
               Position: [0 0 1 1]
            SelectedTab: [1x1 Tab]
    SelectionChangedFcn: @tabChangedCB
         SizeChangedFcn: ''
            TabLocation: 'top'
                    Tag: ''
                   Type: 'uitabgroup'
          UIContextMenu: [0x0 GraphicsPlaceholder]
                  Units: 'normalized'
               UserData: []
                Visible: 'on'
get(tab3)
     BackgroundColor: [0.9400 0.9400 0.9400]
        BeingDeleted: 'off'
          BusyAction: 'queue'
       ButtonDownFcn: ''
            Children: [0x0 GraphicsPlaceholder]
           CreateFcn: ''
           DeleteFcn: ''
     ForegroundColor: [0 0 1]
    HandleVisibility: 'on'
       InnerPosition: [0.0036 0.0071 0.9911 0.9357]
       Interruptible: 'on'
       OuterPosition: [0.0036 0.0071 0.9911 0.9357]
              Parent: [1x1 TabGroup]
            Position: [0.0036 0.0071 0.9911 0.9357]
      SizeChangedFcn: ''
                 Tag: ''
               Title: 'Principal/Interest Plot'
       TooltipString: ''
                Type: 'uitab'
       UIContextMenu: [0x0 GraphicsPlaceholder]
               Units: 'normalized'
            UserData: []
Was this topic helpful?