Matrices and Arrays Matrices and Magic Squares 42 Entering Matrices sum,transpose, and diag Subscripts 4-6 The Colon Operator The magic Function 4-8 expressions 4-10 Variables 4-10 Nun 4-10 Functions 4-11 Examples of Expressions 4-13 Working with Matrices 4-14 Generating Matrices 414 The load Function 4-15 M-Files 4-15 Concatenation 4-16 Deleting Rows and colt umns 4-17 More About Matrices and arrays 4-18 Linear algebra 4-18 Arrays Multivariate data 4-24 Scalar Expansion Logical Subscripting The find Function 567 Controlling Command Window Input and Output 428 The format Function ..428 Suppressing Output 4-30 Entering Long Statements 4-30 Command Line Editing 4-30
iii 4 Matrices and Arrays Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Entering Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 sum, transpose, and diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Subscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 The Colon Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 The magic Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11 Examples of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Working with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 Generating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 The load Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15 M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16 Deleting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17 More About Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . 4-18 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21 Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24 Scalar Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25 Logical Subscripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26 The find Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-27 Controlling Command Window Input and Output . . . . . . . 4-28 The format Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28 Suppressing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30 Entering Long Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30 Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Graphics 5 Overview of MATLAB Plotting 2 The Plotting Process 5-2 5 Figure Tools 57 Graphs Within a fi Selecting Plot Types ng Plots Plot editing Mode Using Functions to Edit graphs 5-19 Examples- Using MATLAB Plotting Tools 520 Modifying the graph Data Source Preparing Graphs for Presentation 529 Modify the graph to Enhance the Presentation 530 Printing the graph Exporting the graph 5-35 Basic Plotting Functions 5-38 Creating a Plot Multiple Data Sets in One graph 5-40 Specifying Line Styles and Colors Plotting Lines and Markers 5-41 Imaginary and Complex Data Adding Plots to an Existing graph 5-44 Figure Windows 5-46 Multiple Plots in One Figure Controlling the Axes 5-48 Axis Labels and Titles 5-49 Saving Figures 5-51 Mesh and Surface plots Visualizing Functions of Two variables Images 5-58 Reading and Writing Images 5-59
iv Contents 5 Graphics Overview of MATLAB Plotting . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 The Plotting Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Graph Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Figure Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Arranging Graphs Within a Figure . . . . . . . . . . . . . . . . . . . . . 5-13 Selecting Plot Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14 Editing Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Plot Editing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Using Functions to Edit Graphs . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Examples — Using MATLAB Plotting Tools . . . . . . . . . . . . . 5-20 Modifying the Graph Data Source . . . . . . . . . . . . . . . . . . . . . . 5-27 Preparing Graphs for Presentation . . . . . . . . . . . . . . . . . . . . 5-29 Modify the Graph to Enhance the Presentation . . . . . . . . . . . 5-30 Printing the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-33 Exporting the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-35 Basic Plotting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38 Creating a Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38 Multiple Data Sets in One Graph . . . . . . . . . . . . . . . . . . . . . . . 5-40 Specifying Line Styles and Colors . . . . . . . . . . . . . . . . . . . . . . . 5-41 Plotting Lines and Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-41 Imaginary and Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . 5-43 Adding Plots to an Existing Graph . . . . . . . . . . . . . . . . . . . . . . 5-44 Figure Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-46 Multiple Plots in One Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-46 Controlling the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-48 Axis Labels and Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-49 Saving Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-51 Mesh and Surface Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-52 Visualizing Functions of Two Variables . . . . . . . . . . . . . . . . . . 5-52 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-58 Reading and Writing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-59
Printing Graphics Handle graphics 562 Using the handle Graphics Objects 5-63 Setting Object Properties Specifying the Axes or Figure 5-68 Finding the Handles of Existing Objects Animations Erase mode method Creating Movies Programming 6 Flow Control 6-2 if switch and case while continue 6-5 break ...6-6 try -catch return Other Data Structures 6-8 Multidimensional Arrays Cell arrays 6-10 Characters and Text Structures 6-15 Scripts and Functions 6-19 Functions Types of Functions Global variables 6-24
v Printing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-60 Handle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-62 Using the Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-62 Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-63 Setting Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-65 Specifying the Axes or Figure . . . . . . . . . . . . . . . . . . . . . . . . . . 5-68 Finding the Handles of Existing Objects . . . . . . . . . . . . . . . . . 5-69 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-71 Erase Mode Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-71 Creating Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-73 6 Programming Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 switch and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 try - catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 Other Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10 Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15 Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20 Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Passing String Arguments to Functions The eval Function 6-26 Function handles 6-27 Function Functions 6-2 Vectorization 630 reallocation Creating Graphical User Interfaces What Is guide? 7-2 Laying Out a gui 7-3 Starting GUIDE The layout edito Programming a Gui 7-6 Desktop Tools and Development Environment 8 Desktop Overview Arranging the Desktop ...8-3 Start button Command window and Command History 8-5 8-5 Command History 8-6 elp Browser Current Directory Browser and Search Path 8-10 Current directory 8-10 Search Path
vi Contents Passing String Arguments to Functions . . . . . . . . . . . . . . . . . . 6-25 The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-26 Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27 Function Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30 Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30 7 Creating Graphical User Interfaces What Is GUIDE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Laying Out a GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3 Starting GUIDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3 The Layout Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4 Programming a GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 8 Desktop Tools and Development Environment Desktop Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2 Arranging the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3 Start Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3 Command Window and Command History . . . . . . . . . . . . . . . 8-5 Command Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5 Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6 Help Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7 Current Directory Browser and Search Path . . . . . . . . . . . 8-10 Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10 Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-11
Workspace Browser and Array Editor Workspace Brows 8-12 rray Editor 8-13 Editor/Debugger 814 M-Lint Code Check and Profiler Reports M-Lint Code Check Report 8-16 Profiler Report 8-18 Other Development Environment Features 8-19 Introducing the Symbolic Math Toolbox 9 What Is the Symbolic Math Toolbox? 9-2 Symbolic Objects 93 Creating Symbolic variables and Expressions 9-5 The findsym Command The subs command 9-8 The Default Symbolic Variable 9-9 Symbolic and Numeric Conversions 9-10 Constructing Real and Complex variables 9-12 Creating Abstract Functions Creating Symbolic Math Functions Using Symbolic Expressions 15 Creating an M-File 9-15
vii Workspace Browser and Array Editor . . . . . . . . . . . . . . . . . . 8-12 Workspace Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12 Array Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13 Editor/Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14 M-Lint Code Check and Profiler Reports . . . . . . . . . . . . . . . 8-16 M-Lint Code Check Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16 Profiler Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-18 Other Development Environment Features . . . . . . . . . . . . . 8-19 9 Introducing the Symbolic Math Toolbox What Is the Symbolic Math Toolbox? . . . . . . . . . . . . . . . . . . . . 9-2 Symbolic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3 Creating Symbolic Variables and Expressions . . . . . . . . . . . 9-5 The findsym Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7 The subs Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8 The Default Symbolic Variable . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9 Symbolic and Numeric Conversions . . . . . . . . . . . . . . . . . . . 9-10 Constructing Real and Complex Variables . . . . . . . . . . . . . . . . 9-12 Creating Abstract Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-13 Creating Symbolic Math Functions . . . . . . . . . . . . . . . . . . . . 9-15 Using Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15 Creating an M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15