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