logo资料库

Learning to Program with MATLAB:Building GUI Tools.pdf

第1页 / 共322页
第2页 / 共322页
第3页 / 共322页
第4页 / 共322页
第5页 / 共322页
第6页 / 共322页
第7页 / 共322页
第8页 / 共322页
资料共322页,剩余部分请下载后查看
Cover
Title Page
Copyright Page
Contents
Preface
I: MATLAB Programming
1: Getting Started
1.1: Running the MATLAB IDE
Manipulating windows
1.2: MATLAB variables
Variable assignment statements
Variable names
Variable workspace
1.3: Numbers and functions
1.4: Documentation
1.5: Writing simple MATLAB scripts
1.6: A few words about errors and debugging
1.7: Using the debugger
2: Strings and Vectors
2.1: String basics
2.2: Using the disp command to print a variable's value
2.3: Getting information from the user
2.4: Vectors
2.5: Operations on vectors
2.6: Special vector functions
Statistical functions
2.7: Using rand and randi
3: Plotting
3.1: The plot command
3.2: Tabulating and plotting a simple function
3.3: Bar graphs and histograms
3.4: Drawing several plots on one graph
Multiple plots with a single plot command
Combining multiple plots with a hold command
3.5: Adding lines and text
4: Matrices
4.1: Entering and manipulating matrices
4.2: Operations on matrices
4.3: Solving linear systems: The backslash operator
Extended example: Solving circuit problems
4.4: Special matrix functions
5: Control Flow Commands
5.1: Conditional execution: The if statement
5.2: Logical expressions
5.3: Logical variables
5.4: for loops
5.5: while loops
5.6: Other control flow commands
Switch-case statement
Break statement (not recommended)
6: Animation
6.1: Basic animation
6.2: Animating function plots
6.3: Kinematics of motion
One-dimensional motion: Constant speed
Motion with constant acceleration
Time-marching dynamics: Nonconstant force
7: Writing Your Own MATLAB Functions
7.1: MATLAB function files
Declaring MATLAB functions
7.2: Function inputs and outputs
7.3: Local workspaces
7.4: Multiple outputs
7.5: Function files
7.6: Other functional forms
Subfunctions
Nested functions
Anonymous functions
8: More MATLAB Data Classes and Structures
8.1: Cell arrays
8.2: Structures
8.3: Complex numbers
8.4: Function handles
8.5: Other data classes and data structures
II: Building GUI Tools
9: Building a Graphical User Interface
9.1: Getting started with GUIDE
Saving the GUI to file
9.2: Starting an action with a GUI element
9.3: Communicating with GUI elements
Building SliderTool
Communicating with GUI elements from the command line
9.4: Synchronizing information with a GUI element
9.5: Key points from this chapter
10: Transforming a MATLAB Program into a GUI Tool
10.1: Creating a GUI tool step by step
10.2: Further GUI design considerations
11: GUI Components
III: Advanced Topics
12: More GUI Techniques
12.1: Waitbars
12.2: File dialogs
Saving and loading data in .mat files
A GUI interface to file names using uiputfile and uigetfile
12.3: Reading and writing formatted text files
12.4: The input dialog
12.5: The question dialog
12.6: Sharing application data between functions
12.7: Responding to keyboard input
12.8: Making graphic objects interactive
Mouse-click response
Mouse events and object dragging
12.9: Creating menus in GUIDE
13: More Graphics
13.1: Logarithmic plots
13.2: Plotting functions on two axes
13.3: Plotting surfaces
13.4: Plotting vector fields
13.5: Working with images
Importing and manipulating bit-mapped images
Placing images on surface objects
13.6: Rotating composite objects in three dimensions
14: More Mathematics
14.1: Derivatives
Derivatives of mathematical functions expressed as MATLAB functions
Derivatives of tabulated functions
14.2: Integration
Integrating tabulated functions
Integrating mathematical functions expressed as MATLAB functions
14.3: Zeros of a function of one variable
14.4: Function minimization
Finding a minimum of a function of one variable
Multidimensional minimization
Fitting to an arbitrary function by multidimensional minimization
Solving simultaneous nonlinear equations by multidimensional minimization
14.5: Solving ordinary differential equations
14.6: Eigenvalues and eigenvectors
Appendix A: Hierarchy of Handle Graphics Objects
Appendix B: Using LATEX Commands
Index
LENT ffirs.tex 28/11/2012 17: 13 Page iv
LENT ffirs.tex 4/12/2012 14: 28 Page i LEARNING TO PROGRAM WITH MATLAB Building GUI Tools Craig S. Lent Department of Electrical Engineering University of Notre Dame
LENT ffirs.tex 4/12/2012 14: 28 Page ii VP & PUBLISHER: ASSOCIATE PUBLISHER: EDITORIAL ASSISTANT: MARKETING MANAGER: DESIGNER: SENIOR PRODUCTION MANAGER: ASSOCIATE PRODUCTION MANAGER: Don Fowley Dan Sayre Jessica Knecht Christopher Ruel Kenji Ngieng Janis Soo Joyce Poh This book was set by MPS Limited, Chennai. Cover and text printed and bound by Courier Westford. This book is printed on acid free paper. Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more than 200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is built on a foundation of principles that include responsibility to the communities we serve and where we live and work. In 2008, we launched a Corporate Citizenship Initiative, a global effort to address the environmental, social, economic, and ethical challenges we face in our business. Among the issues we are addressing are carbon impact, paper specifications and procurement, ethical conduct within our business and among our vendors, and community and charitable support. For more information, please visit our website: www.wiley.com/go/citizenship. Copyright © 2013 John Wiley & Sons, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc. 222 Rosewood Drive, Danvers, MA 01923, website www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, (201)748-6011, fax (201)748-6008, website http://www.wiley.com/go/permissions. Evaluation copies are provided to qualified academics and professionals for review purposes only, for use in their courses during the next academic year. These copies are licensed and may not be sold or transferred to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return instructions and a free of charge return mailing label are available at www.wiley.com/go/returnlabel. If you have chosen to adopt this textbook for use in your course, please accept this book as your complimentary desk copy. Outside of the United States, please contact your local sales representative. Library of Congress Cataloging-in-Publication Data Lent, Craig S., 1956– Learning to program with MATLAB : building GUI tools / Craig S. Lent, Department of Electrical Engineering, University of Notre Dame. pages cm Includes index. ISBN 978-0-470-93644-3 (pbk. : acid-free paper) 1. Computer programming. 2. Visual programming (Computer science) 3. MATLAB. (Computer systems) I. Title. 4. Graphical user interfaces QA76.6.L45 005.4'37—dc23 2013 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 2012041638
LENT ffirs.tex 4/12/2012 14: 28 Page iii To Tom Finke, Pat Malone, the late Katy McShane, and all the other amazing teachers at the Trinity School campuses in South Bend, IN, Eagan, MN, and Falls Church, VA.
LENT ffirs.tex 4/12/2012 14: 28 Page iv
LENT ftoc.tex 6/11/2012 15: 48 Page v Contents Preface I MATLAB Programming ix 1 1 Getting Started 3 1.1 Running the MATLAB IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Manipulating windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2 MATLAB variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Variable assignment statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Variable workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.3 Numbers and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5 Writing simple MATLAB scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.6 A few words about errors and debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.7 Using the debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 14 2 Strings and Vectors 20 2.1 String basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.2 Using the disp command to print a variable’s value . . . . . . . . . . . . . . . . . . . . . . . 22 2.3 Getting information from the user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 22 2.4 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 23 2.5 Operations on vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.6 Special vector functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Statistical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.7 Using rand and randi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3 Plotting 34 3.1 The plot command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.2 Tabulating and plotting a simple function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.3 Bar graphs and histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.4 Drawing several plots on one graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Multiple plots with a single plot command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Combining multiple plots with a hold command . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.5 Adding lines and text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 v
LENT ftoc.tex 6/11/2012 15: 48 Page vi vi Contents 4 Matrices 56 4.1 Entering and manipulating matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2 Operations on matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.3 Solving linear systems: The backslash operator. . . . . . . . . . . . . . . . . . . . . .. . . . . . 65 Extended example: Solving circuit problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4.4 Special matrix functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5 Control Flow Commands 75 5.1 Conditional execution: The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.3 Logical variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.4 for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.5 while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.6 Other control flow commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Switch-case statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Break statement (not recommended) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 6 Animation 94 6.1 Basic animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 95 6.2 Animating function plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 99 6.3 Kinematics of motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 103 One-dimensional motion: Constant speed . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 103 Motion with constant acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Time-marching dynamics: Nonconstant force . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 7 Writing Your Own MATLAB Functions 117 7.1 MATLAB function files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 118 Declaring MATLAB functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 119 7.2 Function inputs and outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.3 Local workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.4 Multiple outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 7.5 Function files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 7.6 Other functional forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 121 Subfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Nested functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8 More MATLAB Data Classes and Structures 137 8.1 Cell arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.2 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 8.3 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 140 8.4 Function handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 141 8.5 Other data classes and data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
分享到:
收藏