1 Introduction to MATLAB 1
1.1 TheAdvantages of MATLAB 1
1.2 Disadvantages of MATLAB 3
1.3 The MATLAB Environment 3
1.3.1 The MATLAB Desktop 4
1.3.2 The Command Window 4
1.3.3 The Command History Window 7
1.3.4 The Launch Pad 7
1.3.5 The Edit/Debug Window 8
1.3.6 Figure Windows 9
1.3.7 The MATLAB Workspace 10
1.3.8 The Workspace Browser 11
1.3.9 Getting Help 13
1.3.10 A Few Important Commands 14
1.3.11 The MATLAB Search Path 15
1.4 Using MATLAB as a Srcratchpad 16
1.5 Summary 18
1.5.1 MATLAB Summary 19
1.6 Exercises 19
2 MATLAB Basics 21
2.1 Variables andArrays 21
2.2 Initializing Variables in MATLAB 24
2.2.1 Initializing Variables in Assignment Statements 25
2.2.2 Initializing with Shortcut Expressions 27
2.2.3 Initializing with Built-in Functions 28
2.2.4 Initializing Variables with Keyboard Input 29
2.3 Multidimensional Arrays 31
2.3.1 Storing Multidimensional Arrays in Memory 32
2.3.2 Accessing Multidimensional Arrays with a Single Subscript 32
2.4 Subarrays 34
2.4.1 The end Function 34
2.4.2 Using Subarrays on the LeR-Hand Side of an Assignment Statement 35
2.4.3 Assigning a Scalar to a Subarray 36
2.5 Special Values 37
2.6 Displaying Output Data 39
2.6.1 Changing the Default Format 39
2.6.2 The disp Function 40
2.6.3 Formatted Output with the fprintf Function 40
2.7 Data Files 42
2.8 Scalar and Array Operations 44
2.8.1 Scalar Operations 45
2.8.2 Array and Matrix Operations 45
2.9 Hierarchy of Operations 48
2.10 Built-in MATLAB Functions 51
2.10.1 Optional Results 51
2.10.2 Using MATLAB Functions with Array Inputs 52
2.10.3 Common MATLAB Functions 52
2.11 Introduction to Plotting 52
2.11.1 Using Simple xy Plots 54
2.11.2 Printing a Plot 55
2.11.3 Multiple Plots 56
2.11.4 Line Color,Line Style,Marker Style,and Legends 56
2.11.5 Logarithmic Scales 58
2.12 Examples 59
2.13 Debugging MATLAB Programs 67
2.14 Summary 69
2.14.1 Summary ofGood Programming Practice 69
2.14.2 MATLAB Summary 70
2.15 Exercises 73
3 Branching Statements and Program Design 81
3.1 Introduction to Top-Down Design Techniques 81
3.2 Use of Pseudocode 86
3.3 Relational and Logical Operators 87
3.3.1 Relational Operators 87
3.3.2 A Caution about the==and~=Operators 89
3.3.3 Logic Operators 90
3.3.4 Logical Functions 92
3.4 Branches 94
3.4.1 The if Construct 94
3.4.2 Examples Using if Constructs 96
3.4.3 Notes Concerning the Use of if Constructs 102
3.4.4 The switch Construct 104
3.4.5 The try/catch Construct 106
3.5 Additional Plotting Features 108
3.5.1 Controlling x-and y-axis Plotting Limits 108
3.5.2 Plotting Multiple Plots on the Same Axes 111
3.5.3 Creating Multiple Figures 111
3.5.4 Subplots 112
3.5.5 Enhanced Control ofPlotted Lines 114
3.5.6 Enhanced Control ofText Strings 115
3.5.7 Polar Plots 115
3.5.8 Annotating and Saving Plots 123
3.6 More on Debugging MATLAB Programs 125
3.7 Summary 128
3.7.1 Summary of Good Programming Practice 129
3.7.2 MATLAB Summary 129
3.8 Exercises 130
4 Loops 137
4.1 Thewhile Loop 137
4.2 The for Loop 143
4.2.1 Details of Operation 150
4.2.2 The break and continue Statements 154
4.2.3 Nesting Loops 156
4.3 Logical Arrays andVectorization 157
4.3.1 The Significance of Logical Arrays 158
4.3.2 Creating the Equivalent of if/else Constructs with Logical Arrays 161
4.4 Additional Examples 163
4.5 Summary 178
4.5.1 Summary of Good Programming Practice 178
4.5.2 MATLAB Summary 179
4.6 Exercises 179
5 User-Defined Functions 187
5.1 Introduction to MATLAB Functions 189
5.2 Variable Passing in MATLAB:The Pass-By-Value Scheme 194
5.3 Optional Arguments 204
5.4 Sharing Data Using Global Memory 209
5.5 Preserving Data Between Calls to a Function 217
5.6 Function Functions 222
5.7 Subfunctions and Private Functions 225
5.8 Summary 227
5.8.1 Summary of Good Programming Practice 228
5.8.2 MATLAB Summary 229
5.9 Exercises 229
6 Complex Data,Character Data,and Additional Plot Types 241
6.1 Complex Data 241
6.1.1 Complex Variables 243
6.1.2 Using Complex Numbers with Relational Operators 244
6.1.3 Complex Functions 244
6.1.4 Plotting Complex Data 248
6.2 String Functions 252
6.2.1 String Conversion Functions 252
6.2.2 Creating Two-Dimensional Character Arrays 252
6.2.3 Concatenating Strings 253
6.2.4 Comparing Strings 254
6.2.5 Searching/Replacing Characters within a String 256
6.2.6 Uppercase and Lowercase Conversion 257
6.2.7 Numeric-to-String Conversions 258
6.2.8 String-to-Numeric Conversions 259
6.2.9 Summary 260
6.3 MultidimensionaI Arrays 266
6.4 Additional Two-DimensionaI Plots 268
6.4.1 Additional Types of Two-Dimensional Plots 268
6.4.2 Plotting Functions 273
6.4.3 Histograms 274
6.5 Three-Dimensional Plots 276
6.5.1 Three-Dimensional Line Plots 276
6.5.2 Three-Dimensional Surface,Mesh,and Contour Plots 278
6.6 Summary 281
6.6.1 Summary of Good Programming Practice 281
6.6.2 MATLAB Summary 282
6.7 Exercises 283
7 Sparse Arrays,Cell Arrays,and Structures 287
7.1 SparseArrays 287
7.1.1 The sparse Data Type 289
7.2 CellArrays 294
7.2.1 Creating Cell Arrays 296
7.2.2 Using Braces{}as Cell Constructors 297
7.2.3 Viewing the Contents of Cell Arrays 298
7.2.4 Extending Cell Arrays 298
7.2.5 Deleting Cells in Arrays 300
7.2.6 Using Data in Cell Arrays 300
7.2.7 Cell Arrays of Strings 301
7.2.8 The Significance of Cell Arrays 302
7.2.9 Summary of cell Array Functions 305
7.3 Structure Arrays 306
7.3.1 Creating Structures 306
7.3.2 Adding Fields to Structures 308
7.3.3 Removing Fields from Structures 309
7.3.4 Using Data in Structure Arrays 310
7.3.5 The getfield and setfield Functions 311
7.3.6 Using the size Function with Structure Arrays 312
7.3.7 Nesting Structure Arrays 312
7.3.8 Summary of structure Functions 313
7.4 Summary 314
7.4.1 Summary of Good Programming Practice 315
7.4.2 MATLAB Summary 315
7.5 Exercises 316
8 Input/Output Functions 319
8.1 The textread Function 319
8.2 More about the load and save Commands 321
8.3 An Introduction to MATLAB File Processing 323
8.4 File Opening and Closing 325
8.4.1 The fopen Function 325
8.4.2 The fc lose Function 328
8.5 Binary I/O Functions 328
8.5.1 The fwri te Function 328
8.5.2 The fread Function 329
8.6 Formatted I/O Functions 332
8.6.1 The fprint Function 332
8.6.2 Understanding Format Conversion Specifiers 334
8.6.3 How Format Strings Are Used 336
8.6.4 The fscanf Function 339
8.6.5 The fgetl Function 341
8.6.6 The fgets Function 341
8.7 Comparing Formatted and Binary I/O Functions 342
8.8 File Positioning and Status Functions 347
8.8.1 The exist Function 347
8.8.2 The ferror Function 349
8.8.3 The feof Function 350
8.8.4 The ftell Function 350
8.8.5 The frewind Function 350
8.8.6 The fseek Function 350
8.9 Function uiimport 356
8.10 Summary 358
8.10.1 Summary of Good Programming Practice 359
8.10.2 MATLAB Summary 359
8.11 Exercises 360
9 Handle Graphics 363
9.1 The MATLAB Graphics System 363
9.2 Object Handles 365
9.3 Examining and Changing Object Properties 365
9.3.1 Changing Object Properties at Creation Time 365
9.3.2 Changing Obiect Properties after Creation Time 366
9.4 Using set to List Possible Property Values 372
9.5 User-Defined Date 374
9.6 Finding Objects 375
9.7 Selecting Objects with the Mouse 377
9.8 Position and Units 380
9.7.1 Positions of figure Objects 380
9.7.2 Positions of axes and uicontrol Obiects 381
9.7.3 Positions of text Obiects 381
9.9 Printer Positions 384
9.10 Default and Factory Properties 385
9.11 Graphics Obiect Properties 387
9.12 Summary 387
9.12.1 Summary of Good Programming Practice 388
9.12.2 MATLAB Summary 388
9.13 Exercises 387
10 Graphical User Interfaces 391
10.1 How a Graphical User InterfaceWorks 391
10.2 Creating and Displaying a Graphical User Interface 392
10.2.1 A Look Under the Hood 402
10.2.2 The Structure of a Callback Subfunction 404
10.2.3 Adding Application Data to a Figure 405
10.2.4 A Few Useful Functions 406
10.3 Obiect Properties 406
10.4 GraphicaI User Interface Components 407
10.4.1 Text Fields 409
10.4.2 Edit Boxes 409
10.4.3 Frames 409
10.4.4 Pushbuttons 409
10.4.5 Toggle Buttons 411
10.4.6 Checkboxes and Radio Buttons 411
10.4.7 Popup Menus 414
10.4.8 List Boxes 414
10.4.9 Sliders 417
10.5 Dialog Boxes 422
10.5.1 Error and Warning Dialog Boxes 423
10.5.2 Input Dialog Boxes 423
10.5.3 The uigetfile and uisetfile Dialog Boxes 424
10.6 Menus 425
10.6.1 Suppressing the Default Menu 425
10.6.2 Creating Your Own Menus 427
10.6.3 Accelerator Keys and Keyboard Mnemonics 430
10.6.4 Creating Context Menus 430
10.7 Tips for Creating Efficient GUIs 436
10.7.1 Tool Tips 436
10.7.2 Pcode 437
10.7.3 Additional Enhancements 439
10.8 Summary 443
10.8.1 Summary of Good Programming Practice 445
10.8.2 MATLAB Summary 445
10.9 Exercises 446
Appendix A ASCII Character Set 449
Append ix B Answers to Quizzes 451
Index 465