Adding Components to the GUI

Other topics that may be of interest:

Available Components

The component palette at the left side of the Layout Editor contains the components that you can add to your GUI. You can display it with or without names.

When you first open the Layout Editor, the component palette contains only icons. To display the names of the GUI components, select Preferences from the File menu, check the box next to Show names in component palette, and click OK.

See Creating Menus for information about adding menus to a GUI.

Component

Icon

Description

Push Button

Push buttons generate an action when clicked. For example, an OK button might apply settings and close a dialog box. When you click a push button, it appears depressed; when you release the mouse button, the push button appears raised.

Slider

Sliders accept numeric input within a specified range by enabling the user to move a sliding bar, which is called a slider or thumb. Users move the slider by clicking the slider and dragging it, by clicking in the trough, or by clicking an arrow. The location of the slider indicates the relative location within the specified range.

Radio Button

Radio buttons are similar to check boxes, but radio buttons are typically mutually exclusive within a group of related radio buttons. That is, when you select one button the previously selected button is deselected. To activate a radio button, click the mouse button on the object. The display indicates the state of the button. Use a button group to manage mutually exclusive radio buttons.

Check Box

Check boxes can generate an action when checked and indicate their state as checked or not checked. Check boxes are useful when providing the user with a number of independent choices, for example, displaying a toolbar.

Edit Text

Edit text components are fields that enable users to enter or modify text strings. Use edit text when you want text as input. Users can enter numbers but you must convert them to their numeric equivalents.

Static Text

Static text controls display lines of text. Static text is typically used to label other controls, provide directions to the user, or indicate values associated with a slider. Users cannot change static text interactively.

Pop-Up Menu

Pop-up menus open to display a list of choices when users click the arrow.

List Box

List boxes display a list of items and enable users to select one or more items.

Toggle Button

Toggle buttons generate an action and indicate whether they are turned on or off. When you click a toggle button, it appears depressed, showing that it is on. When you release the mouse button, the toggle button remains depressed until you click it a second time. When you do so, the button returns to the raised state, showing that it is off. Use a button group to manage mutually exclusive toggle buttons.

Table

Use the table button to create a table component. Refer to the uitable function for more information on using this component.

Axes

Axes enable your GUI to display graphics such as graphs and images. Like all graphics objects, axes have properties that you can set to control many aspects of its behavior and appearance. See Axes Properties in the MATLAB® Graphics documentation and commands such as the following for more information on axes objects: plot, surf, line, bar, polar, pie, contour, and mesh. See Functions — By Category in the MATLAB Function Reference documentation for a complete list.

Panel

Panels arrange GUI components into groups. By visually grouping related controls, panels can make the user interface easier to understand. A panel can have a title and various borders.

Panel children can be user interface controls and axes as well as button groups and other panels. The position of each component within a panel is interpreted relative to the panel. If you move the panel, its children move with it and maintain their positions on the panel.

Button Group

Button groups are like panels but are used to manage exclusive selection behavior for radio buttons and toggle buttons.

ActiveX® Component

ActiveX components enable you to display ActiveX controls in your GUI. They are available only on the Microsoft® Windows® platform.

An ActiveX control can be the child only of a figure, i.e., of the GUI itself. It cannot be the child of a panel or button group.

See ActiveX® Control in this document for an example. See Creating COM Objects in the MATLAB External Interfaces documentation to learn more about ActiveX controls.

Adding Components to the GUIDE Layout Area

This topic tells you how to place components in the GUIDE layout area and give each component a unique identifier.

  1. Place components in the layout area according to your design.

    The components listed in the following table need additional considerations.

    If You Are Adding...Then...
    Panels or button groupsSee Adding a Component to a Panel or Button Group.
    ActiveX controlsSee Adding ActiveX® Controls.

    See Grid and Rulers for information about using the grid.

  2. Assign a unique identifier to each component. Do this by setting the value of the component Tag properties. SeeAssigning an Identifier to Each Component for more information.

  3. Specify the look and feel of each component by setting the appropriate properties. The following topics contain specific information.

This is an example of a GUI in the Layout Editor. Components in the Layout Editor are not active. Saving and Running a GUIDE GUI describes how to generate a functioning GUI.

figure shows placing controls in the layout area

Using Coordinates to Place Components

The status bar at the bottom of the GUIDE Layout Editor displays:

If you select a single component and move it, the first two elements of the position vector (distance from left, distance from bottom) are updated as you move the component. If you resize the component, the last two elements of the position vector (width, height) are updated as you change the size. The first two elements may also change if you resize the component such that the position of its lower left corner changes. If no components are selected, the position vector is that of the figure.

