ebook img

MatLab GUI Tutorial PDF

500 Pages·2015·6.26 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview MatLab GUI Tutorial

® MATLAB Creating Graphical User Interfaces R2015a How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 MATLAB® Creating Graphical User Interfaces © COPYRIGHT 2000–2015 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History November 2000 Online Only New for MATLAB 6.0 (Release 12) June 2001 Online Only Revised for MATLAB 6.1 (Release 12.1) July 2002 Online Only Revised for MATLAB 6.6 (Release 13) June 2004 Online Only Revised for MATLAB 7.0 (Release 14) October 2004 Online Only Revised for MATLAB 7.0.1 (Release 14SP1) March 2005 Online Only Revised for MATLAB 7.0.4 (Release 14SP2) September 2005 Online Only Revised for MATLAB 7.1 (Release 14SP3) March 2006 Online Only Revised for MATLAB 7.2 (Release 2006a) May 2006 Online Only Revised for MATLAB 7.2 September 2006 Online Only Revised for MATLAB 7.3 (Release 2006b) March 2007 Online Only Revised for MATLAB 7.4 (Release 2007a) September 2007 Online Only Revised for MATLAB 7.5 (Release 2007b) March 2008 Online Only Revised for MATLAB 7.6 (Release 2008a) October 2008 Online Only Revised for MATLAB 7.7 (Release 2008b) March 2009 Online Only Revised for MATLAB 7.8 (Release 2009a) September 2009 Online Only Revised for MATLAB 7.9 (Release 2009b) March 2010 Online Only Revised for MATLAB 7.10 (Release 2010a) September 2010 Online Only Revised for MATLAB 7.11 (Release 2010b) April 2011 Online Only Revised for MATLAB 7.12 (Release 2011a) September 2011 Online Only Revised for MATLAB 7.13 (Release 2011b) March 2012 Online Only Revised for MATLAB 7.14 (Release 2012a) September 2012 Online Only Revised for MATLAB 8.0 (Release 2012b) March 2013 Online Only Revised for MATLAB 8.1 (Release 2013a) September 2013 Online Only Revised for MATLAB 8.2 (Release 2013b) March 2014 Online Only Revised for MATLAB 8.3 (Release 2014a) October 2014 Online Only Revised for MATLAB 8.4 (Release 2014b) March 2015 Online Only Revised for MATLAB 8.5 (Release 2015a) Contents Introduction to Creating UIs About UIs in MATLAB Software 1 What Is a UI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 How Does a UI Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 Ways to Build MATLAB UIs . . . . . . . . . . . . . . . . . . . . . . . 1-5 How to Create a UI with GUIDE 2 Create a Simple UI Using GUIDE . . . . . . . . . . . . . . . . . . 2-2 Open a New UI in the GUIDE Layout Editor . . . . . . . . 2-2 Set the Window Size in GUIDE . . . . . . . . . . . . . . . . . . 2-5 Layout the Simple GUIDE UI . . . . . . . . . . . . . . . . . . . . 2-6 Code the Behavior of the Simple GUIDE UI . . . . . . . . 2-16 Open and Run the Simple GUIDE UI . . . . . . . . . . . . . 2-22 Files Generated by GUIDE . . . . . . . . . . . . . . . . . . . . . . . 2-24 Code Files and FIG-Files . . . . . . . . . . . . . . . . . . . . . . . 2-24 UI Code File Structure . . . . . . . . . . . . . . . . . . . . . . . . 2-24 Adding Callback Templates to an Existing UI Code File 2-25 About GUIDE-Generated Callbacks . . . . . . . . . . . . . . 2-26 v A Simple Programmatic UI 3 Create a Simple UI Programmatically . . . . . . . . . . . . . . 3-2 Create a Code File for the Simple Programmatic UI . . . 3-3 Create a Figure for the Simple Programmatic UI . . . . . 3-3 Add Components to the Simple Programmatic UI . . . . . 3-4 Code the Simple Programmatic UI Behavior . . . . . . . . . 3-6 Verify Code and Run the Program . . . . . . . . . . . . . . . 3-10 Create UIs with GUIDE What Is GUIDE? 4 GUIDE: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 UI Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 UI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 GUIDE Tools Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 GUIDE Preferences and Options 5 GUIDE Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Set Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Confirmation Preferences . . . . . . . . . . . . . . . . . . . . . . . 5-2 Backward Compatibility Preference . . . . . . . . . . . . . . . 5-4 All Other Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 GUIDE Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8 The GUI Options Dialog Box . . . . . . . . . . . . . . . . . . . . . 5-8 Resize Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Command-Line Accessibility . . . . . . . . . . . . . . . . . . . . . 5-9 vi Contents Generate FIG-File and MATLAB File . . . . . . . . . . . . . 5-10 Generate FIG-File Only . . . . . . . . . . . . . . . . . . . . . . . 5-12 Lay Out a UI Using GUIDE 6 GUIDE Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Access the Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Template Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Set the UI Window Size in GUIDE . . . . . . . . . . . . . . . . . 6-11 Prevent Existing Objects from Resizing with the Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11 Set the Window Position or Size to an Exact Value . . . 6-12 Maximize the Layout Area . . . . . . . . . . . . . . . . . . . . . 6-12 GUIDE Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13 Add Components to the GUIDE Layout Area . . . . . . . . 6-17 Place Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17 User Interface Controls . . . . . . . . . . . . . . . . . . . . . . . . 6-23 Panels and Button Groups . . . . . . . . . . . . . . . . . . . . . 6-44 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-50 Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-54 ActiveX Component . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-65 Resize GUIDE UI Components . . . . . . . . . . . . . . . . . . 6-67 Copy, Paste, and Arrange Components . . . . . . . . . . . . . 6-70 Select Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-70 Copy, Cut, and Clear Components . . . . . . . . . . . . . . . . 6-71 Paste and Duplicate Components . . . . . . . . . . . . . . . . 6-71 Front-to-Back Positioning . . . . . . . . . . . . . . . . . . . . . . 6-72 Locate and Move Components . . . . . . . . . . . . . . . . . . . . 6-74 Use Coordinate Readouts . . . . . . . . . . . . . . . . . . . . . . 6-74 Drag Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-75 Use Arrow Keys to Move Components . . . . . . . . . . . . . 6-76 Set the Component's Position Property . . . . . . . . . . . . 6-76 vii Align GUIDE UI Components . . . . . . . . . . . . . . . . . . . . . 6-79 Align Objects Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-79 Property Inspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-82 Grid and Rulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-85 Guide Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-86 Customize Tabbing Behavior in a GUIDE UI . . . . . . . . 6-88 Create Menus for GUIDE UIs . . . . . . . . . . . . . . . . . . . . . 6-91 Menus for the Menu Bar . . . . . . . . . . . . . . . . . . . . . . . 6-91 Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-101 Create Toolbars for GUIDE UIs . . . . . . . . . . . . . . . . . . 6-108 Toolbar and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-108 Editing Tool Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-116 View the GUIDE Object Hierarchy . . . . . . . . . . . . . . . 6-119 Design Cross-Platform UIs in GUIDE . . . . . . . . . . . . . 6-120 Default System Font . . . . . . . . . . . . . . . . . . . . . . . . . 6-120 Standard Background Color . . . . . . . . . . . . . . . . . . . 6-121 Cross-Platform Compatible Units . . . . . . . . . . . . . . . 6-121 UI Design References . . . . . . . . . . . . . . . . . . . . . . . . . . 6-123 Save and Run a GUIDE UI 7 Save a GUIDE UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Save a UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Create a Backward Compatible GUIDE Fig-File . . . . . . 7-2 Append New Callbacks to an Existing GUIDE Code File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3 Create Programmatic Files from GUIDE Files . . . . . . . 7-4 Rename GUIDE UIs and Files . . . . . . . . . . . . . . . . . . . . . 7-5 viii Contents Programming a GUIDE UI 8 Write Callbacks Using the GUIDE Workflow . . . . . . . . . 8-2 Callbacks for Different User Actions . . . . . . . . . . . . . . . 8-2 GUIDE-Generated Callback Functions and Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4 GUIDE Callback Syntax . . . . . . . . . . . . . . . . . . . . . . . . 8-5 Renaming and Removing GUIDE-Generated Callbacks . 8-5 Initialize UIs Created Using GUIDE . . . . . . . . . . . . . . . . 8-7 Opening Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7 Output Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9 Callbacks for Specific Components . . . . . . . . . . . . . . . . 8-11 How to Use the Example Code . . . . . . . . . . . . . . . . . . 8-11 Push Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12 Toggle Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12 Radio Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13 Check Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14 Edit Text Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14 Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-15 List Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16 Pop-Up Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-18 Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-20 Button Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-21 Menu Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22 Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-25 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-26 Examples of GUIDE UIs . . . . . . . . . . . . . . . . . . . . . . . . . 8-29 Examples of GUIDE UIs 9 Modal Dialog Box in GUIDE . . . . . . . . . . . . . . . . . . . . . . 9-2 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2 Set Up the Close Confirmation Dialog Box . . . . . . . . . . 9-2 Set Up a UI with a Close Button . . . . . . . . . . . . . . . . . 9-3 ix Run the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4 How Close Confirmation Dialogs Work . . . . . . . . . . . . . 9-5 UI That Uses Persistent Data . . . . . . . . . . . . . . . . . . . . . . 9-7 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7 Calling Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8 MAT-file Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9 UI Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10 Overall UI Characteristics . . . . . . . . . . . . . . . . . . . . . 9-17 UI That Accepts Parameters and Generates Plots . . . . 9-20 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20 UI Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22 Validate Input as Numbers . . . . . . . . . . . . . . . . . . . . . 9-24 Plot Push Button Behavior . . . . . . . . . . . . . . . . . . . . . 9-27 Synchronized Data Presentations in a GUIDE UI . . . . 9-30 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-30 Recreate the UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-32 Interactive List Box in a GUIDE UI . . . . . . . . . . . . . . . 9-46 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-46 Implement the List Box . . . . . . . . . . . . . . . . . . . . . . . 9-47 Plot Workspace Variables in a GUIDE UI . . . . . . . . . . . 9-52 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-52 Read Workspace Variables . . . . . . . . . . . . . . . . . . . . . 9-53 Read Selections from List Box . . . . . . . . . . . . . . . . . . . 9-54 UI for Setting Simulink Model Parameters . . . . . . . . . 9-57 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-57 How to Use the UI . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-58 Run the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-59 Program the Slider and Edit Text Components . . . . . . 9-60 Run the Simulation from the UI . . . . . . . . . . . . . . . . . 9-62 Remove Results from List Box . . . . . . . . . . . . . . . . . . 9-64 Plot Results Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-64 The Help Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-66 Close the UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-66 The List Box Callback and Create Function . . . . . . . . 9-67 Animation with Slider Controls in GUIDE . . . . . . . . . . 9-68 About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-68 x Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.