You are on page 1of 31

MATLAB®

Programming Fundamentals

R2013b

How to Contact MathWorks

Web
Newsgroup
www.mathworks.com/contact_TS.html Technical Support
www.mathworks.com

comp.soft-sys.matlab

suggest@mathworks.com
bugs@mathworks.com
doc@mathworks.com
service@mathworks.com
info@mathworks.com

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–2013 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern
the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government)
and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the
government’s needs or is inconsistent in any respect with federal procurement law, the government agrees
to return the Program and Documentation, unused, to The MathWorks, Inc.

Trademarks

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents

MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.

Revision History

June 2004
October 2004
March 2005
June 2005
September 2005
March 2006
September 2006
March 2007
September 2007
March 2008
October 2008
March 2009
September 2009
March 2010
September 2010
April 2011
September 2011
March 2012
September 2012
March 2013
September 2013

First printing
Online only
Online only
Second printing
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
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)
Revised for Version 7.8 (Release 2009a)
Revised for Version 7.9 (Release 2009b)
Revised for Version 7.10 (Release 2010a)
Revised for Version 7.11 (Release 2010b)
Revised for Version 7.12 (Release 2011a)
Revised for Version 7.13 (Release 2011b)
Revised for Version 7.14 (Release 2012a)
Revised for Version 8.0 (Release 2012b)
Revised for Version 8.1 (Release 2013a)
Revised for Version 8.2 (Release 2013b)

.

. . . . . . . . . . . Undefined Functions or Variables . . .Contents Language Syntax Basics 1 Create Variables . . . . .. . . . . . . . . . Function Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 Command vs. . . . . . Avoid Common Syntax Mistakes . . . . . . . . 1-5 Call Functions . . . . . . . . 1-8 1-8 1-8 Case and Space Sensitivity . . . . . . . . . . . . 1-6 Ignore Function Outputs . . . . . . . . . . .. .. . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Create Numeric Arrays . . . . . . . Command and Function Syntaxes . . . 1-12 1-12 1-13 1-14 Common Errors When Calling Functions . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . How MATLAB Recognizes Command Syntax . . . . . . . . . . . . . . . Conflicts with Function Names . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . Conflicting Function and Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16 1-16 1-16 Continue Long Statements on Multiple Lines v . . . . . . . . . . . .. . . . . . . . . . Valid Names . . . . . . . . . . . . . . . . . . . . . 1-7 Variable Names . . . . . . . . . . . . . . . . . . 1-3 . . . . . . . . .

. . . . . . Relational Operators and Empty Arrays . . . . . 2-18 Conditional Statements . . . . . . . . . 2-2 2-2 2-2 2-4 Relational Operators . . . 2-22 Represent Dates and Times in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Date Strings . . . . . . . . . . . . . . . . . . . . . . . . 2-24 2-24 2-25 2-25 Compute Elapsed Time . . . . . . . . . . . . . . . . . . . . . . Serial Date Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 2-7 2-8 Logical Operators . . . . . . . . . . . Precedence of AND and OR Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20 Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Short-Circuit Operators . . . . . . . Relational Operators and Arrays . . . . . . . . . . . . . . . . . . . . . . . Element-Wise Operators and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . Matrix Operations . . . . 2-16 2-16 2-17 Special Values . 2-9 2-9 2-13 2-14 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . 2-29 . . . . . . . . .Program Components 2 vi Contents Array vs. . . 2-26 2-26 2-27 Carryover in Date Vectors and Strings . . . . . . . . Overriding Default Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compute Elapsed Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compute Future Date . . . . . . . . . . . . . . . . . . . . Bit-Wise Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . . . . . . Date Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . Array Operations . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . Generating a Comma-Separated List . . . . . . . . . . . . . . . Steps for Building Expressions . . . . . . . . . . . . . . . . . . . . . Function Names in Variables . 2-74 2-74 2-74 2-75 2-76 2-59 2-60 2-63 vii . Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31 2-31 2-33 2-36 Lookahead Assertions in Regular Expressions . . . . . . . . . . . . . . . . . . . . . Dynamic Match Expressions — (??expr) . . . . . . . . . . Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Logical AND Conditions . . . . . . . . . . . . . 2-30 Regular Expressions . . . . . Assigning Output from a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . .Troubleshooting: Converting Date Vector Returns Unexpected Output . . . . Assigning to a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . . . 2-51 2-51 2-53 2-53 2-55 2-55 Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . 2-66 2-66 2-66 2-68 2-69 2-70 2-72 Alternatives to the eval Function . . . . . . . . . . What Is a Regular Expression? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tokens in Replacement Strings . . . . . Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files with Sequential Names . . . . . . . . 2-57 2-57 2-58 Comma-Separated Lists . . . . . . . . . . . . . . . . . Operators and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fast Fourier Transform Example . . . . . Named Capture . . . . . . . . . . . . . Lookahead Assertions . . . . . . . . . . . . . . . . . . . Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is a Comma-Separated List? . . Commands in Replacement Expressions — ${cmd} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Use the Comma-Separated Lists . . . . . . . . . . . . . . . Variables with Sequential Names . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . Overlapping Matches . . . . . . . . . . . . . . . . Commands That Serve a Functional Purpose — (?@cmd) . . . . . . . . . . . . . . . . . . . . . . . . . . Commands That Modify the Match Expression — (??@cmd) . . . . . . . . . . . . . . . . . . . 2-48 2-48 2-49 2-49 Tokens in Regular Expressions . . . . . . . . . . . . . . . .