For more information, see Using Coordinate Readouts.

Adding a Component to a Panel or Button Group

To add a component to a panel or button group, select the component in the component palette then move the cursor over the desired panel or button group. The position of the cursor determines the component's parent.

GUIDE highlights the potential parent as shown in the following figure. The highlight indicates that if you drop the component or click the cursor, the component will be a child of the highlighted panel, button group, or figure.

Assigning an Identifier to Each Component

Use the Tag property to assign each component a unique meaningful string identifier.

When you place a component in the layout area, GUIDE assigns a default value to the Tag property. Before saving the GUI, replace this value with a string that reflects the role of the component in the GUI.

The string value you assign Tag is used in the M-file code to identify the component and must be unique in the GUI. To set Tag:

  1. Select Property Inspector from the View menu or click the Property Inspector button .

  2. In the layout area, select the component for which you want to set Tag.

  3. In the Property Inspector, select Tag and then replace the value with the string you want to use as the identifier. In the following figure, Tag is set to mybutton.

Defining User Interface Controls

User interface controls include push buttons, toggle buttons, sliders, radio buttons, edit text controls, static text controls, pop-up menus, check boxes, and list boxes.

To define user interface controls, you must set certain properties. To do this:

  1. Use the Property Inspector to modify the appropriate properties. Open the Property Inspector by selecting Property Inspector from the View menu or by clicking the Property Inspector button .

  2. In the layout area, select the component you are defining.

Subsequent topics describe commonly used properties of user interface controls and offer a simple example for each kind of control:

Commonly Used Properties

The most commonly used properties needed to describe a user interface control are shown in the following table. Instructions for a particular control may also list properties that are specific to that control.

PropertyValueDescription
Enableon, inactive, off. Default is on.Determines whether the control is available to the user
MaxScalar. Default is 1.Maximum value. Interpretation depends on the type of component.
MinScalar. Default is 0.Minimum value. Interpretation depends on the type of component.
Position4-element vector: [distance from left, distance from bottom, width, height].Size of the component and its location relative to its parent.
StringString. Can also be a cell array or character array of strings.Component label. For list boxes and pop-up menus it is a list of the items.
Unitscharacters, centimeters, inches, normalized, pixels, points. Default is characters.Units of measurement used to interpret the Position property vector
ValueScalar or vectorValue of the component. Interpretation depends on the type of component.

For a complete list of properties and for more information about the properties listed in the table, see Uicontrol Properties in the MATLAB documentation. Properties needed to control GUI behavior are discussed in Programming a GUIDE GUI

Push Button

To create a push button with label Button 1, as shown in this figure:

Slider

To create a slider as shown in this figure:

Radio Button

To create a radio button with label Indent nested functions, as shown in this figure:

Check Box

To create a check box with label Display file extension that is initially checked, as shown in this figure:

Edit Text

To create an edit text component that displays the initial text Enter your name here, as shown in this figure:

Static Text

To create a static text component with text Select a data set, as shown in this figure:

Pop-Up Menu

To create a pop-up menu (also known as a drop-down menu or combo box) with items one, two, three, and four, as shown in this figure:

List Box

To create a list box with items one, two, three, and four, as shown in this figure:

Toggle Button

To create a toggle button with label Left/Right Tile, as shown in this figure:

Defining Panels and Button Groups

Panels and button groups are containers that arrange GUI components into groups. If you move the panel or button group, its children move with it and maintain their positions relative to the panel or button group.

To define panels and button groups, you must set certain properties. To do this:

  1. Use the Property Inspector to modify the appropriate properties. Open the Property Inspector by selecting Property Inspector from the View menu or by clicking the Property Inspector button .

  2. In the layout area, select the component you are defining.

Subsequent topics describe commonly used properties of panels and button groups and offer a simple example for each component.

Commonly Used Properties

The most commonly used properties needed to describe a panel or button group are shown in the following table:

Property

Values

Description

Position

4-element vector: [distance from left, distance from bottom, width, height].

Size of the component and its location relative to its parent.

Title

String

Component label.

TitlePosition

lefttop, centertop, righttop, leftbottom, centerbottom, rightbottom. Default is lefttop.

Location of title string in relation to the panel or button group.

Units

characters, centimeters, inches, normalized, pixels, points. Default is characters.

Units of measurement used to interpret the Position property vector

For a complete list of properties and for more information about the properties listed in the table, see the Uipanel Properties and Uibuttongroup Properties in the MATLAB reference documentation. Properties needed to control GUI behavior are discussed in theProgramming a GUIDE GUI.

Panel

