MATLAB® 7
Programming Fundamentals
CuuDuongThanCong.com
/>
How to Contact The MathWorks
Web
Newsgroup
www.mathworks.com/contact_TS.html Technical Support
www.mathworks.com
comp.soft-sys.matlab
Product enhancement suggestions
Bug reports
Documentation error reports
Order status, license renewals, passcodes
Sales, pricing, and general information
508-647-7000 (Phone)
508-647-7001 (Fax)
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site.
MATLAB Programming Fundamentals
© COPYRIGHT 1984–2008 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
The MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
CuuDuongThanCong.com
/>
Revision History
June 2004
October 2004
March 2005
June 2005
September 2005
March 2006
September 2006
March 2007
September 2007
March 2008
October 2008
CuuDuongThanCong.com
First printing
Online only
Online only
Second printing
Online only
Online only
Online only
Online only
Online only
Online only
Online only
New for MATLAB 7.0 (Release 14)
Revised for MATLAB 7.0.1 (Release 14SP1)
Revised for MATLAB 7.0.4 (Release 14SP2)
Minor revision for MATLAB 7.0.4
Revised for MATLAB 7.1 (Release 14SP3)
Revised for MATLAB 7.2 (Release 2006a)
Revised for MATLAB 7.3 (Release 2006b)
Revised for MATLAB 7.4 (Release 2007a)
Revised for Version 7.5 (Release 2007b)
Revised for Version 7.6 (Release 2008a)
Revised for Version 7.7 (Release 2008b)
/>
CuuDuongThanCong.com
/>
Contents
Matrices and Arrays
1
Creating and Concatenating Matrices . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing a Simple Matrix . . . . . . . . . . . . . . . . . . . . . . .
Specialized Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . .
Concatenating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrix Concatenation Functions . . . . . . . . . . . . . . . . . . . . .
Generating a Numeric Sequence . . . . . . . . . . . . . . . . . . . . .
Combining Unlike Classes . . . . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-2
1-3
1-4
1-7
1-8
1-10
1-12
Matrix Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Single Elements . . . . . . . . . . . . . . . . . . . . . . . . . .
Linear Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions That Control Indexing Style . . . . . . . . . . . . . . . .
Accessing Multiple Elements . . . . . . . . . . . . . . . . . . . . . . . .
Using Logicals in Array Indexing . . . . . . . . . . . . . . . . . . . .
Single-Colon Indexing with Different Array Types . . . . . . .
Indexing on Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-17
1-17
1-18
1-18
1-19
1-21
1-25
1-25
Getting Information About a Matrix . . . . . . . . . . . . . . . . .
Dimensions of the Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . .
Classes Used in the Matrix . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Structures Used in the Matrix . . . . . . . . . . . . . . . . . .
1-27
1-27
1-28
1-29
Resizing and Reshaping Matrices . . . . . . . . . . . . . . . . . . .
Expanding the Size of a Matrix . . . . . . . . . . . . . . . . . . . . . .
Diminishing the Size of a Matrix . . . . . . . . . . . . . . . . . . . . .
Reshaping a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preallocating Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-30
1-30
1-34
1-35
1-37
Shifting and Sorting Matrices . . . . . . . . . . . . . . . . . . . . . .
Shift and Sort Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Shifting the Location of Matrix Elements . . . . . . . . . . . . . .
Sorting the Data in Each Column . . . . . . . . . . . . . . . . . . . .
Sorting the Data in Each Row . . . . . . . . . . . . . . . . . . . . . . .
1-40
1-40
1-40
1-42
1-42
v
CuuDuongThanCong.com
/>
Sorting Row Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-43
Operating on Diagonal Matrices . . . . . . . . . . . . . . . . . . . .
Diagonal Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing a Matrix from a Diagonal Vector . . . . . . . . . .
Returning a Triangular Portion of a Matrix . . . . . . . . . . . .
Concatenating Matrices Diagonally . . . . . . . . . . . . . . . . . . .
1-45
1-45
1-45
1-46
1-46
Empty Matrices, Scalars, and Vectors . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Empty Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-47
1-47
1-48
1-50
1-51
Full and Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sparse Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-53
1-53
1-53
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Multidimensional Arrays . . . . . . . . . . . . . . . . . . .
Accessing Multidimensional Array Properties . . . . . . . . . .
Indexing Multidimensional Arrays . . . . . . . . . . . . . . . . . . .
Reshaping Multidimensional Arrays . . . . . . . . . . . . . . . . . .
Permuting Array Dimensions . . . . . . . . . . . . . . . . . . . . . . . .
Computing with Multidimensional Arrays . . . . . . . . . . . . .
Organizing Data in Multidimensional Arrays . . . . . . . . . . .
Multidimensional Cell Arrays . . . . . . . . . . . . . . . . . . . . . . .
Multidimensional Structure Arrays . . . . . . . . . . . . . . . . . . .
1-55
1-55
1-57
1-61
1-61
1-65
1-67
1-69
1-70
1-72
1-73
Summary of Matrix and Array Functions . . . . . . . . . . . .
1-75
Classes (Data Types)
2
Overview of MATLAB Classes . . . . . . . . . . . . . . . . . . . . . . .
Fundamental Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How to Use the Different Classes . . . . . . . . . . . . . . . . . . . .
vi
Contents
CuuDuongThanCong.com
/>
2-2
2-2
2-3
Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . .
Display Format for Numeric Values . . . . . . . . . . . . . . . . . .
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-5
2-5
2-5
2-13
2-23
2-24
2-26
2-26
2-28
Logical Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of Logical Classes . . . . . . . . . . . . . . . . . . . . . . . . .
Identifying Logical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions that Return a Logical Result . . . . . . . . . . . . . . . .
Using Logical Arrays in Conditional Statements . . . . . . . .
Using Logical Arrays in Indexing . . . . . . . . . . . . . . . . . . . .
2-32
2-32
2-33
2-34
2-36
2-37
Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .
Cell Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching and Replacing . . . . . . . . . . . . . . . . . . . . . . . . . . .
Converting from Numeric to String . . . . . . . . . . . . . . . . . . .
Converting from String to Numeric . . . . . . . . . . . . . . . . . . .
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-38
2-38
2-43
2-45
2-58
2-61
2-62
2-64
2-66
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Data in Structure Arrays . . . . . . . . . . . . . . . . . . .
Using Dynamic Field Names . . . . . . . . . . . . . . . . . . . . . . . .
Finding the Size of Structure Arrays . . . . . . . . . . . . . . . . . .
Adding Fields to Structures . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting Fields from Structures . . . . . . . . . . . . . . . . . . . . . .
Applying Functions and Operators . . . . . . . . . . . . . . . . . . .
Writing Functions to Operate on Structures . . . . . . . . . . . .
Organizing Data in Structure Arrays . . . . . . . . . . . . . . . . .
Nesting Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-69
2-69
2-70
2-73
2-75
2-76
2-77
2-78
2-78
2-79
2-80
2-86
2-87
Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-88
vii
CuuDuongThanCong.com
/>
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cell Array Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referencing Cells of a Cell Array . . . . . . . . . . . . . . . . . . . . .
Deleting Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reshaping Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Replacing Lists of Variables with Cell Arrays . . . . . . . . . . .
Applying Functions and Operators . . . . . . . . . . . . . . . . . . .
Organizing Data in Cell Arrays . . . . . . . . . . . . . . . . . . . . . .
Nesting Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Converting Between Cell and Numeric Arrays . . . . . . . . . .
Cell Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-88
2-89
2-90
2-94
2-101
2-101
2-102
2-103
2-104
2-105
2-107
2-108
2-109
Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Function Handle . . . . . . . . . . . . . . . . . . . . . . . . .
Calling a Function Using Its Handle . . . . . . . . . . . . . . . . . .
Handling Values Returned From a Call . . . . . . . . . . . . . . .
Applications of Function Handles . . . . . . . . . . . . . . . . . . . .
Saving and Loading Function Handles . . . . . . . . . . . . . . . .
Advanced Operations on Function Handles . . . . . . . . . . . .
Functions That Operate on Function Handles . . . . . . . . . .
2-110
2-110
2-110
2-113
2-114
2-115
2-120
2-121
2-127
Map Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of the Map Data Structure . . . . . . . . . . . . . . . . . .
Description of the Map Class . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Examining the Contents of the Map . . . . . . . . . . . . . . . . . .
Reading and Writing Using a Key Index . . . . . . . . . . . . . . .
Modifying Keys and Values in the Map . . . . . . . . . . . . . . . .
Mapping to Different Value Types . . . . . . . . . . . . . . . . . . . .
Methods Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-128
2-128
2-129
2-131
2-134
2-135
2-138
2-140
2-142
Definìng Your Own Classes . . . . . . . . . . . . . . . . . . . . . . . . . 2-143
Sun Java Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-144
viii
Contents
CuuDuongThanCong.com
/>
Basic Program Components
3
MATLAB Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic Command Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering More Than One Command on a Line . . . . . . . . . .
Assigning to Multiple Outputs . . . . . . . . . . . . . . . . . . . . . . .
Commands that Call MATLAB Functions . . . . . . . . . . . . . .
3-2
3-2
3-3
3-3
3-5
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Shell Escape Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-6
3-6
3-7
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Guidelines to Using Variables . . . . . . . . . . . . . . . . . . . . . . .
Scope of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lifetime of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-9
3-9
3-13
3-17
3-17
3-19
Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-20
Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-21
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-23
3-23
3-24
3-25
3-32
Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is a Comma-Separated List? . . . . . . . . . . . . . . . . . . .
Generating a Comma-Separated List . . . . . . . . . . . . . . . . .
Assigning Output from a Comma-Separated List . . . . . . . .
Assigning to a Comma-Separated List . . . . . . . . . . . . . . . .
How to Use the Comma-Separated Lists . . . . . . . . . . . . . . .
Fast Fourier Transform Example . . . . . . . . . . . . . . . . . . . .
3-34
3-34
3-34
3-36
3-37
3-38
3-40
Program Control Statements . . . . . . . . . . . . . . . . . . . . . . .
Conditional Control — if, switch . . . . . . . . . . . . . . . . . . . . .
3-42
3-42
ix
CuuDuongThanCong.com
/>
Loop Control — for, while, continue, break . . . . . . . . . . . . .
Error Control — try, catch . . . . . . . . . . . . . . . . . . . . . . . . . .
Program Termination — return . . . . . . . . . . . . . . . . . . . . . .
3-46
3-49
3-50
Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Date Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Conversions Between Date Formats . . . . . . . . . . . . . . . . . .
Date String Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Current Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-51
3-51
3-51
3-53
3-54
3-55
3-56
3-57
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-59
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-59
MATLAB Regular Expression Functions . . . . . . . . . . . . . . 3-60
Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-61
Character Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-64
Grouping Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-65
Nonmatching Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-67
Positional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-67
Lookaround Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-68
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-74
Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-77
Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-82
Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-84
Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . 3-87
String Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-96
Handling Multiple Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 3-98
Operator Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-101
Symbol Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Asterisk — * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
At — @ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Colon — : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comma — , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Curly Braces — { } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dot — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dot-Dot — .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dot-Dot-Dot (Ellipsis) — ... . . . . . . . . . . . . . . . . . . . . . . . . . .
Dot-Parentheses — .( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exclamation Point — ! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parentheses — ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
x
Contents
CuuDuongThanCong.com
/>
3-108
3-108
3-109
3-110
3-111
3-112
3-112
3-113
3-113
3-114
3-115
3-115
Percent — % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Percent-Brace — %{ %} . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Semicolon — ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Single Quotes — ’ ’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Space Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Slash and Backslash — / \ . . . . . . . . . . . . . . . . . . . . . . . . . .
Square Brackets — [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-115
3-116
3-116
3-117
3-118
3-118
3-119
Internal MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
M-File Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overloaded MATLAB Functions . . . . . . . . . . . . . . . . . . . . .
3-120
3-120
3-120
3-121
3-122
Functions and Scripts
4
Program Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Bugs Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cleaning Up the Program . . . . . . . . . . . . . . . . . . . . . . . . . . .
Improving Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checking It In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
4-2
4-2
4-4
4-5
4-5
4-6
Working with M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic Parts of an M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Simple M-File . . . . . . . . . . . . . . . . . . . . . . . . . . .
Providing Help for Your Program . . . . . . . . . . . . . . . . . . . .
Cleaning Up the M-File When Done . . . . . . . . . . . . . . . . . .
Creating P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-7
4-7
4-7
4-8
4-13
4-15
4-16
4-17
M-File Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . .
M-File Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
M-File Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Organizing Your Functions . . . . . . . . . . . . . . . . . . . . . . . . .
4-19
4-19
4-20
4-21
4-22
xi
CuuDuongThanCong.com
/>
Identifying Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-23
Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Command vs. Function Syntax . . . . . . . . . . . . . . . . . . . . . .
What Happens When You Call a Function . . . . . . . . . . . . .
Determining Which Function Gets Called . . . . . . . . . . . . . .
Calling External Functions . . . . . . . . . . . . . . . . . . . . . . . . .
Running External Programs . . . . . . . . . . . . . . . . . . . . . . . .
4-25
4-25
4-34
4-34
4-37
4-38
Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Certain Argument Types . . . . . . . . . . . . . . . . . . . .
Passing Arguments in Structures or Cell Arrays . . . . . . . .
Assigning Output Arguments . . . . . . . . . . . . . . . . . . . . . . . .
Checking the Number of Input Arguments . . . . . . . . . . . . .
Passing Variable Numbers of Arguments . . . . . . . . . . . . . .
Parsing Inputs with inputParser . . . . . . . . . . . . . . . . . . . . .
Passing Optional Arguments to Nested Functions . . . . . . .
Returning Modified Input Arguments . . . . . . . . . . . . . . . . .
4-39
4-39
4-39
4-41
4-43
4-45
4-47
4-50
4-60
4-63
Types of Functions
5
xii
Overview of MATLAB Function Types . . . . . . . . . . . . . . .
5-2
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing an Anonymous Function . . . . . . . . . . . . . . . .
Arrays of Anonymous Functions . . . . . . . . . . . . . . . . . . . . .
Outputs from Anonymous Functions . . . . . . . . . . . . . . . . . .
Variables Used in the Expression . . . . . . . . . . . . . . . . . . . .
Examples of Anonymous Functions . . . . . . . . . . . . . . . . . . .
5-3
5-3
5-6
5-7
5-8
5-11
Primary M-File Functions . . . . . . . . . . . . . . . . . . . . . . . . . .
5-15
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calling Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variable Scope in Nested Functions . . . . . . . . . . . . . . . . . .
5-16
5-16
5-18
5-19
Contents
CuuDuongThanCong.com
/>
Using Function Handles with Nested Functions . . . . . . . .
Restrictions on Assigning to Variables . . . . . . . . . . . . . . . .
Examples of Nested Functions . . . . . . . . . . . . . . . . . . . . . . .
5-21
5-26
5-27
Subfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calling Subfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Help for a Subfunction . . . . . . . . . . . . . . . . . . . . .
5-33
5-33
5-34
5-34
Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Private Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Help for a Private Function . . . . . . . . . . . . . . . . .
5-35
5-35
5-35
5-36
Overloaded Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-37
Using Objects
6
MATLAB Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects In the MATLAB Language . . . . . . . . . . . . . . . . . . .
Other Kinds of Objects Used by MATLAB . . . . . . . . . . . . .
6-2
6-2
6-3
6-3
General Purpose Vs. Specialized Arrays . . . . . . . . . . . . .
How They Differ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using General-Purpose Variables . . . . . . . . . . . . . . . . . . . .
Using Specialized Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-5
6-5
6-5
6-6
Key Object Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Classes Describe How to Create Objects . . . . . . . . . . . . . . .
Properties Contain Data . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods Implement Operations . . . . . . . . . . . . . . . . . . . . . .
6-8
6-8
6-8
6-8
6-9
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
When to Use Package Names . . . . . . . . . . . . . . . . . . . . . . . .
6-11
6-11
6-11
xiii
CuuDuongThanCong.com
/>
Accessing Object Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Listing Public Properties . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-14
6-14
6-14
6-15
Calling Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Operations Can You Perform . . . . . . . . . . . . . . . . . . .
Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class of Objects Returned by Methods . . . . . . . . . . . . . . . .
6-16
6-16
6-16
6-18
Desktop Tools Are Object Aware . . . . . . . . . . . . . . . . . . . .
Tab Completion Works with Objects . . . . . . . . . . . . . . . . . .
Editing Objects with the Variable Editor . . . . . . . . . . . . . .
6-19
6-19
6-19
Getting Information About Objects . . . . . . . . . . . . . . . . . .
The Class of Workspace Variables . . . . . . . . . . . . . . . . . . . .
Information About Class Members . . . . . . . . . . . . . . . . . . .
Logical Tests for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting Help for MATLAB Objects . . . . . . . . . . . . . . . . . . .
6-21
6-21
6-23
6-23
6-24
6-25
Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . .
Handle Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . .
Testing for Handle or Value Class . . . . . . . . . . . . . . . . . . . .
6-26
6-26
6-26
6-27
6-29
Data Import and Export
7
xiv
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Supported File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Other MATLAB I/O Capabilities . . . . . . . . . . . . . . . . . . . . .
Functions Used in File Management . . . . . . . . . . . . . . . . . .
7-2
7-2
7-4
7-6
Supported File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-8
Using the Import Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-11
Contents
CuuDuongThanCong.com
/>
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Starting the Import Wizard . . . . . . . . . . . . . . . . . . . . . . . . .
Previewing Contents of the File or Clipboard [Text only] . .
Specifying Delimiters and Header Format [Text only] . . . .
Determining Assignment to Variables . . . . . . . . . . . . . . . . .
Automated M-Code Generation . . . . . . . . . . . . . . . . . . . . . .
Writing Data to the Workspace . . . . . . . . . . . . . . . . . . . . . .
7-11
7-11
7-13
7-15
7-16
7-19
7-22
Accessing Files with Memory-Mapping . . . . . . . . . . . . . .
Overview of Memory-Mapping . . . . . . . . . . . . . . . . . . . . . . .
The memmapfile Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing a memmapfile Object . . . . . . . . . . . . . . . . . . .
Reading a Mapped File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing to a Mapped File . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods of the memmapfile Class . . . . . . . . . . . . . . . . . . . .
Deleting a Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Memory-Mapping Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-24
7-24
7-28
7-30
7-44
7-49
7-57
7-59
7-60
Exporting Data to MAT-Files . . . . . . . . . . . . . . . . . . . . . . .
MAT-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the save Function . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Appending to an Existing File . . . . . . . . . . . . . . . . . . . . . . .
Data Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unicode Character Encoding . . . . . . . . . . . . . . . . . . . . . . . .
Optional Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . .
Storage Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving from External Programs . . . . . . . . . . . . . . . . . . . . . .
7-65
7-65
7-65
7-66
7-67
7-67
7-69
7-70
7-71
7-72
Importing Data From MAT-Files . . . . . . . . . . . . . . . . . . . .
Using the load Function . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Previewing MAT-File Contents . . . . . . . . . . . . . . . . . . . . . .
Loading Into a Structure . . . . . . . . . . . . . . . . . . . . . . . . . . .
Loading Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Loading ASCII Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-73
7-73
7-73
7-74
7-74
7-75
Importing Text Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MATLAB Import Wizard . . . . . . . . . . . . . . . . . . . . . . . .
Using Import Functions with Text Data . . . . . . . . . . . . . . .
Importing Numeric Text Data . . . . . . . . . . . . . . . . . . . . . . .
Importing Delimited ASCII Data Files . . . . . . . . . . . . . . . .
Importing Numeric Data with Text Headers . . . . . . . . . . .
7-76
7-76
7-76
7-79
7-80
7-81
xv
CuuDuongThanCong.com
/>
Importing Mixed Alphabetic and Numeric Data . . . . . . . . .
Importing from XML Documents . . . . . . . . . . . . . . . . . . . . .
7-82
7-84
Exporting Text Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exporting Delimited ASCII Data Files . . . . . . . . . . . . . . . .
Using the diary Function to Export Data . . . . . . . . . . . . . .
Exporting to XML Documents . . . . . . . . . . . . . . . . . . . . . . .
7-85
7-85
7-87
7-88
7-89
Working with Graphics Files . . . . . . . . . . . . . . . . . . . . . . .
Getting Information About Graphics Files . . . . . . . . . . . . .
Importing Graphics Data . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exporting Graphics Data . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-90
7-90
7-91
7-91
Working with Audio and Video Data . . . . . . . . . . . . . . . . .
Getting Information About Audio/Video Files . . . . . . . . . . .
Importing Audio/Video Data . . . . . . . . . . . . . . . . . . . . . . . . .
Exporting Audio/Video Data . . . . . . . . . . . . . . . . . . . . . . . . .
7-93
7-93
7-93
7-95
Working with Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . 7-98
Microsoft® Excel Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . 7-98
Lotus 123 Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-101
xvi
Using Low-Level File I/O Functions . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Position in a File . . . . . . . . . . . . . . . . . . . . . . . .
Reading Strings Line by Line from Text Files . . . . . . . . . .
Reading Formatted ASCII Data . . . . . . . . . . . . . . . . . . . . . .
Writing Formatted Text Files . . . . . . . . . . . . . . . . . . . . . . .
Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-104
7-104
7-105
7-107
7-109
7-109
7-112
7-113
7-114
7-115
Exchanging Files over the Internet . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Downloading Web Content and Files . . . . . . . . . . . . . . . . . .
Creating and Decompressing Zip Archives . . . . . . . . . . . . .
Sending E-Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Performing FTP File Operations . . . . . . . . . . . . . . . . . . . . .
7-117
7-117
7-117
7-119
7-120
7-122
Contents
CuuDuongThanCong.com
/>
Scientific Data File Formats
8
Common Data Format (CDF) Files . . . . . . . . . . . . . . . . . .
Getting Information About CDF Files . . . . . . . . . . . . . . . . .
Importing Data from a CDF File . . . . . . . . . . . . . . . . . . . . .
Exporting Data to a CDF File . . . . . . . . . . . . . . . . . . . . . . .
8-2
8-2
8-3
8-6
Network Common Data Form (netCDF) Files . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mapping netCDF API Syntax to MATLAB Function
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example: Exploring the Contents of a netCDF File . . . . . .
Example: Reading Data from a netCDF File . . . . . . . . . . .
Example: Storing Data in a netCDF File . . . . . . . . . . . . . .
8-8
8-8
8-9
8-10
8-14
8-14
Flexible Image Transport System (FITS) Files . . . . . . . .
Getting Information About FITS Files . . . . . . . . . . . . . . . .
Importing Data from a FITS File . . . . . . . . . . . . . . . . . . . . .
8-17
8-17
8-18
Hierarchical Data Format (HDF5) Files . . . . . . . . . . . . . .
Using the MATLAB High-Level HDF5 Functions . . . . . . .
Using the MATLAB Low-Level HDF5 Functions . . . . . . . .
8-20
8-20
8-35
Hierarchical Data Format (HDF4) Files . . . . . . . . . . . . . .
Using the HDF Import Tool . . . . . . . . . . . . . . . . . . . . . . . . .
Using the HDF Import Tool Subsetting Options . . . . . . . . .
Using the MATLAB HDF4 High-Level Functions . . . . . . .
Using the HDF4 Low-Level Functions . . . . . . . . . . . . . . . .
8-45
8-45
8-50
8-62
8-65
Error Handling
9
Error Reporting in a MATLAB Application . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting an Exception at the Command Line . . . . . . . . . . . .
Getting an Exception in Your Program Code . . . . . . . . . . .
Generating a New Exception . . . . . . . . . . . . . . . . . . . . . . . .
9-2
9-2
9-2
9-3
9-4
xvii
CuuDuongThanCong.com
/>
Capturing Information About the Error . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MException Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties of the MException Class . . . . . . . . . . . . . . . . . . .
Methods of the MException Class . . . . . . . . . . . . . . . . . . . .
9-5
9-5
9-5
9-7
9-14
Throwing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-16
Responding to an Exception . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The try-catch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Suggestions on How to Handle an Exception . . . . . . . . . . .
9-17
9-17
9-17
9-19
Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reporting a Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Identifying the Cause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-22
9-22
9-23
Warning Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Warning Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Warning Control Statements . . . . . . . . . . . . . . . . . . . . . . . .
Output from Control Statements . . . . . . . . . . . . . . . . . . . . .
Saving and Restoring State . . . . . . . . . . . . . . . . . . . . . . . . .
Backtrace and Verbose Modes . . . . . . . . . . . . . . . . . . . . . . .
9-24
9-24
9-25
9-26
9-28
9-30
9-31
Debugging Errors and Warnings . . . . . . . . . . . . . . . . . . . .
9-34
Program Scheduling
10
xviii
Using a MATLAB Timer Object . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example: Displaying a Message . . . . . . . . . . . . . . . . . . . . .
10-2
10-2
10-3
Creating Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating the Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming the Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10-5
10-5
10-6
Contents
CuuDuongThanCong.com
/>
Working with Timer Object Properties . . . . . . . . . . . . . .
Retrieving the Value of Timer Object Properties . . . . . . . .
Setting the Value of Timer Object Properties . . . . . . . . . . .
10-7
10-7
10-8
Starting and Stopping Timers . . . . . . . . . . . . . . . . . . . . . .
Starting a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Starting a Timer at a Specified Time . . . . . . . . . . . . . . . . . .
Stopping Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Blocking the MATLAB Command Line . . . . . . . . . . . . . . . .
10-10
10-10
10-10
10-11
10-12
Creating and Executing Callback Functions . . . . . . . . .
Associating Commands with Timer Object Events . . . . . . .
Creating Callback Functions . . . . . . . . . . . . . . . . . . . . . . . .
Specifying the Value of Callback Function Properties . . . .
10-14
10-14
10-15
10-17
Timer Object Execution Modes . . . . . . . . . . . . . . . . . . . . .
Executing a Timer Callback Function Once . . . . . . . . . . . .
Executing a Timer Callback Function Multiple Times . . . .
Handling Callback Function Queuing Conflicts . . . . . . . . .
10-19
10-19
10-20
10-21
Deleting Timer Objects from Memory . . . . . . . . . . . . . . . 10-23
Deleting One or More Timer Objects . . . . . . . . . . . . . . . . . . 10-23
Testing the Validity of a Timer Object . . . . . . . . . . . . . . . . . 10-23
Finding Timer Objects in Memory . . . . . . . . . . . . . . . . . . . 10-24
Finding All Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24
Finding Invisible Timer Objects . . . . . . . . . . . . . . . . . . . . . . 10-24
Performance
11
Analyzing Your Program’s Performance . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The M-File Profiler Utility . . . . . . . . . . . . . . . . . . . . . . . . . .
Stopwatch Timer Functions . . . . . . . . . . . . . . . . . . . . . . . . .
11-2
11-2
11-2
11-2
Techniques for Improving Performance . . . . . . . . . . . . .
Vectorizing Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-4
11-4
xix
CuuDuongThanCong.com
/>
Preallocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use Distributed Arrays for Large Datasets . . . . . . . . . . . . .
When Possible, Replace for with parfor (Parallel for) . . . . .
Multithreading Capabilities in MATLAB . . . . . . . . . . . . . .
Limiting M-File Size and Complexity . . . . . . . . . . . . . . . . .
Coding Loops in a MEX-File . . . . . . . . . . . . . . . . . . . . . . . . .
Assigning to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operating on Real Data . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Appropriate Logical Operators . . . . . . . . . . . . . . . . .
Overloading Built-In Functions . . . . . . . . . . . . . . . . . . . . . .
Functions Are Generally Faster Than Scripts . . . . . . . . . .
Load and Save Are Faster Than File I/O Functions . . . . . .
Avoid Large Background Processes . . . . . . . . . . . . . . . . . . .
11-7
11-9
11-9
11-9
11-9
11-10
11-10
11-11
11-11
11-12
11-12
11-12
11-12
MATLAB Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implicit Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Explicit Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-13
11-13
11-14
11-17
Memory Usage
12
Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Memory Allocation for Arrays . . . . . . . . . . . . . . . . . . . . . . .
Data Structures and Memory . . . . . . . . . . . . . . . . . . . . . . . .
12-2
12-2
12-7
Memory Management Functions . . . . . . . . . . . . . . . . . . . . 12-12
The whos Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
xx
Strategies for Efficient Use of Memory . . . . . . . . . . . . . . .
Ways to Reduce the Amount of Memory Required . . . . . . .
Using Appropriate Data Storage . . . . . . . . . . . . . . . . . . . . .
How to Avoid Fragmenting Memory . . . . . . . . . . . . . . . . . .
Reclaiming Used Memory . . . . . . . . . . . . . . . . . . . . . . . . . . .
12-14
12-14
12-16
12-19
12-21
Resolving “Out of Memory” Errors . . . . . . . . . . . . . . . . . .
General Suggestions for Reclaiming Memory . . . . . . . . . . .
Setting the Process Limit . . . . . . . . . . . . . . . . . . . . . . . . . . .
Disabling Java VM on Startup . . . . . . . . . . . . . . . . . . . . . .
12-22
12-22
12-23
12-24
Contents
CuuDuongThanCong.com
/>
Increasing System Swap Space . . . . . . . . . . . . . . . . . . . . . . 12-25
Using the 3GB Switch on Windows Systems . . . . . . . . . . . . 12-26
Freeing Up System Resources on Windows Systems . . . . . 12-26
Programming Tips
13
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-2
Command and Function Syntax . . . . . . . . . . . . . . . . . . . . .
Syntax Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Command and Function Syntaxes . . . . . . . . . . . . . . . . . . . .
Command Line Continuation . . . . . . . . . . . . . . . . . . . . . . . .
Completing Commands Using the Tab Key . . . . . . . . . . . . .
Recalling Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Clearing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Suppressing Output to the Screen . . . . . . . . . . . . . . . . . . . .
13-3
13-3
13-3
13-3
13-4
13-4
13-5
13-5
Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Help Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Help on Functions from the Help Browser . . . . . . . . . . . . .
Help on Functions from the Command Window . . . . . . . . .
Topical Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Paged Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Your Own Help . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Help for Subfunctions and Private Functions . . . . . . . . . . .
Help for Methods and Overloaded Functions . . . . . . . . . . .
13-6
13-6
13-7
13-7
13-7
13-8
13-8
13-9
13-9
Development Environment . . . . . . . . . . . . . . . . . . . . . . . . .
Workspace Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Find and Replace Utility . . . . . . . . . . . . . . . . . . .
Commenting Out a Block of Code . . . . . . . . . . . . . . . . . . . .
Creating M-Files from Command History . . . . . . . . . . . . . .
Editing M-Files in EMACS . . . . . . . . . . . . . . . . . . . . . . . . . .
13-10
13-10
13-10
13-11
13-11
13-11
M-File Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
M-File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Lowercase for Function Names . . . . . . . . . . . . . . . . .
Getting a Function’s Name and Path . . . . . . . . . . . . . . . . . .
13-12
13-12
13-12
13-13
xxi
CuuDuongThanCong.com
/>
What M-Files Does a Function Use? . . . . . . . . . . . . . . . . . . 13-13
Dependent Functions, Built-Ins, Classes . . . . . . . . . . . . . . . 13-14
xxii
Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Input and Output Arguments . . . . . . . . . . . . .
Variable Numbers of Arguments . . . . . . . . . . . . . . . . . . . . .
String or Numeric Arguments . . . . . . . . . . . . . . . . . . . . . . .
Passing Arguments in a Structure . . . . . . . . . . . . . . . . . . . .
Passing Arguments in a Cell Array . . . . . . . . . . . . . . . . . . .
13-15
13-15
13-15
13-16
13-16
13-17
Program Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Planning the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Pseudo-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting the Right Data Structures . . . . . . . . . . . . . . . . . .
General Coding Practices . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming a Function Uniquely . . . . . . . . . . . . . . . . . . . . . . . .
The Importance of Comments . . . . . . . . . . . . . . . . . . . . . . .
Coding in Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making Modifications in Steps . . . . . . . . . . . . . . . . . . . . . . .
Functions with One Calling Function . . . . . . . . . . . . . . . . .
Testing the Final Program . . . . . . . . . . . . . . . . . . . . . . . . . .
13-18
13-18
13-18
13-18
13-19
13-19
13-19
13-20
13-20
13-20
13-20
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MATLAB Debug Functions . . . . . . . . . . . . . . . . . . . . . .
More Debug Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MATLAB Graphical Debugger . . . . . . . . . . . . . . . . . . .
A Quick Way to Examine Variables . . . . . . . . . . . . . . . . . . .
Setting Breakpoints from the Command Line . . . . . . . . . .
Finding Line Numbers to Set Breakpoints . . . . . . . . . . . . .
Stopping Execution on an Error or Warning . . . . . . . . . . . .
Locating an Error from the Error Message . . . . . . . . . . . . .
Using Warnings to Help Debug . . . . . . . . . . . . . . . . . . . . . .
Making Code Execution Visible . . . . . . . . . . . . . . . . . . . . . .
Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-21
13-21
13-21
13-22
13-22
13-23
13-23
13-23
13-23
13-24
13-24
13-24
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rules for Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making Sure Variable Names Are Valid . . . . . . . . . . . . . . .
Do Not Use Function Names for Variables . . . . . . . . . . . . .
Checking for Reserved Keywords . . . . . . . . . . . . . . . . . . . . .
Avoid Using i and j for Variables . . . . . . . . . . . . . . . . . . . . .
Avoid Overwriting Variables in Scripts . . . . . . . . . . . . . . . .
13-25
13-25
13-25
13-26
13-26
13-27
13-27
Contents
CuuDuongThanCong.com
/>
Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-27
Protecting Persistent Variables . . . . . . . . . . . . . . . . . . . . . . 13-27
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-28
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Strings with Concatenation . . . . . . . . . . . . . . . . .
Comparing Methods of Concatenation . . . . . . . . . . . . . . . . .
Store Arrays of Strings in a Cell Array . . . . . . . . . . . . . . . .
Converting Between Strings and Cell Arrays . . . . . . . . . . .
Search and Replace Using Regular Expressions . . . . . . . . .
13-29
13-29
13-29
13-30
13-30
13-31
Evaluating Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Find Alternatives to Using eval . . . . . . . . . . . . . . . . . . . . . .
Assigning to a Series of Variables . . . . . . . . . . . . . . . . . . . .
Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . .
Changing the Counter Variable within a for Loop . . . . . . .
13-32
13-32
13-32
13-33
13-33
MATLAB Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Precedence Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Directory to the Search Path . . . . . . . . . . . . . . . .
Handles to Functions Not on the Path . . . . . . . . . . . . . . . . .
Making Toolbox File Changes Visible to MATLAB . . . . . . .
Making Nontoolbox File Changes Visible to MATLAB . . . .
Change Notification on Windows . . . . . . . . . . . . . . . . . . . . .
13-34
13-34
13-35
13-35
13-35
13-36
13-37
13-37
Program Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using break, continue, and return . . . . . . . . . . . . . . . . . . . .
Using switch Versus if . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MATLAB case Evaluates Strings . . . . . . . . . . . . . . . . . . . . .
Multiple Conditions in a case Statement . . . . . . . . . . . . . . .
Implicit Break in switch-case . . . . . . . . . . . . . . . . . . . . . . . .
Variable Scope in a switch . . . . . . . . . . . . . . . . . . . . . . . . . .
Catching Errors with try-catch . . . . . . . . . . . . . . . . . . . . . .
Nested try-catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Forcing an Early Return from a Function . . . . . . . . . . . . . .
13-38
13-38
13-39
13-39
13-39
13-39
13-40
13-40
13-41
13-41
Save and Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Data from the Workspace . . . . . . . . . . . . . . . . . . . . .
Loading Data into the Workspace . . . . . . . . . . . . . . . . . . . .
Viewing Variables in a MAT-File . . . . . . . . . . . . . . . . . . . . .
Appending to a MAT-File . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-42
13-42
13-42
13-43
13-43
xxiii
CuuDuongThanCong.com
/>
Save and Load on Startup or Quit . . . . . . . . . . . . . . . . . . . . 13-44
Saving to an ASCII File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-44
Files and Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Naming Other Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Filenames as Arguments . . . . . . . . . . . . . . . . . . . .
Passing Filenames to ASCII Files . . . . . . . . . . . . . . . . . . . .
Determining Filenames at Run-Time . . . . . . . . . . . . . . . . .
Returning the Size of a File . . . . . . . . . . . . . . . . . . . . . . . . .
13-45
13-45
13-45
13-46
13-46
13-46
13-46
Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File I/O Function Overview . . . . . . . . . . . . . . . . . . . . . . . . .
Common I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Readable File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Import Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . .
Loading Mixed Format Data . . . . . . . . . . . . . . . . . . . . . . . .
Reading Files with Different Formats . . . . . . . . . . . . . . . . .
Reading ASCII Data into a Cell Array . . . . . . . . . . . . . . . . .
Interactive Input into Your Program . . . . . . . . . . . . . . . . . .
13-48
13-48
13-48
13-49
13-49
13-49
13-50
13-50
13-50
Starting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-51
Getting MATLAB to Start Up Faster . . . . . . . . . . . . . . . . . 13-51
Operating System Compatibility . . . . . . . . . . . . . . . . . . . .
Executing O/S Commands from MATLAB . . . . . . . . . . . . .
Searching Text with grep . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing Paths and Filenames . . . . . . . . . . . . . . . . . . .
Finding the MATLAB Root Directory . . . . . . . . . . . . . . . . .
Temporary Directories and Filenames . . . . . . . . . . . . . . . .
13-52
13-52
13-52
13-52
13-53
13-53
Demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-54
Demos Available with MATLAB . . . . . . . . . . . . . . . . . . . . . 13-54
For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Current CSSM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Archived CSSM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MATLAB Technical Support . . . . . . . . . . . . . . . . . . . . . . . .
Tech Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MATLAB Central . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MATLAB Newsletters (Digest, News & Notes) . . . . . . . . . .
xxiv
Contents
CuuDuongThanCong.com
/>
13-55
13-55
13-55
13-55
13-55
13-55
13-55
MATLAB Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-56
MATLAB Index of Examples . . . . . . . . . . . . . . . . . . . . . . . . 13-56
Index
xxv
CuuDuongThanCong.com
/>