. . . . . . . . Square Brackets — [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . .. . . . . . Exclamation Point — ! . . . . . . . . . . . . .. . . . . . .. . . . . . . .. . . . . . . . Colon — : . . . . . . . . . . . At — @ . .. . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . . . Single Quotes — ’ ’ . . . . . . . . . . . . . . . . . . . . . . . . Error Handling . . . . . . . . Percent-Brace — %{ %} .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Plus — + . . . . . . . . . . . . . 2-79 2-79 2-80 2-81 2-82 2-83 2-83 2-84 2-84 2-85 2-86 2-86 2-87 2-87 2-88 2-88 2-89 2-89 2-90 2-90 2-91 Shell Escape Functions Classes (Data Types) Overview of MATLAB Classes 3 Fundamental MATLAB Classes . . . . . . . . . . . . . . . . . . . Dot — . . . . .. . . . . . . . . . . . . . . . . . . .Field Names in Variables . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Parentheses — ( ) .. . . . . . . . . . Dot-Dot-Dot (Ellipsis) — . Tilde — ~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii Contents 3-2 . . . . . . Comma — . . . . . . . . . . . . . . . . . . . . Dot-Dot — . . . . . . . . . . 2-78 Symbol Reference . . . . . . . . . . . . . . Space Character . . . . . . . . . . . Slash and Backslash — / \ . . . . . . Percent — % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .( ) . . . . . . . . . . . .. . 2-77 2-77 . . . . . . . . . . .. . Dot-Parentheses — . . . . . . . . . . . .. . . . . . . . . .. . . . . . . . . . . Semicolon — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . .. . . . . . . . . . . . . . .. . . . . . .. . . Curly Braces — { } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . . . . .. . .. . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . Asterisk — * . . . . . . . . . . . .. . .. . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

. . . Single-Precision Floating Point . . . . . . . . . . . . . . . . . . . . . 4-7 4-7 4-8 4-8 4-10 4-11 4-12 Complex Numbers . . . . . . . . . . . . . . . Creating Floating-Point Data . . . . . . . . . . . Display Format Examples . . . . . . . . . . . . 4-20 4-20 4-20 4-21 Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . Creating Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23 4-23 4-23 4-24 4-14 4-16 4-16 ix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Default Display . . . . . . Double-Precision Floating Point . . . . . . . . . . Setting Numeric Format in a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Largest and Smallest Values for Integer Classes . . . . . . . . . . NaN . . . . . . . . . . . . . . . . . . . . . . . . Floating-Point Functions . . . . . . . . . . . . . . 4-3 4-3 4-4 4-5 4-6 4-6 Floating-Point Numbers . . . . . Avoiding Common Problems with Floating-Point Arithmetic . .Numeric Classes 4 Overview of Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Integer Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integer Functions . . . . . . . . . . 4-22 Display Format for Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Largest and Smallest Values for Floating-Point Classes . . 4-18 4-18 4-19 Infinity and NaN . . . . . . . . . . . . . . . . . . . . . Infinity and NaN Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Complex Number Functions . . . . . Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Integers . . . . . . . . . . . Arithmetic Operations on Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic Operations on Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accuracy of Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . 6-10 . 5-9 Using Logical Arrays in Indexing . . . . . . . . . Identifying Characters in a String . . . . . . . . . . . . . . . . . . . . . . 5-6 5-6 5-6 Using Logical Arrays in Conditional Statements . . . . . . . . . . . . . . . Expanding Character Arrays . . . . . . . . . . . . . . . . . . 5-10 Characters and Strings 6 x Contents Creating Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overview . . . .Function Summary . . . . . . . . . . . . . . . . . . . . Converting to a Cell Array of Strings . . . . Examples of Functions that Return a Logical Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26 The Logical Class 5 Overview of the Logical Class . . . . . . . . . . . . . . . . 5-4 5-4 5-4 Functions that Return a Logical Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 6-2 6-3 6-4 6-5 6-6 Cell Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Examples of Identifying Logical Arrays . . . . . . . . . . . . . . . . . . . . . 5-2 Identifying Logical Arrays . . . . . . . . . . . . . . . . 6-7 6-7 6-8 Formatting Strings . . . . . . Creating a Character String . . Functions for Cell Arrays of Strings . . . . . Function Summary . Creating a Rectangular Character Array . . . . . . . . Working with Space Characters . . . . . . . . . . .

. . Setting Field Width and Precision . Categorizing Characters Within a String . . . . . . . . . 7-2 Convert Table Variables Containing Strings to Categorical Array . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing for Equality Using Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-35 Categorical Arrays 7 Create Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting from a Specific Radix . . . . . . . The Format String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting to a Specific Radix . . . . . . . . . . . . . . . 6-32 6-32 6-33 6-33 6-34 Function Summary .Functions that Use Format Strings . . . Restrictions for Using Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Summary . . . . . . . . . . . . . . . . . . 6-10 6-11 6-12 6-13 6-14 6-20 6-23 String Comparisons . . . . . . . . . . . . . . . . . Constructing the Formatting Operator . . . . . . . . Comparing Strings for Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-28 Converting from Numeric to String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25 6-25 6-26 6-27 Searching and Replacing . . The Formatting Operator . . . . Converting from a Character Equivalent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Input Value Arguments . . . 6-30 6-30 6-31 6-31 6-31 Converting from String to Numeric . . . . . . . . . . . . Converting to a Character Equivalent . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting to a String of Numbers . . . . . . . . . . . . . Function Summary . . . 7-6 xi . . . Converting from a Numeric String . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . 7-46 Tables 8 xii Contents Create a Table . . . . . . 8-17 . . . . . . . . . . . . Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . 7-19 Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Natural Representation of Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . 7-12 Compare Categorical Array Elements . . . . . . . . . . . . 8-9 Add and Delete Table Variables . 8-2 Add and Delete Table Rows . . . . . . . 7-39 7-39 7-39 7-40 Ordinal Categorical Arrays . . . . . . Order of Categories . . . . . . . . . . 7-33 Advantages of Using Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . 7-26 7-26 7-26 Work with Protected Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13 Clean Messy and Missing Data in Tables . . . . . How to Create Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-42 7-42 7-42 7-45 Other MATLAB Functions Supporting Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . Common Ways to Access Data Using Categorical Arrays . . . . . . . 7-22 Access Data Using Categorical Arrays . . . . Working with Ordinal Categorical Arrays . . . . . . . . . . .Plot Categorical Data . . . . . . . . . . . Select Data By Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mathematical Ordering for Strings . . . . . . . . . . . .

. . . Descriptions and Table Variable Names . . . . . . . . . 8-28 8-28 8-29 8-33 Calculations on Tables . . . . . . 9-11 Access Data in Nested Structures . . . . . . . . . . . . . . . . . . 8-41 8-41 8-44 8-46 Structures 9 Create a Structure Array . Access Data Using Numeric or Named Indexing . . . 9-20 Memory Requirements for a Structure Array xiii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24 Access Data in a Table . . . . . . . . . . . . . . . 9-16 9-16 9-18 . . . . . . . . . . . . . . Use Table Properties to Store Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Create Array from the Contents of Table . . 9-2 Access Data in a Structure Array . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . Conveniently Store Mixed-Type Data in Single Container . . . . . . . . . . . . . .. . . . . .. . Element-by-Element Organization . . . . . . . . . . . . . . . . . . . . . . .. . . . . .Modify Units. . . . . . . . . Plane Organization . . . . . . . . 9-9 Generate Field Names from Variables . . . . . 8-36 Advantages of Using Tables . . . . . . . .. . . . . . . . . . . . . . . . . 9-6 Concatenate Structures . . . . . . . . . . . . . . . . . . . . . . . 9-14 Ways to Organize Data in Structure Arrays . . Create Table from Subset of Larger Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12 Access Elements of a Nonscalar Struct Array . . . . . . . . . . . . . . . . . Ways to Index into a Table . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . Struct Arrays . . . . . . 10-17 Function Handles 11 xiv Contents What Is a Function Handle? . . . . 10-8 Delete Data from a Cell Array . . 10-9 Combine Cell Arrays . . . . . . . . . . . . Precedence. . . . . . . . . 10-10 Pass Contents of Cell Arrays to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2 Create a Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-11 Preallocate Memory for a Cell Array . . . . . . . . Obtaining Permissions from Class Methods . . . . . . . The Role of Scope. . . . . . . . . . . . . 10-15 Multilevel Indexing to Access Parts of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Function Handles for Anonymous Functions . . . . . 10-14 Cell vs. . . . . . . . 11-2 Creating a Function Handle . . . . . . . . . . . . . . . . . . . . . Arrays of Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cell Arrays 10 What Is a Cell Array? . . . . . . . . . . . . . . . . 10-3 Access Data in a Cell Array . Maximum Length of a Function Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3 11-4 11-4 11-5 11-6 11-6 . . . . . . . . . . . . and Overloading When Creating a Function Handle . . . . . . . . . . 10-5 Add Cells to a Cell Array . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . Example of Passing a Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling a Function with Multiple Outputs . . . . . . Methods of the Map Class . . . . . 11-10 Preserving Data with Nested Functions . . . . . . . 12-4 12-4 12-5 xv . . . . 11-19 Advanced Operations on Function Handles . . . . . . . . . . . 11-27 Map Containers 12 Overview of the Map Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Examining a Function Handle . . . . 11-10 Preserving Data with Anonymous Functions . . . . . . . . . . Properties of the Map Class . . . . . . .Calling a Function Using Its Handle . Example — Using Function Handles in Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2 Description of the Map Class . . . 11-7 11-7 11-8 11-8 11-9 Preserving Data from the Workspace . . . . . . . . . . . . . . . . . . . . . . . . 11-19 Invalid or Obsolete Function Handles . . . . . Save the Handle in a MAT-File for Use in a Later MATLAB Session . . . . . . . . . . . 11-20 11-20 11-21 11-23 Functions That Operate on Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Capture Data Values For Later Use By a Function . . . . . . . . . Comparing Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-11 Applications of Function Handles . . . . . . . . . . . . . . . . . . . . . . . Call Functions Outside of Their Normal Scope . . . Converting to and from a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning a Handle for Use Outside of a Function File . . . . . . . . Pass a Function to Another Function . 11-13 11-13 11-13 11-15 11-18 11-18 Saving and Loading Function Handles . . . . . . . . . .

. .. . . . . . . . . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . Reading From the Map . . . . . . . . 12-18 Mapping to a Structure Array . . . . . . . . . . . . . . .. . . Constructing An Initialized Map Object . . . . . . . . . . . . . . .. . . . . . . . . . . . . ... . . . . . . . .. . . . . . . .. . . .. . . . . .. Removing Keys and Values from the Map .. . .. . .. . . . . . . . .. . . . . . . . . . . . . .. . . ... . Example of Combining Signed with Unsigned . . . . . . Modifying Keys . . . . . . . . .. . .. . . . . . . . . Example of Combining Unlike Integer Sizes . . . . . . . . . . . . . . . . . . Constructing an Empty Map Object . . . . . . . .. . . . . . . . . . . . . . .Creating a Map Object . .. . . .... . . . . . . . . . . . . . . . Adding Key/Value Pairs . . . . . . . .. . . .. . . . . 13-6 Combining Cell Arrays with Non-Cell Arrays . . . . . . . . . . . . . . . .. . . . . . . . 13-2 Combining Unlike Integer Types . . . . . . . . . . .. . . . . . . . 12-6 12-6 12-7 12-8 Examining the Contents of the Map . . . .. 13-7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . 13-3 13-3 13-4 13-4 . . . . . . . . . . . . .. . . . . . . . . . . . .. . . . . 12-11 12-11 12-12 12-13 Modifying Keys and Values in the Map . 12-9 Reading and Writing Using a Key Index . . . . . . . . Combining Map Objects . Overview . . . . . . . . . . 12-15 12-15 12-15 12-16 12-16 Mapping to Different Value Types .. . . . . . .. . . . . . . ... . . . . . . . . . . . . . . 13-8 Combining Integer and Noninteger Data Empty Matrices xvi Contents . . . . . . . . . 12-19 Combining Unlike Classes 13 Valid Combinations of Unlike Classes .. . . . . . . . . . . . Modifying Values . . . . .. . . . . . .. 12-18 Mapping to a Cell Array . . . . . . . . . . . . . . . . . . . . . Building a Map with Concatenation . . .. . . . . . . . . . . .. . . . . . . . Modifying a Copy of the Map ...

13-9 Combining Single and Double Types . . . . . . . . . . . . . . . 14-9 Events are Notices Broadcast to Listening Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2 14-2 14-2 14-3 14-3 14-4 General Purpose Vs. . . . Working with Objects . . . . . . . . . . . . . . . . . 13-10 Using Objects 14 MATLAB Objects . . . . . . . Other Kinds of Objects Used by MATLAB . . . . . . 14-11 When to Use Package Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16 xvii . . . . 14-5 14-5 14-5 14-6 Key Object Concepts . . . Specialized Arrays . . . 13-9 Combining Character and Double Types . . . . . . . . . . . . . . . . What Are Objects and Why Use Them? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Concatenation Examples . . How They Differ . . . . . 14-10 Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . Getting Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling Object Methods 14-14 14-14 14-14 14-15 . . 14-11 Class Constructor . . . . . . . . . . . . . . . . . . . . . 14-8 Properties Contain Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-10 Combining Logical and Double Types . . . . . . . Using General-Purpose Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Listing Public Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Objects In the MATLAB Language . . . . . . . . . . . . . . . . . . . . 14-9 Methods Implement Operations . . . . . . . . . . . . . 14-11 Accessing Object Data . . . . . . . . . . . . . . . . . . . . . 14-8 Classes Describe How to Create Objects . . . . . . . . . . . . . . . . 13-9 Combining Integer and Double Types . . . . . . . . . . . . . . . . . . . . . 14-8 Basic Concepts . . . . . . . . . . . . . . Using Specialized Objects . . . . . . . . . . . . . . . Getting Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . Getting Help for MATLAB Objects . . . . . . . . . . . . . . Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-26 14-26 14-26 14-27 14-31 Destroying Objects . . . . . . . . . . . . . . . . . Information About Class Members . 14-19 Editing Objects with the Variables Editor . . . . . . . . . . . . . . . . . . . 14-19 Getting Information About Objects . . . . . . . . . . . . . . Logical Tests for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-33 Difference Between clear and delete . Two Copy Behaviors . . 14-18 Desktop Tools Are Object Aware . . . . . . 14-16 Class of Objects Returned by Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-33 Object Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-19 Tab Completion Works with Objects . . . . . . . . . . 14-16 Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . Testing for Handle or Value Class . . . . . . . . . . . . . . . . . . .What Operations Can You Perform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-21 14-21 14-23 14-23 14-24 14-25 Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . Displaying Objects . . . . xviii Contents 16-2 . . . . . . . . . . . . . . . . . . The Class of Workspace Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handle Object Copy Behavior . . . . . . . . 14-33 Defining Your Own Classes 15 Scripts and Functions Scripts 16 Create Scripts . . . . .

. . . 16-4 Run Code Sections .. . . . . Best Practice: Passing Arguments . . 17-2 . 16-6 Divide Your File into Code Sections . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Evaluating in Another Workspace . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 17-7 Base and Function Workspaces . . . . . . . . .. . . . . . . Introduction . . . . . . . . . 17-15 Use Automatic Function and Variable Highlighting . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . .. . . . . . . . . . 17-15 Example of Using Automatic Function and Variable Highlighting . . . . . . . . . . . . .. . . . . . . . . . . . 16-9 Change the Appearance of Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . Functions . . . . . . . . . . . . . . . . . .. . . . 16-13 Scripts vs. . 16-16 Function Basics 17 Create Functions in Files . . . . .Add Comments to Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 16-6 Evaluate Code Sections . . . . . . . . . .. . . . . . . . . . . . 16-7 Navigate Among Code Sections in a File . . . . . . . . . . . . . . . . . . . . . . .. . . . Nested Functions . . . . . . . . . . .. . . Persistent Variables . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 17-5 Run Functions in the Editor . . . . . . 17-9 Share Data Between Workspaces . . . . . . . . . 16-12 Use Code Sections with Control Statements and Functions . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. 17-10 17-10 17-10 17-11 17-12 17-12 17-13 Add Help for Your Program Check Variable Scope in Editor . . .. . . . . . . . . . . . . . . . 17-16 xix .. .. . . . . . . . . . . . . . . . . . ... . . 16-8 Example of Evaluating Code Sections . . . . . . Global Variables . . . . . . . .. . . . . . . . . . . . . .

Local and Nested Functions in a File . .Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visibility of Nested Functions . . . . . . Functions with Multiple Inputs or Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Private Functions in a Subfolder . . . . . . . . . . . . What Are Anonymous Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variables in the Expression . . . . . . . . . . . . . . . . . . . . . . . Requirements for Nested Functions . . . . . . . . . . . . . . . . 17-43 Function Arguments 18 xx Contents Find Number of Function Arguments . . . 17-30 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2 Support Variable Number of Inputs . 17-39 Private Functions . . . . . . . . . . . . . . . . . . . Functions with No Inputs . . . . . . . . . . . . . . . . Arrays of Anonymous Functions . . . . . . . . 18-4 Support Variable Number of Outputs . . 17-19 17-19 17-20 17-21 Anonymous Functions . . . . . . . . . . 17-41 Function Precedence Order . . . . . . . . . . . . . . . . . . Using Handles to Store Function Parameters . . . . . . . . . . 18-6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-23 17-23 17-24 17-25 17-26 17-26 17-28 Local Functions . . . . . . . . . . . . . What Are Nested Functions? . . . . . . . . . . Multiple Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . Anonymous Functions Without a File . . . . . . Sharing Variables Between Parent and Nested Functions . . . . . . . 17-32 17-32 17-33 17-33 17-35 17-37 Variables in Nested and Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . 20-2 Document and Share Code Using Examples . . . . . . . . . . . . . . . . . . . . Error Breakpoints . . . . 18-17 Input Parser Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ways to Debug MATLAB Files . . Correct Problems and End Debugging . 18-13 Check Function Inputs with validateattributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4 xxi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2 19-2 19-2 19-5 19-8 19-10 19-11 19-17 19-24 19-26 19-27 19-28 Presenting MATLAB Code 20 Options for Presenting Your Code . . . . . . . . . . . . . . . Set Breakpoints . . . . . . . . . . . . . . . . . . . . . Breakpoints in Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . Conditional Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-22 Debugging MATLAB Code 19 Debugging Process and Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-8 Argument Checking in Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-11 Ignore Function Inputs . . . . . . Preparing for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Validate Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Run a File with Breakpoints . . . . . . . . . Examine Values . . . . . . . . Step Through a File . . 18-14 Parse Function Inputs . Breakpoints in Methods That Overload Functions . . . .

. . . . . . . . . . . . . . . . . . Text Formatting . . . . . . . . . . . . . . . . . . . . . . 20-6 Publishing Markup . . . . . . . . . . . . . . . . . . . . . . HTML Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-8 20-8 20-11 20-13 20-14 20-15 20-16 20-19 20-20 20-22 20-25 20-26 Output Preferences for Publishing . . . . . . . . . . . . . Tips for Using MATLAB Notebooks . . . . . . . . . . . . . . . . . . . . Specify Output File . . . Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . 21-2 21-2 21-4 Check Code for Errors and Warnings . . . . . . . . . . . . . . Save Files . . . . . . . . . . . . . . . 20-29 20-29 20-30 20-31 20-34 20-38 20-40 Create a MATLAB Notebook with Microsoft Word . . Manage a Publish Configuration . . . . . . How to Edit Publishing Options . . . .Publishing MATLAB Code . . . Manipulate Graphics in Publishing Output . . . . . Run Code During Publishing . . . . . . . . . . . . . . . Getting Started with MATLAB Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Save a Publish Setting . . . . . . LaTeX Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Formatting a MATLAB Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sections and Section Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . LaTeX Equations . . . . . . . . . . . . . . Open Existing Files . . . . Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . External Graphics . . . . . . . . . . . . . . . Image Snapshot . . . . . . . . . . . . . . . 21-7 . . . 20-44 20-44 20-46 20-52 20-55 20-56 Coding and Productivity Tips 21 xxii Contents Open and Save Files . . . . . . . . . . . . . . . . . . . . Configuring the MATLAB Notebook Software . . . . . . . . . . . . . . . . . . . Markup Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Evaluating Cells in a MATLAB Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Text and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Navigate to a Specific Location . . . . . . . . Automatically Rename All Functions or Variables in a File . . . . . . . . . . . . . . . . . . . . Navigate Backward and Forward in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Find and Replace Any Text . . . . . . . . . . . .Automatically Check Code in the Editor — Code Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understand the Limitations of Code Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Create a Code Analyzer Message Report . . . . . . . . . . . . . . 21-45 Colors in the MATLAB Editor . . . . . . . . . . . 21-42 Add Reminders to Files . . Find and Replace Functions or Variables in the Current File . . . 21-30 21-30 Go To Location in File . . . . . . . . . . . . . . . Find Text in Multiple File Names or Files . . 21-49 Code Contains %#ok — What Does That Mean? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enable MATLAB Compiler Deployment Messages . . . . . . 21-52 xxiii . . . . . . . . . . . . . . . . . . . . . . . . . . Function Alternative for Finding Text . . . . . . . . . . . . . . . . . . . . . . . Perform an Incremental Search in the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adjust Code Analyzer Message Indicators and Messages . . . . . . . . 21-7 21-12 21-13 21-17 21-18 21-22 Improve Code Readability . . . 21-45 Working with TODO/FIXME Reports . . . . . . . 21-51 MATLAB Code Analyzer Report . . Indenting Code . . . . . . . . . . . Open a File or Variable from Within a File . Right-Side Text Limit Indicator . . . . . . 21-23 21-23 21-25 21-25 Find and Replace Text in Files . . . . Find Any Text in the Current File . . . . . . . . . . . . . . . . . . . . . . Set Bookmarks . . . . . 21-35 21-35 21-39 21-39 21-40 21-30 21-32 21-34 21-34 21-34 21-34 Display Two Parts of a File Simultaneously . . . . . . . . . . . . . . . . . . Understand Code Containing Suppressed Messages . . . . . . . Code Folding — Expand and Collapse Code Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . Overview . . . . . . . . . . . . . . Run a Single Function . . Dependencies Within a Folder . . . 21-52 Changing Code Based on Code Analyzer Messages . . . 21-55 Change Default Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . Detailed Display of Program File Dependencies . . . . . . . . . . . . . . . . . . . 21-56 Set Default Editor in '-nodisplay' mode . . . . . . . . 22-12 22-13 22-13 22-14 22-14 Software Development Error Handling 23 Exception Handling in a MATLAB Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2 22-2 22-2 22-3 Protect Your Source Code . . . . . . . . . . . . . . . 21-56 Set Default Editor . . . . . . . . . . . . . . . . . . . . . . . 22-9 Building a Content Obscured Format with P-Code . . . . . . . . Run Multiple Functions . . Getting an Exception at the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Running the Code Analyzer Report . . . . . . 22-11 Create Hyperlinks that Run Functions . . . 22-9 Building a Standalone Executable . . . . . . . . . . . . . . . Provide Command Options . . . . . . . Simple Display of Program File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting an Exception in Your Program Code . . . . . . . . . . . . . . . 21-56 Programming Utilities 22 Identify Program Dependencies . . . . . . . . . . . . . . . . . Include Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . 21-54 Other Ways to Access Code Analyzer Messages . . xxiv Contents 23-2 23-2 23-2 23-3 . . . .

. . . . . . . . . . . Suggestions on How to Handle an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-16 Respond to an Exception . . 23-18 23-18 23-18 23-20 Clean Up When Functions Complete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Add Identifiers to Warnings and Errors . . . . . . . . . . . . . . . . . . 23-23 23-23 23-25 23-27 23-28 23-28 Issue Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throw Errors . . . . . . . . . . . . . . . . . . . . . . 23-37 Disable and Restore Multiple Warnings . . . . . . . . . . . . . . . . . . . . . . . . 23-14 Throw an Exception . . Add Run-Time Parameters to Your Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-5 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overview . . . . . . . . . . . . . . . . . . . . . . . . . . The try/catch Statement . . . 23-5 The MException Class . . . . . . . 23-40 Display a Stack Trace on a Specific Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-30 23-30 23-30 23-31 23-32 Suppress Warnings . . . . . . . . . . . . . . . 23-7 Methods of the MException Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-34 Turn Warnings On and Off . . . . . . . . . . . . . . . . . . . . . . Using onCleanup Versus try/catch . . . . . . . . . . . . . . 23-41 xxv . . . . . . . . . . Overview . . . 23-5 Properties of the MException Class . Examples of Cleaning Up a Program Upon Exit . . . . . . . . . . . . . . . . Retrieving Information About the Cleanup Routine . . . . . . . . onCleanup in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-4 Capture Information About Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Issue Warnings . . . . . . . . 23-35 Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Generating a New Exception . . . . . . . . . . . 23-38 Change How Warnings Display . . . . . . . . 23-40 Enable Verbose Warnings . . 23-37 Disable and Restore a Particular Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . Time Portions of Code . . Queue Mode . . . . . . . . Time Functions . . . . . . . . . . . . . . . . . . . . . 24-10 24-10 24-12 24-13 Performance 25 Analyzing Your Program’s Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . tic/toc and timeit . . . . . . . . . . 23-42 Program Scheduling 24 Use a MATLAB Timer Object . . . . . . . . . . . . . . . . . . . Associating Commands with Timer Object Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overview of Performance Timing Functions . . . . 25-2 25-2 25-2 25-2 25-3 Profiling for Improving Performance . . Drop Mode (Default) . . Error Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-14 The profile Function . . . . . . . . . . . . . . . . 24-5 24-5 24-6 24-8 Handling Timer Queuing Conflicts . . . . . . . . . . . . . . . . . . . .Use try/catch to Handle Errors . . . . . . . . . . . . 25-12 Profile Detail Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-4 Profiling Process and Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-5 Using the Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . Example: Displaying a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The cputime Function vs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overview . . . . . . 25-4 What Is Profiling? . . . . . . . . . . . 24-2 24-2 24-3 Timer Callback Functions . . . . . Creating Callback Functions . . . . . . . . . . Specifying the Value of Callback Function Properties . . . . . 25-6 Profile Summary Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-20 xxvi Contents .

. . . . . . . Data Structures and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . Indexing Methods for Vectorization . . . . . . . . . . . . . . . . Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions Commonly Used in Vectorizing . . . . . . . . . . . . . Using Appropriate Data Storage . . . . . . . . 26-23 26-23 26-23 26-25 26-25 xxvii . . . . . . . . .Determining Profiler Coverage . . . . Using Appropriate Logical Operators . . . . Memory Allocation for Arrays . . . . . . . . . . . 25-27 Techniques for Improving Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-13 Strategies for Efficient Use of Memory . . . . . . . . . . . . . . . . . . . . . . . . . Additional Tips on Improving Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting the Process Limit . . . . . . . . . . . Ordering. . . . . . . . . . . . . . . . . . . . . . Logical Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . and Counting Operations . Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Increasing System Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-15 26-15 26-17 26-20 26-21 Resolving “Out of Memory” Errors . . . . . . . Reclaiming Used Memory . . . . . . . . . . . Ways to Reduce the Amount of Memory Required . . . . . . . . . . . . . . . 26-12 The whos Function . . . . . . 25-29 25-29 25-30 25-31 25-31 Vectorization . . . . . . . . . . . . . . . . . Setting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assigning Variables . . . . . . . Preallocating Arrays . . . . . . 25-33 25-33 25-34 25-37 25-37 25-39 25-41 25-43 Memory Usage 26 Memory Allocation . . . . . . . . Disabling Java VM on Startup . . . . . Using Vectorization . . . . . . . . . . . . . . . How to Avoid Fragmenting Memory . . . . . . . . . . . . . . General Suggestions for Reclaiming Memory . . . . . . . . . . . . . . . . . . . . 26-2 26-2 26-6 Memory Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-25 27-25 27-28 27-29 Source Control Interface 28 xxviii Contents Source Control Interface on Microsoft Windows . . . . . . . . . . 27-2 27-2 27-3 Check Which Programs Have Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Display Examples . . . . . . . . . 28-2 Set Up Source Control (Microsoft Windows) . . . . . Overview . Identify Your Documentation (info. Custom Help Text . . . . . .m File . .Using the 3GB Switch on Windows Systems . . . . . . . What Is a Contents. . . .m) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Thumbnail Images . . . . . . . . . . . . . . . .m File . . .xml) . . . . . . . . 27-16 27-16 27-17 27-20 27-22 27-23 Display Custom Examples . . . . .xml Files . . . . Address Validation Errors for info. . . . . . . . . 26-26 Freeing Up System Resources on Windows Systems . . . Create a Contents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Check an Existing Contents. . . . . . . . . . . . .m File? . Help Text from the doc Command . . . . . . . . . . . . . . . 28-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-10 Create Help Summary Files (Contents. . . . . . . . . . . 27-13 27-13 27-14 27-14 Display Custom Documentation . . . . . . . . . 26-27 Custom Help and Documentation 27 Create Help for Classes . . . . . . . . . . . . . Create a Table of Contents (helptoc. . . . . . .xml File . . . . . . . . . . . . . . . . . . . . . . . Build a Search Database . . . . . . . . . . . . .xml) . . Elements of the demos. . . . . .

. . . . Function Alternative . Getting the Latest Version of Files for Viewing or Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting a View and Checking Out a Folder with ClearCase Software on UNIX Platforms . . 28-11 28-11 28-11 28-12 Additional Source Control Actions (Microsoft Windows) . . Check Files Out of Source Control . . . . . . . . . . . . 28-24 28-24 28-25 28-25 28-25 Source Control Interface on UNIX Platforms . . . Restriction Against @ Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MATLAB Desktop Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-14 28-14 28-15 28-16 28-18 28-20 28-21 Access Source Control from Editors (Microsoft Windows) . . . . . . . . . . . . . . . . . . .Create Projects in Source Control System . . . . . . . 28-23 Troubleshoot Source Control Problems (Microsoft Windows) . . . . . . . . . . Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Files from the Source Control System . . . . . . Starting the Source Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Viewing Source Control Properties of a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-27 28-27 28-28 28-28 Check In Files (UNIX Platforms) . . . . . . . . . . . . . . . . Register Source Control Project with MATLAB Software . . . . . . . . 28-3 28-5 28-7 28-9 Check Files In and Out (Microsoft Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specify Source Control System with MATLAB Software . . . . . . . Source Control Error: Provider Not Present or Not Installed Properly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing the Working Copy of a File to the Latest Version in Source Control . . . 28-26 Specify Source Control System (UNIX Platforms) . . . . . . . . . . . . . . . . . . . . . . . . . Check Files Into Source Control . . . . . . . . . . . . . . . . Add to Source Control Is the Only Action Available . . . . . . . . . . . . . . Showing File History . More Solutions for Source Control Problems . . . . . . . . . . . . . . . . . . . . . 28-30 xxix . . . . . . . Add Files to Source Control . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . Test Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . 29-10 Write Test Methods Using Verifications . . . . . . . . . . . Test Case with Method-Level Setup Code . . . . . . . . . . . . . . . . . . . . . . . or the Simulink or Stateflow Products . . . . . . . . . 29-6 29-6 29-6 29-7 Select Qualifications . . . . 28-35 28-35 28-32 28-33 28-33 28-35 28-35 Unit Testing 29 Write Simple Test Case with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-12 Write TestClassSetup Method Using Assumptions . . . . . . . . . Function Alternative . . . . . . . .Checking In One or More Files Using the Current Folder Browser . . Checking Out One or More Files Using the Current Folder Browser . . 28-30 Function Alternative . . . . . . . . . . . . . . . . . . . . . . or the Simulink or Stateflow Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Checking Out a Single File Using the Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . Function Alternative . . . . . . . 29-2 Write Setup and Teardown Code with Classes . . . . . . . . . 29-18 xxx Contents . . . . . . . . . . . . . 28-31 Check Out Files (UNIX Platforms) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-30 Checking In One File Using the Editor. . . . . . . . . . . . . . . . . . . Test Case with Class-Level Setup Code . . . . . . . . . . . . . . . 28-32 Undo the Checkout (UNIX Platforms) . Undoing the Checkout for One or More Files Using the Current Folder Browser . . . . . . . . . . . . . . . . . . . . . 29-15 Test for Preconditions Using Assertions . . . . . . . . . . . . . . . . . . . . Impact of Undoing a File Checkout . . . . . . . .

. . . . . . . . . . . . . . . 29-42 29-42 29-42 29-42 Write Function-Based Unit Tests . . . . . . . . Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-30 Run Tests for Various Workflows . . . . . 29-32 29-32 29-32 29-33 29-34 29-34 29-35 Customize Test Runner with Plugins . . . . . 29-23 Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Set Up Example Tests . . . . . . . . . . . . 29-25 Create Simple Test Suites . . . . . . . . . . . . . . . . . Run the Tests . . . . . . . . . . . . . . . Create Test Function . . . . Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-43 29-43 29-46 29-47 Write Simple Test Case Using Functions . . 29-48 Write Test Using Setup and Teardown Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Methods . . . . . . . . . . . . . . . . . . . . . . . 29-37 Analyze Failed Test Results . . . . . . . . . . . . . . . . .Write Helper Function Using Fatal Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-53 Index xxxi . . . Run Test Suites by Name . . . . . . Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Run All Tests in Class or Function . . . . Run Test Suites from Test Array . 29-39 Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . . . 29-36 Analyze Test Case Results . . . . . . . . . . . . . . Run Single Test in Class or Function .