To create a panel with title My Panel as shown in the following figure:

Button Group

To create a button group with title My Button Group as shown in the following figure:

Defining Axes

Axes enable your GUI to display graphics such as graphs and images using commands such as: plot, surf, line, bar, polar, pie, contour, and mesh.

To define an axes, you must set certain properties. To do this:

  1. Use the Property Inspector to modify the appropriate properties. Open the Property Inspector by selecting Property Inspector from the View menu or by clicking the Property Inspector button .

  2. In the layout area, select the component you are defining.

Subsequent topics describe commonly used properties of axes and offer a simple example.

Commonly Used Properties

The most commonly used properties needed to describe an axes are shown in the following table:

Property

Values

Description

Position

4-element vector: [distance from left, distance from bottom, width, height].

Size of the component and its location relative to its parent.

Units

normalized, centimeters, characters, inches, pixels, points. Default is normalized.

Units of measurement used to interpret position vector

For a complete list of properties and for more information about the properties listed in the table, see Axes Properties in the MATLAB documentation. Properties needed to control GUI behavior are discussed in Programming a GUIDE GUI.

See commands such as the following for more information on axes objects: plot, surf, line, bar, polar, pie, contour and mesh. See Functions — By Category in the MATLAB Function Reference documentation for a complete list.

Axes

To create an axes as shown in the following figure:

Defining Tables

Tables enable you to display data in a two dimensional table. You can use the Property Inspector to get and set the object property values.

Commonly Used Properties

The most commonly used properties of a table component are listed in the table below. These are grouped in the order they appear in the Table Property Editor. Please refer to uitable command documentation for detail of all the table properties:

GroupPropertyValuesDescription
ColumnColumnName1-by-n cell array of strings|{'numbered'}|empty matrix ([])The header label of the column.
ColumnFormatCell array of stringsDetermines display and editablility of columns
ColumnWidth1–by-n numerical matrix or cell arrayWidth of each column in pixels
ColumnEditablelogical 1–by-n matrix|scalar logical value|{ empty matrix ([])}Determines data in a column as editable
RowRowName1-by-n cell array of stringsRow header label names
ColorBackgroundColorn-by-3 matrix of RGB triplesBackground color of cells
RowStriping{on} | offColor striping of table rows
DataDataMatrix or cell array of numeric, logical, or character data Table data.

Tables

To create a GUI with a table in GUIDE as shown, do the following:

Drag the table icon on to the Layout Editor and right click in the table. Select Table Property Editor form the pop-up menu.

Set Column Properties.   The Table Property Editor, opens with Column panel on top. Click Insert to add two more columns.

Select Show names entered below as the column headers and set the ColumnName by entering Rate, Amount, Available, and Fixed/Adj in Name group. for the Available and Fixed/Adj columns set the ColumnEditable property to on. Lastly set the ColumnFormat for the four columns

For the Rate column, select Numeric. For the Amount Column select Custom and in the Custom Format Editor, choose Bank.

Leave the Available column at the default value. This allows MATLAB to chose based on the value of the Data property of the table. For the Fixed/Adj column select Choice List to create a pop-up menu. In the Choice List Editor, click Insert to add a second choice and type Fixed and Adjustable as the 2 choices.

Set Row Properties.   In the Row tab, leave the default RowName, Show numbered row headers.

Set Data Properties.   Specify the value of the Data you want in the table. You need create Data in the MATLAB command window before you specify it in GUIDE. For this example, type:

dat =  {6.125, 456.3457, true,  'Fixed';...
6.75,  510.2342, false, 'Adjustable';...
7,     658.2,    false, 'Fixed';};

In the Table Property Editor, select the data that you defined and select Change data value to the selected workspace variable below.

Set Color Properties.   Specify the BackgroundColor and RowStriping for your table in the Color tab.

You can change other uitable properties to the table via the Property Inspector.

Adding ActiveX® Controls

When you drag an ActiveX component from the component palette into the layout area, GUIDE opens a dialog box, similar to the following, that lists the registered ActiveX controls on your system.

  1. Select the desired ActiveX control. The right panel shows a preview of the selected control.

  2. Click Create. The control appears as a small box in the Layout Editor.

  3. Resize the control to approximately the size of the square shown in the preview pane. You can do this by clicking and dragging a corner of the control, as shown in the following figure.

    Resizing the control by clicking and dragging

See ActiveX® Control for information about programming a sample ActiveX control and an example.

Working with Components in the Layout Area

This topic provides basic information about selecting, copying, pasting, and deleting components in the layout area.

Other topics that may be of interest are

Selecting Components

You can select components in the layout area in the following ways:

In some cases, a component may lie outside its parent's boundary. Such a component is not visible in the Layout Editor but can be selected by dragging a rectangle that encloses it or by selecting it in the Object Browser. Such a component is visible in the active GUI.

See Viewing the Object Hierarchy for information about the Object Browser.

Copying, Cutting, and Clearing Components

Use standard menu and pop-up menu commands, toolbar icons, keyboard keys, and shortcut keys to copy, cut, and clear components.

Copying.   Copying places a copy of the selected components on the clipboard. A copy of a panel or button group includes its children.

Cutting.   Cutting places a copy of the selected components on the clipboard and deletes them from the layout area. If you cut a panel or button group, you also cut its children.

Clearing.   Clearing deletes the selected components from the layout area. It does not place a copy of the components on the clipboard. If you clear a panel or button group, you also clear its children.

Pasting and Duplicating Components

Pasting.   Use standard menu and pop-up menu commands, toolbar icons, and shortcut keys to paste components. GUIDE pastes the contents of the clipboard to the location of the last mouse click. It positions the upper-left corner of the contents at the mouse click.

Consecutive pastes place each copy to the lower right of the last one.

Duplicating.   Select one or more components that you want to duplicate, then do one of the following:

Front-to-Back Positioning

MATLAB figures maintain separate stacks that control the front-to-back positioning for different kinds of components:

You can control the front-to-back positioning of components that overlap only if those components are in the same stack. For overlapping components that are in different stacks:

The Layout Editor provides four operations that enable you to control front-to-back positioning. All are available from the Layout menu, which is shown in the following figure.

Locating and Moving Components

You can locate or move components in one of the following ways:

Another topic that may be of interest is

Using Coordinate Readouts

Coordinate readouts indicate where a component is placed and where the mouse pointer is located. Use these readouts to position and align components manually. The coordinate readout in the lower right corner of the Layout Editor shows the position of a selected component or components as [xleft ybottom width height]. These values are displayed in units of pixels, regardless of the coordinate units you select for components.

If you drag or resize the component, the readout updates accordingly. The readout to the left of the component position readout displays the current mouse position, also in pixels. The following readout example shows a selected component that has a position of [35, 30, 180, 180], a 180-by-180 pixel object with a lower left corner at x=35 and y=30, and locates the mouse position at [200, 30].

When you select multiple objects, the Position readout displays numbers for x, y, width and height only if the objects have the same respective values; in all other cases it displays 'MULTI'. For example, if you select two checkboxes, one with Position [250, 140, 76, 20] pixels and the other with position [250, 190, 68, 20] pixels, the Position readout indicates [250, MULTI, MULTI, 20].

Dragging Components

Select one or more components that you want to move, then drag them to the desired position and drop them. You can move components from the figure into a panel or button group. You can move components from a panel or button group into the figure or into another panel or button group.

The position of the cursor when you drop the components also determines the parent of all the selected components. Look for the highlight as described in Adding a Component to a Panel or Button Group.

In some cases, one or more of the selected components may lie outside its parent's boundary. Such a component is not visible in the Layout Editor but can be selected by dragging a rectangle that encloses it or by selecting it in the Object Browser. Such a component is visible in the active GUI.

See Viewing the Object Hierarchy for information about the Object Browser.

Using Arrow Keys to Move Components

Select one or more components that you want to move, then press and hold the arrow keys until the components have moved to the desired position. Note that the components remain children of the figure, panel, or button group from which you move them, even if they move outside its boundaries.

Setting the Component's Position Property

Select one or more components that you want to move. Then open the Property Inspector from the View menu or by clicking the Property Inspector button .

  1. In the Property Inspector, scroll to the Units property and note whether the current setting is characters or normalized. Click the button next to Units and then change the setting to inches from the pop-up menu.

  2. Click the + sign next to Position. The Property Inspector displays the elements of the Position property.

  3. If you have selected

  4. Reset the Units property to its previous setting, either characters or normalized.

Resizing Components

You can resize components in one of the following ways:

Dragging a Corner of the Component

Select the component you want to resize. Click one of the corner handles and drag it until the component is the desired size.

Setting the Component's Position Property

Select one or more components that you want to resize. Then open the Property Inspector from the View menu or by clicking the Property Inspector button .

  1. In the Property Inspector, scroll to the Units property and note whether the current setting is characters or normalized. Click the button next to Units and then change the setting to inches from the pop-up menu.

  2. Click the + sign next to Position. The Property Inspector displays the elements of the Position property.

  3. Type the width and height you want the components to be.

  4. Reset the Units property to its previous setting, either characters or normalized.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS