MATLAB The Language of Technical Computing Computation Visualization Programming Creating Graphical User Interfaces The MathWorks Version 6
Computation Visualization Programming Creating Graphical User Interfaces Version 6 MATLABÆ The Language of Technical Computing
Contents Getting Started with GUIDE GUIDE- GUI Development Environment GUIDE Toolset GUI FIG-Files and M-files 15 sing GUIDE Templates 1-6 Example: Creating a gui 2 Designing the gul 22 Laying Out the GUI Layout and Code for the Example 23 Open a New gUI in the Layout Editor Set the gui Figure siz∈ Add the components Align the objects Programming the gui Set Properties for the GUI Components 2-9 Opening the gUI M-file Opening Function 2-15 etween 2-15 Adding Code to the Opening Function 2-16 Adding Code to the Callbacks 2-17 Using the Object Browser to Identify Callbacks Saving and running the gui 2-21
i Contents 1 Getting Started with GUIDE GUIDE — GUI Development Environment . . . . . . . . . . . . . . 1-2 GUIDE Toolset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 GUI FIG-Files and M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5 Using GUIDE Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 2 Example: Creating a GUI Designing the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Laying Out the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Layout and Code for the Example . . . . . . . . . . . . . . . . . . . . . . . 2-3 Open a New GUI in the Layout Editor . . . . . . . . . . . . . . . . . . . 2-4 Set the GUI Figure Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Add the Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6 Align the Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Programming the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 Set Properties for the GUI Components . . . . . . . . . . . . . . . . . . 2-9 Opening the GUI M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Opening Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 Sharing Data Between Callbacks . . . . . . . . . . . . . . . . . . . . . . . 2-15 Adding Code to the Opening Function . . . . . . . . . . . . . . . . . . . 2-16 Adding Code to the Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Using the Object Browser to Identify Callbacks . . . . . . . . . . . 2-20 Saving and Running the GUI . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
MATLAB GUIS What Is a gui 32 Creating GUIs with GUIDE 3-3 GUI Development Environment 33 Editing version 5 GUIs with Version 6 GUIDE Using GUIDE Templates 3-6 Blank gui 37 GUI with uicontrols GUI with Axes and menu 3-9 Modal question Dialog 3-10 Example: Using the Modal Dialog to Confirm an Operation 3-13 View the Layout and gui M-File tting up the close Confirmation Dialog Setting Up the GUI with the Close Button 3-15 Running the Gui with the Close Button How the gui and Dialog Work 3-17 Example: Displaying an Image File Selecting GUI Options 3-21 Configuring the GUI M-File Resize behavior Command-Line Accessibility 3-23 Generate Fig-file and m-file 325 Generate Callback Function Prototypes 3-26 GUI Allows Only One Instance to Run( Singleton) 327 Using the System Background Colors Generate FIG- File Only 3-28 User Interface Controls Push buttons 3-30 Toggle Buttons Radio buttons Edit text
ii Contents 3 MATLAB GUIs What Is a GUI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Creating GUIs with GUIDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 GUI Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Editing Version 5 GUIs with Version 6 GUIDE . . . . . . . . . . . . . 3-4 Using GUIDE Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6 Blank GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 GUI with UIcontrols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8 GUI with Axes and Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9 Modal Question Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10 Example: Using the Modal Dialog to Confirm an Operation 3-13 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . 3-13 Setting Up the Close Confirmation Dialog . . . . . . . . . . . . . . . . 3-14 Setting Up the GUI with the Close Button . . . . . . . . . . . . . . . 3-15 Running the GUI with the Close Button . . . . . . . . . . . . . . . . . 3-16 How the GUI and Dialog Work . . . . . . . . . . . . . . . . . . . . . . . . . 3-17 Example: Displaying an Image File . . . . . . . . . . . . . . . . . . . . 3-19 Selecting GUI Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Configuring the GUI M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Resize Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22 Command-Line Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23 Generate FIG-File and M-File . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25 Generate Callback Function Prototypes . . . . . . . . . . . . . . . . . . 3-26 GUI Allows Only One Instance to Run (Singleton) . . . . . . . . . 3-27 Using the System Background Colors . . . . . . . . . . . . . . . . . . . 3-28 Generate FIG-File Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28 User Interface Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Push Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Toggle Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31 Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31 Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33 Edit Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33
Static Text 3-34 Sliders 335 Frames List boxes Pop-Up Menus 339 Enabling or Disabling Controls Axes 341 Renaming GUI Files and tags 344 Renaming GUI Files Using Save As Changing Component Tag Properties 3-44 Updating Callback Properties and Functions After Changing Tags 3-45 Changing the Name of Callback Properties porting a gui to a single M-File GUIDE Layout Tools Laying Out GUIs- The Layout Editor 42 Placing Objects In the Layout Area Running the gu ...4-3 ving the Layout 45 Displaying Your GUI Layout Editor Preferences 4-6 Layout Editor Context Menus 4-6 Aligning Groups of Components-The Alignment loo Aligning Components in the Layout Editor 49 4-9 Grids and rulers 4-11 Aligning Components to Guide lines 4-12 Front to Back Positioning 4-12 Setting Component Properties- The Property Inspector 4-14 Viewing the Object Hierarchy The Object Browser 4-16
iii Static Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37 List Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37 Pop-Up Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-39 Enabling or Disabling Controls . . . . . . . . . . . . . . . . . . . . . . . . . 3-41 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-41 Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-42 Renaming GUI Files and Tags . . . . . . . . . . . . . . . . . . . . . . . . . 3-44 Renaming GUI Files Using Save As . . . . . . . . . . . . . . . . . . . . . 3-44 Changing Component Tag Properties . . . . . . . . . . . . . . . . . . . . 3-44 Updating Callback Properties and Functions After Changing Tags 3-45 Changing the Name of Callback Properties . . . . . . . . . . . . . . . 3-45 Exporting a GUI to a Single M-File . . . . . . . . . . . . . . . . . . . . 3-47 4 GUIDE Layout Tools Laying Out GUIs — The Layout Editor . . . . . . . . . . . . . . . . . . 4-2 Placing Objects In the Layout Area . . . . . . . . . . . . . . . . . . . . . . 4-2 Running the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Saving the Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Displaying Your GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Layout Editor Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Layout Editor Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Aligning Components in the Layout Editor . . . . . . . . . . . . . . 4-9 Aligning Groups of Components — The Alignment Tool . . . . . . 4-9 Grids and Rulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11 Aligning Components to Guide Lines . . . . . . . . . . . . . . . . . . . . 4-12 Front to Back Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12 Setting Component Properties — The Property Inspector 4-14 Viewing the Object Hierarchy — The Object Browser . . . 4-16
Creating Menus- The Menu edito 4-17 Defining Menus for the Menu Bar 4-17 Defining Context Menus 4-23 Setting the Tab Order- The Tab Order Editor 4-2 Programming GUis Understanding the guI M-File 52 Sharing Data with the Handles Structure 52 Functions and callbacks in the m-file Output function 5 Callbacks Input and Output Arguments Managing GUI Data with the Handles Structure 5-8 Example: Passing Data Between Callbacks 5-8 Application Data .. 511 Designing for Cross-Platform Compatibility Using the Default System Font 5-12 Using Standard Background Color Cross-Platform Compatible Figure Units ..5-14 Types of Callbacks 515 Callback Properties for All graphics Objects 5-15 Callback Properties for Figures 515 Which Callback executes Adding A Callback 516 Interrupting Executing Callbacks 5-17 Controlling Interruptibility 5-17 The Event queue Event Processing During Callback Execution ..5-18
iv Contents Creating Menus — The Menu Editor . . . . . . . . . . . . . . . . . . . 4-17 Defining Menus for the Menu Bar . . . . . . . . . . . . . . . . . . . . . . 4-17 Menu Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22 Defining Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23 Setting the Tab Order — The Tab Order Editor . . . . . . . . . 4-27 5 Programming GUIs Understanding the GUI M-File . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Sharing Data with the Handles Structure . . . . . . . . . . . . . . . . . 5-2 Functions and Callbacks in the M-File . . . . . . . . . . . . . . . . . . . . 5-3 Opening Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Input and Output Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Managing GUI Data with the Handles Structure . . . . . . . . . 5-8 Example: Passing Data Between Callbacks . . . . . . . . . . . . . . . . 5-8 Application Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11 Designing for Cross-Platform Compatibility . . . . . . . . . . . . 5-12 Using the Default System Font . . . . . . . . . . . . . . . . . . . . . . . . . 5-12 Using Standard Background Color . . . . . . . . . . . . . . . . . . . . . . 5-13 Cross-Platform Compatible Figure Units . . . . . . . . . . . . . . . . . 5-14 Types of Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15 Callback Properties for All Graphics Objects . . . . . . . . . . . . . . 5-15 Callback Properties for Figures . . . . . . . . . . . . . . . . . . . . . . . . 5-15 Which Callback Executes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Adding A Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Interrupting Executing Callbacks . . . . . . . . . . . . . . . . . . . . . 5-17 Controlling Interruptibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17 The Event Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17 Event Processing During Callback Execution . . . . . . . . . . . . . 5-18
Controlling Figure Window Behavior Using Modal Figure Windows 520 Application Examples 6 GUI with Multiple Axes 6-2 View the Layout and guI M-File 6-3 Design of the GUI 6-3 Plot push Button callback 6-6 List Box Directory Reader 6-9 iew the Layout and GUI M-File Implementing the GuI 6-10 Specifying the Directory to List 6-10 Loading the list Box 611 Accessing Workspace Variables from a List Box 6-15 Techniques Used in This Example View the Layout and GUI M-File 6-16 Reading Workspace Variables 6-16 Reading the Selections from the List Box 6-17 A GUI to Set simulink model parameters Techniques Used in This Example View the Layout and GUI M-File 6-19 How to Use the GUI (Text of GUI Help Running the gUI Programming the Slider and Edit Text Components Running the Simulation from the GUI ..6-24 Removing Results from the list box 6-26 Plotting the results d The GUI Help Button 6-29 Closing the gui The list box callback and create Function
v Controlling Figure Window Behavior . . . . . . . . . . . . . . . . . . 5-20 Using Modal Figure Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20 6 Application Examples GUI with Multiple Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Techniques Used in the Example . . . . . . . . . . . . . . . . . . . . . . . . 6-2 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Design of the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Plot Push Button Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 List Box Directory Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . . 6-9 Implementing the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10 Specifying the Directory to List . . . . . . . . . . . . . . . . . . . . . . . . . 6-10 Loading the List Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11 Accessing Workspace Variables from a List Box . . . . . . . . . 6-15 Techniques Used in This Example . . . . . . . . . . . . . . . . . . . . . . 6-15 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . 6-16 Reading Workspace Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16 Reading the Selections from the List Box . . . . . . . . . . . . . . . . . 6-17 A GUI to Set Simulink Model Parameters . . . . . . . . . . . . . . 6-19 Techniques Used in This Example . . . . . . . . . . . . . . . . . . . . . . 6-19 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . 6-19 How to Use the GUI (Text of GUI Help) . . . . . . . . . . . . . . . . . . 6-20 Running the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21 Programming the Slider and Edit Text Components . . . . . . . . 6-22 Running the Simulation from the GUI . . . . . . . . . . . . . . . . . . . 6-24 Removing Results from the List Box . . . . . . . . . . . . . . . . . . . . 6-26 Plotting the Results Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27 The GUI Help Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-29 Closing the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-29 The List Box Callback and Create Function . . . . . . . . . . . . . . 6-29
An Address book reader Techniques Used in This Example Managing Shared Data 333 View the Layout and GUI M-File 6-32 Running the gui 632 Loading an Address book Into the reader The Contact Name Callback 6-36 The Contact phone number callback Paging Through the Address Book- Prev/Next 6-39 ng Changes to the Address Book from the menu 6-41 The Create New Menu The Address book resize function vI Contents
vi Contents An Address Book Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-31 Techniques Used in This Example . . . . . . . . . . . . . . . . . . . . . . 6-31 Managing Shared Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-31 View the Layout and GUI M-File . . . . . . . . . . . . . . . . . . . . . . . 6-32 Running the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-32 Loading an Address Book Into the Reader . . . . . . . . . . . . . . . . 6-34 The Contact Name Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-36 The Contact Phone Number Callback . . . . . . . . . . . . . . . . . . . . 6-38 Paging Through the Address Book — Prev/Next . . . . . . . . . . . 6-39 Saving Changes to the Address Book from the Menu . . . . . . . 6-41 The Create New Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-43 The Address Book Resize Function . . . . . . . . . . . . . . . . . . . . . . 6-43
Getting Started with GUIDE GUIDE- GUI Development Introduction to GUIDE and the layout Editor Environment(p. 1-2) GUI FIG-Files and M-Files(p. 1-5) How GUIDE stores GUIs Using GUIDE Templates(p 1-6) GUIDE provides templates -simple examples of GUIs, which
1 Getting Started with GUIDE GUIDE — GUI Development Environment (p. 1-2) Introduction to GUIDE and the Layout Editor GUI FIG-Files and M-Files (p. 1-5) How GUIDE stores GUIs Using GUIDE Templates (p. 1-6) GUIDE provides templates — simple examples of GUIs, which you can modify for your own purposes
1 Getting Started with GUIDE GUIDE- GUl Development Environment GUIDE, the MatLAB Graphical User Interface development environment t of tools for creating gUis. These tools greatly simplify th process of laying out and programming a GUI. This section introduces you to GUIDE and the layout tools it provides When you open a gui in guide, it is displayed in the layout Editor, which is the control panel for all of the guide tools. The Layout Editor enables you to lay out a gui quickly and easily by dragging components, such as push buttons, pop-up menus, or axes, from the component palette into the layout area. The following picture shows the Layout Editor 1-2
1 Getting Started with GUIDE 1-2 GUIDE — GUI Development Environment GUIDE, the MATLAB Graphical User Interface development environment, provides a set of tools for creating GUIs. These tools greatly simplify the process of laying out and programming a GUI. This section introduces you to GUIDE and the layout tools it provides. When you open a GUI in GUIDE, it is displayed in the Layout Editor, which is the control panel for all of the GUIDE tools. The Layout Editor enables you to lay out a GUI quickly and easily by dragging components, such as push buttons, pop-up menus, or axes, from the component palette into the layout area. The following picture shows the Layout Editor
GUIDE -GUI Development Environment Alignment Tool M-file edit rty Inspector Object Browser Run butto )untitled.fig 「郾以爵哈 Redo OKI Push Button O Radio Button 区 Checkb La Area EDIT Edit Tent Palette THT Static Text □ Frame IM Axes Figure Resize Tab Once you lay out your GUI and set each components properties, using the tools in the layout Editor, you can program the GUi with the M-file Editor. Finally, when you press the Run button on the toolbar, the functioning GUI appears outside the layout Editor window GUIDE To。lser For more information on the full set of GuidE development tools, see the following sections
GUIDE — GUI Development Environment 1-3 Once you lay out your GUI and set each component’s properties, using the tools in the Layout Editor, you can program the GUI with the M-file Editor. Finally, when you press the Run button on the toolbar, the functioning GUI appears outside the Layout Editor window. GUIDE Toolset For more information on the full set of GUIDE development tools, see the following sections: Component Palette Alignment Tool Menu Editor Property Inspector Run Button Layout Area Figure Resize Tab Undo Redo M-file Editor Object Browser