Documentation Center

  • Trial Software
  • Product Updates

Set Tab Order in a Programmatic GUI

How Tabbing Works

A GUI's tab order is the order in which components of the GUI acquire focus when a user presses the keyboard Tab key. Focus is generally denoted by a border or a dotted border.

Tab order is determined separately for the children of each parent. For example, child components of the GUI figure have their own tab order. Child components of each panel or button group also have their own tab order.

If, in tabbing through the components at one level, a user tabs to a panel or button group, then the tabbing sequences through the components of the panel or button group before returning to the level from which the panel or button group was reached. For example, if a GUI figure contains a panel that contains three push buttons and the user tabs to the panel, then the tabbing sequences through the three push buttons before returning to the figure.

    Note   You cannot tab to axes and static text components. You cannot determine programmatically which component has focus.

Default Tab Order

The default tab order for each level is the order in which you create the components at that level.

The following code creates a GUI that contains a pop-up menu with a static text label, a panel with three push buttons, and an axes.

fh = figure('Position',[200 200 450 270]);
pmh = uicontrol(fh,'Style','popupmenu',...
           'Position',[290 200 130 20]);
sth = uicontrol(fh,'Style','text','String','Select Data',...
           'Position',[290 230 60 20]);
ph = uipanel('Parent',fh,'Units','pixels',...
           'Position',[290 30 130 150]);
ah = axes('Parent',fh,'Units','pixels',...
           'Position',[40 30 220 220]);
bh1 = uicontrol(ph,'Style','pushbutton',...
           'String','Contour','Position',[20 20 80 30]);
bh2 = uicontrol(ph,'Style','pushbutton',...
           'String','Mesh','Position',[20 60 80 30]);
bh3 = uicontrol(ph,'Style','pushbutton',...
           'String','Surf','Position',[20 100 80 30]);

You can obtain the default tab order for a figure, panel, or button group by retrieving its Children property. For the example, the statement is

ch = get(ph,'Children')

where ph is the handle of the panel. This statement returns a vector containing the handles of the children, the three push buttons.

ch =

These handles correspond to the push buttons as shown in the following table:


Handle Variable

Push Button










The default tab order of the push buttons is the reverse of the order of the child vector: Contour > Mesh > Surf.

    Note   The get function returns only those children whose handles are visible, i.e., those with their HandleVisibility property set to on. Use allchild to retrieve children regardless of their handle visibility.

In the example GUI figure, the default order is pop-up menu followed by the panel's Contour, Mesh, and Surf push buttons (in that order), and then back to the pop-up menu. You cannot tab to the axes component or the static text component.

Try modifying the code to create the pop-up menu following the creation of the Contour push button and before the Mesh push button. Now execute the code to create the GUI and tab through the components. This code change does not alter the default tab order. This is because the pop-up menu does not have the same parent as the push buttons. The figure is the parent of the panel and the pop-up menu.

Change the Tab Order

Use the uistack function to change the tab order of components that have the same parent. A convenient syntax for uistack is


where h is a vector of handles of the components whose tab order is to be changed.

stackopt represents the direction of the move. It must be one of the strings: up, down, top, or bottom, and is interpreted relative to the column vector returned by the statement:

ch = get(ph,'Children')

ch =

If the tab order is currently Contour > Mesh > Surf, the statement


moves bh2 (Mesh) up one place in the vector of children and changes the tab order to Contour > Surf > Mesh.

ch = get(ph,'Children')

now returns

ch =

step is the number of levels changed. The default is 1.

    Note   Tab order also affects the stacking order of components. If components overlap, those that appear lower in the child order, are drawn on top of those that appear higher in the order. If the push buttons in the example overlapped, the Contour push button would be on top.

Was this topic helpful?