MATLAB®
Programming Fundamentals
R2018a
How to Contact MathWorks
Latest news: [Link]
Sales and services: [Link]/sales_and_services
User community: [Link]/matlabcentral
Technical support: [Link]/support/contact_us
Phone: 508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
MATLAB Programming Fundamentals
© COPYRIGHT 1984–2018 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
[Link]/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
[Link]/patents for more information.
Revision History
June 2004 First printing New for MATLAB 7.0 (Release 14)
October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1)
March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2)
June 2005 Second printing Minor revision for MATLAB 7.0.4
September 2005 Online only Revised for MATLAB 7.1 (Release 14SP3)
March 2006 Online only Revised for MATLAB 7.2 (Release 2006a)
September 2006 Online only Revised for MATLAB 7.3 (Release 2006b)
March 2007 Online only Revised for MATLAB 7.4 (Release 2007a)
September 2007 Online only Revised for Version 7.5 (Release 2007b)
March 2008 Online only Revised for Version 7.6 (Release 2008a)
October 2008 Online only Revised for Version 7.7 (Release 2008b)
March 2009 Online only Revised for Version 7.8 (Release 2009a)
September 2009 Online only Revised for Version 7.9 (Release 2009b)
March 2010 Online only Revised for Version 7.10 (Release 2010a)
September 2010 Online only Revised for Version 7.11 (Release 2010b)
April 2011 Online only Revised for Version 7.12 (Release 2011a)
September 2011 Online only Revised for Version 7.13 (Release 2011b)
March 2012 Online only Revised for Version 7.14 (Release 2012a)
September 2012 Online only Revised for Version 8.0 (Release 2012b)
March 2013 Online only Revised for Version 8.1 (Release 2013a)
September 2013 Online only Revised for Version 8.2 (Release 2013b)
March 2014 Online only Revised for Version 8.3 (Release 2014a)
October 2014 Online only Revised for Version 8.4 (Release 2014b)
March 2015 Online only Revised for Version 8.5 (Release 2015a)
September 2015 Online only Revised for Version 8.6 (Release 2015b)
October 2015 Online only Rereleased for Version 8.5.1 (Release
2015aSP1)
March 2016 Online only Revised for Version 9.0 (Release 2016a)
September 2016 Online only Revised for Version 9.1 (Release 2016b)
March 2017 Online only Revised for Version 9.2 (Release 2017a)
September 2017 Online only Revised for Version 9.3 (Release 2017b)
March 2018 Online only Revised for Version 9.4 (Release 2018a)
Contents
Language
Syntax Basics
1
Continue Long Statements on Multiple Lines . . . . . . . . . . 1-2
Call Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Ignore Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Valid Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Conflicts with Function Names . . . . . . . . . . . . . . . . . . . . 1-5
Case and Space Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Command vs. Function Syntax . . . . . . . . . . . . . . . . . . . . . . 1-9
Command and Function Syntaxes . . . . . . . . . . . . . . . . . . 1-9
Avoid Common Syntax Mistakes . . . . . . . . . . . . . . . . . . 1-10
How MATLAB Recognizes Command Syntax . . . . . . . . . 1-11
Common Errors When Calling Functions . . . . . . . . . . . . 1-13
Conflicting Function and Variable Names . . . . . . . . . . . 1-13
Undefined Functions or Variables . . . . . . . . . . . . . . . . . 1-13
v
Program Components
2
MATLAB Operators and Special Characters . . . . . . . . . . . 2-2
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
String and Character Formatting . . . . . . . . . . . . . . . . . 2-10
Array vs. Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . 2-13
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Compatible Array Sizes for Basic Operations . . . . . . . . . 2-19
Inputs with Compatible Sizes . . . . . . . . . . . . . . . . . . . . 2-19
Inputs with Incompatible Sizes . . . . . . . . . . . . . . . . . . . 2-22
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23
Array Comparison with Relational Operators . . . . . . . . . 2-25
Array Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25
Logic Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-28
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29
Precedence of AND and OR Operators . . . . . . . . . . . . . 2-29
Overriding Default Precedence . . . . . . . . . . . . . . . . . . . 2-30
Average Similar Data Points Using a Tolerance . . . . . . . 2-31
Group Scattered Data Using a Tolerance . . . . . . . . . . . . . 2-34
Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37
Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . 2-39
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-41
What Is a Regular Expression? . . . . . . . . . . . . . . . . . . . 2-41
Steps for Building Expressions . . . . . . . . . . . . . . . . . . . 2-43
Operators and Characters . . . . . . . . . . . . . . . . . . . . . . 2-46
vi Contents
Lookahead Assertions in Regular Expressions . . . . . . . . 2-57
Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . 2-57
Overlapping Matches . . . . . . . . . . . . . . . . . . . . . . . . . . 2-58
Logical AND Conditions . . . . . . . . . . . . . . . . . . . . . . . . 2-58
Tokens in Regular Expressions . . . . . . . . . . . . . . . . . . . . . 2-60
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-60
Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-62
Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-62
Tokens in Replacement Text . . . . . . . . . . . . . . . . . . . . . 2-63
Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-64
Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . 2-66
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66
Dynamic Match Expressions — (??expr) . . . . . . . . . . . . 2-67
Commands That Modify the Match Expression — (??
@cmd) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-68
Commands That Serve a Functional Purpose — (?
@cmd) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-69
Commands in Replacement Expressions — ${cmd} . . . 2-71
Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 2-74
What Is a Comma-Separated List? . . . . . . . . . . . . . . . . 2-74
Generating a Comma-Separated List . . . . . . . . . . . . . . . 2-74
Assigning Output from a Comma-Separated List . . . . . . 2-76
Assigning to a Comma-Separated List . . . . . . . . . . . . . . 2-77
How to Use the Comma-Separated Lists . . . . . . . . . . . . 2-79
Fast Fourier Transform Example . . . . . . . . . . . . . . . . . . 2-81
Alternatives to the eval Function . . . . . . . . . . . . . . . . . . . 2-83
Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . . . 2-83
Variables with Sequential Names . . . . . . . . . . . . . . . . . 2-83
Files with Sequential Names . . . . . . . . . . . . . . . . . . . . . 2-84
Function Names in Variables . . . . . . . . . . . . . . . . . . . . 2-85
Field Names in Variables . . . . . . . . . . . . . . . . . . . . . . . 2-85
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-86
vii
Classes (Data Types)
Overview of MATLAB Classes
3
Fundamental MATLAB Classes . . . . . . . . . . . . . . . . . . . . . . 3-2
Numeric Classes
4
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Creating Integer Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
Arithmetic Operations on Integer Classes . . . . . . . . . . . . 4-4
Largest and Smallest Values for Integer Classes . . . . . . . 4-5
Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Double-Precision Floating Point . . . . . . . . . . . . . . . . . . . 4-7
Single-Precision Floating Point . . . . . . . . . . . . . . . . . . . . 4-7
Creating Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . 4-8
Arithmetic Operations on Floating-Point Numbers . . . . . 4-9
Largest and Smallest Values for Floating-Point
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Accuracy of Floating-Point Data . . . . . . . . . . . . . . . . . . 4-12
Avoiding Common Problems with Floating-Point
Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Creating Complex Numbers . . . . . . . . . . . . . . . . . . . . . 4-17
Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . 4-21
viii Contents
Display Format for Numeric Values . . . . . . . . . . . . . . . . . 4-22
Default Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22
Display Format Examples . . . . . . . . . . . . . . . . . . . . . . . 4-22
Setting Numeric Format in a Program . . . . . . . . . . . . . 4-23
The Logical Class
5
Find Array Elements That Meet a Condition . . . . . . . . . . . 5-2
Apply a Single Condition . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Apply Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Replace Values that Meet a Condition . . . . . . . . . . . . . . . 5-5
Determine if Arrays Are Logical . . . . . . . . . . . . . . . . . . . . . 5-7
Identify Logical Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Test an Entire Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
Test Each Array Element . . . . . . . . . . . . . . . . . . . . . . . . 5-8
Summary Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Reduce Logical Arrays to Single Value . . . . . . . . . . . . . . 5-11
Truth Table for Logical Operations . . . . . . . . . . . . . . . . . 5-14
Characters and Strings
6
Represent Text with Character and String Arrays . . . . . . 6-2
Create Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Create Character Vector . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Create Rectangular Character Array . . . . . . . . . . . . . . . . 6-6
Identify Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
Work with Space Characters . . . . . . . . . . . . . . . . . . . . . . 6-9
Expand Character Arrays . . . . . . . . . . . . . . . . . . . . . . . 6-10
Create String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
ix
Cell Arrays of Character Vectors . . . . . . . . . . . . . . . . . . . 6-20
Convert to Cell Array of Character Vectors . . . . . . . . . . 6-20
Functions for Cell Arrays of Character Vectors . . . . . . . 6-21
Analyze Text Data with String Arrays . . . . . . . . . . . . . . . . 6-23
Test for Empty Strings and Missing Values . . . . . . . . . . . 6-30
Formatting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-35
Fields of the Formatting Operator . . . . . . . . . . . . . . . . . 6-35
Setting Field Width and Precision . . . . . . . . . . . . . . . . . 6-41
Restrictions on Using Identifiers . . . . . . . . . . . . . . . . . . 6-44
Compare Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-46
Search and Replace Text . . . . . . . . . . . . . . . . . . . . . . . . . . 6-53
Convert from Numeric Values to Character Array . . . . . 6-60
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-60
Convert Numbers to Character Codes . . . . . . . . . . . . . . 6-61
Represent Numbers as Text . . . . . . . . . . . . . . . . . . . . . 6-61
Convert to Specific Radix . . . . . . . . . . . . . . . . . . . . . . . 6-61
Convert from Character Arrays to Numeric Values . . . . 6-62
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-62
Convert from Character Code . . . . . . . . . . . . . . . . . . . . 6-63
Convert Text that Represents Numeric Values . . . . . . . . 6-63
Convert from Specific Radix . . . . . . . . . . . . . . . . . . . . . 6-64
Update Your Code to Accept Strings . . . . . . . . . . . . . . . . 6-65
What Are String Arrays? . . . . . . . . . . . . . . . . . . . . . . . . 6-65
Recommended Approaches for String Adoption in Old
APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-65
How to Adopt String Arrays in Old APIs . . . . . . . . . . . . 6-67
Recommended Approaches for String Adoption in New
Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-68
How to Maintain Compatibility in New Code . . . . . . . . . 6-70
How to Manually Convert Input Arguments . . . . . . . . . 6-71
How to Check Argument Data Types . . . . . . . . . . . . . . . 6-71
Terminology for Character and String Arrays . . . . . . . . 6-73
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-75
x Contents
Dates and Time
7
Represent Dates and Times in MATLAB . . . . . . . . . . . . . . 7-2
Specify Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6
Convert Date and Time to Julian Date or POSIX Time . . . 7-8
Set Date and Time Display Format . . . . . . . . . . . . . . . . . . 7-12
Formats for Individual Date and Duration Arrays . . . . . 7-12
datetime Display Format . . . . . . . . . . . . . . . . . . . . . . . . 7-12
duration Display Format . . . . . . . . . . . . . . . . . . . . . . . . 7-13
calendarDuration Display Format . . . . . . . . . . . . . . . . . 7-14
Default datetime Format . . . . . . . . . . . . . . . . . . . . . . . . 7-15
Generate Sequence of Dates and Time . . . . . . . . . . . . . . 7-17
Sequence of Datetime or Duration Values Between
Endpoints with Step Size . . . . . . . . . . . . . . . . . . . . . 7-17
Add Duration or Calendar Duration to Create Sequence of
Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-19
Specify Length and Endpoints of Date or Duration
Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Sequence of Datetime Values Using Calendar Rules . . . 7-22
Share Code and Data Across Locales . . . . . . . . . . . . . . . . 7-25
Write Locale-Independent Date and Time Code . . . . . . . 7-25
Write Dates in Other Languages . . . . . . . . . . . . . . . . . . 7-26
Read Dates in Other Languages . . . . . . . . . . . . . . . . . . 7-27
Extract or Assign Date and Time Components of Datetime
Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-28
Combine Date and Time from Separate Variables . . . . . 7-32
Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . 7-34
Compare Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . 7-41
Plot Dates and Durations . . . . . . . . . . . . . . . . . . . . . . . . . 7-45
Line Plot with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-45
Line Plot with Durations . . . . . . . . . . . . . . . . . . . . . . . . 7-47
xi
Scatter Plot with Dates and Durations . . . . . . . . . . . . . 7-49
Plots that Support Dates and Durations . . . . . . . . . . . . 7-50
Core Functions Supporting Date and Time Arrays . . . . . 7-52
Convert Between Datetime Arrays, Numbers, and
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-53
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-53
Convert Between Datetime and Character Vectors . . . . 7-54
Convert Between Datetime and String Arrays . . . . . . . . 7-55
Convert Between Datetime and Date Vectors . . . . . . . . 7-56
Convert Serial Date Numbers to Datetime . . . . . . . . . . 7-57
Convert Datetime Arrays to Numeric Values . . . . . . . . . 7-57
Carryover in Date Vectors and Strings . . . . . . . . . . . . . . . 7-59
Converting Date Vector Returns Unexpected Output . . . 7-60
Categorical Arrays
8
Create Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Convert Text in Table Variables to Categorical . . . . . . . . . 8-7
Plot Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
Compare Categorical Array Elements . . . . . . . . . . . . . . . 8-20
Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . 8-23
Combine Categorical Arrays Using Multiplication . . . . . 8-27
Access Data Using Categorical Arrays . . . . . . . . . . . . . . . 8-30
Select Data By Category . . . . . . . . . . . . . . . . . . . . . . . . 8-30
Common Ways to Access Data Using Categorical
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-30
Work with Protected Categorical Arrays . . . . . . . . . . . . . 8-38
xii Contents
Advantages of Using Categorical Arrays . . . . . . . . . . . . . 8-43
Natural Representation of Categorical Data . . . . . . . . . 8-43
Mathematical Ordering for Character Vectors . . . . . . . . 8-43
Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . 8-43
Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . 8-46
Order of Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-46
How to Create Ordinal Categorical Arrays . . . . . . . . . . 8-46
Working with Ordinal Categorical Arrays . . . . . . . . . . . 8-48
Core Functions Supporting Categorical Arrays . . . . . . . 8-50
Tables
9
Create and Work with Tables . . . . . . . . . . . . . . . . . . . . . . . 9-2
Add and Delete Table Rows . . . . . . . . . . . . . . . . . . . . . . . . 9-14
Add, Delete, and Rearrange Table Variables . . . . . . . . . . 9-18
Clean Messy and Missing Data in Tables . . . . . . . . . . . . . 9-27
Modify Units, Descriptions, and Table Variable Names . 9-33
Access Data in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-36
Ways to Index into a Table . . . . . . . . . . . . . . . . . . . . . . 9-36
Create Table from Subset of Larger Table . . . . . . . . . . . 9-38
Create Array from the Contents of Table . . . . . . . . . . . . 9-41
Calculations on Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-45
Split Data into Groups and Calculate Statistics . . . . . . . 9-49
Split Table Data Variables and Apply Functions . . . . . . . 9-53
Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . . . 9-58
Grouping Variables To Split Data . . . . . . . . . . . . . . . . . . . 9-65
Grouping Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-65
xiii
Group Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-66
The Split-Apply-Combine Workflow . . . . . . . . . . . . . . . . 9-66
Missing Group Values . . . . . . . . . . . . . . . . . . . . . . . . . . 9-67
Changes to DimensionNames Property in R2016b . . . . . 9-69
Timetables
10
Create Timetables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Resample and Aggregate Data in Timetable . . . . . . . . . . 10-7
Combine Timetables and Synchronize Their Data . . . . 10-11
Retime and Synchronize Timetable Variables Using
Different Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18
Select Timetable Data by Row Time and Variable
Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24
Clean Timetable with Missing, Duplicate, or Nonuniform
Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-31
Using Row Labels in Table and Timetable
Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-42
Structures
11
Create Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Access Data in a Structure Array . . . . . . . . . . . . . . . . . . . 11-7
Concatenate Structures . . . . . . . . . . . . . . . . . . . . . . . . . 11-11
Generate Field Names from Variables . . . . . . . . . . . . . . 11-13
xiv Contents
Access Data in Nested Structures . . . . . . . . . . . . . . . . . 11-14
Access Elements of a Nonscalar Struct Array . . . . . . . . 11-16
Ways to Organize Data in Structure Arrays . . . . . . . . . . 11-18
Plane Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
Element-by-Element Organization . . . . . . . . . . . . . . . 11-20
Memory Requirements for a Structure Array . . . . . . . . 11-22
Cell Arrays
12
What Is a Cell Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Create Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Access Data in Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . 12-5
Add Cells to Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8
Delete Data from Cell Array . . . . . . . . . . . . . . . . . . . . . . 12-10
Combine Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
Pass Contents of Cell Arrays to Functions . . . . . . . . . . . 12-13
Preallocate Memory for Cell Array . . . . . . . . . . . . . . . . . 12-18
Cell vs. Struct Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-19
Multilevel Indexing to Access Parts of Cells . . . . . . . . . 12-25
xv
Function Handles
13
Create Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Is a Function Handle? . . . . . . . . . . . . . . . . . . . . . 13-2
Creating Function Handles . . . . . . . . . . . . . . . . . . . . . . 13-2
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Arrays of Function Handles . . . . . . . . . . . . . . . . . . . . . 13-4
Saving and Loading Function Handles . . . . . . . . . . . . . 13-5
Pass Function to Another Function . . . . . . . . . . . . . . . . . 13-6
Call Local Functions Using Function Handles . . . . . . . . 13-8
Compare Function Handles . . . . . . . . . . . . . . . . . . . . . . 13-10
Map Containers
14
Overview of Map Data Structure . . . . . . . . . . . . . . . . . . . 14-2
Description of Map Class . . . . . . . . . . . . . . . . . . . . . . . . . 14-4
Properties of Map Class . . . . . . . . . . . . . . . . . . . . . . . . 14-4
Methods of Map Class . . . . . . . . . . . . . . . . . . . . . . . . . 14-5
Create Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6
Construct Empty Map Object . . . . . . . . . . . . . . . . . . . . 14-6
Construct Initialized Map Object . . . . . . . . . . . . . . . . . 14-6
Combine Map Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 14-8
Examine Contents of Map . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Read and Write Using Key Index . . . . . . . . . . . . . . . . . . 14-11
Read From Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-11
Add Key/Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . 14-12
Build Map with Concatenation . . . . . . . . . . . . . . . . . . 14-13
Modify Keys and Values in Map . . . . . . . . . . . . . . . . . . . 14-16
Remove Keys and Values from Map . . . . . . . . . . . . . . . 14-16
xvi Contents
Modify Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16
Modify Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-17
Modify Copy of Map . . . . . . . . . . . . . . . . . . . . . . . . . . 14-17
Map to Different Value Types . . . . . . . . . . . . . . . . . . . . . 14-19
Map to Structure Array . . . . . . . . . . . . . . . . . . . . . . . 14-19
Map to Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-20
Combining Unlike Classes
15
Valid Combinations of Unlike Classes . . . . . . . . . . . . . . . 15-2
Combining Unlike Integer Types . . . . . . . . . . . . . . . . . . . 15-3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-3
Example of Combining Unlike Integer Sizes . . . . . . . . . 15-3
Example of Combining Signed with Unsigned . . . . . . . . 15-4
Combining Integer and Noninteger Data . . . . . . . . . . . . 15-5
Combining Cell Arrays with Non-Cell Arrays . . . . . . . . . 15-6
Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-7
Concatenation Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 15-8
Combining Single and Double Types . . . . . . . . . . . . . . . 15-8
Combining Integer and Double Types . . . . . . . . . . . . . . 15-8
Combining Character and Double Types . . . . . . . . . . . . 15-9
Combining Logical and Double Types . . . . . . . . . . . . . . 15-9
Using Objects
16
Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Handle Object Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . 16-2
xvii
Handle Object Copy Behavior . . . . . . . . . . . . . . . . . . . . 16-3
Testing for Handle or Value Class . . . . . . . . . . . . . . . . . 16-6
Defining Your Own Classes
17
Scripts and Functions
Scripts
18
Create Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
Add Comments to Programs . . . . . . . . . . . . . . . . . . . . . . . 18-4
Run Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-7
Divide Your File into Code Sections . . . . . . . . . . . . . . . . 18-7
Evaluate Code Sections . . . . . . . . . . . . . . . . . . . . . . . . 18-7
Navigate Among Code Sections in a File . . . . . . . . . . . . 18-9
Example of Evaluating Code Sections . . . . . . . . . . . . . . 18-9
Change the Appearance of Code Sections . . . . . . . . . . 18-12
Use Code Sections with Control Statements and
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-13
Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-16
Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Add Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Access Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-19
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-19
Add and Run Sections in Live Scripts . . . . . . . . . . . . . 18-20
xviii Contents
Live Scripts and Functions
19
What Is a Live Script or Function? . . . . . . . . . . . . . . . . . . 19-2
Differences with Plain Code Scripts and Functions . . . . 19-4
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-5
Unsupported Features . . . . . . . . . . . . . . . . . . . . . . . . . 19-6
Create Live Scripts in the Live Editor . . . . . . . . . . . . . . . 19-7
Create Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-7
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-8
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-9
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-10
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Run Sections in Live Scripts . . . . . . . . . . . . . . . . . . . . . . 19-15
Divide Your File Into Sections . . . . . . . . . . . . . . . . . . . 19-15
Evaluate Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-15
Debug Code in the Live Editor . . . . . . . . . . . . . . . . . . . . 19-17
Show Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-17
Debug Using Run to Here . . . . . . . . . . . . . . . . . . . . . . 19-18
View Variable Value While Debugging . . . . . . . . . . . . . 19-20
Pause a Running File . . . . . . . . . . . . . . . . . . . . . . . . . 19-21
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . 19-21
Step Into Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 19-22
Add Breakpoints and Run . . . . . . . . . . . . . . . . . . . . . . 19-23
Modify Figures in Live Scripts . . . . . . . . . . . . . . . . . . . . 19-26
Explore Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-26
Update Code with Figure Changes . . . . . . . . . . . . . . . 19-28
Add Formatting and Annotations . . . . . . . . . . . . . . . . 19-29
Add and Modify Multiple Subplots . . . . . . . . . . . . . . . 19-32
Save and Print Figure . . . . . . . . . . . . . . . . . . . . . . . . . 19-38
Format Files in the Live Editor . . . . . . . . . . . . . . . . . . . . 19-40
Autoformatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-42
Insert Equations into the Live Editor . . . . . . . . . . . . . . 19-46
Insert Equation Interactively . . . . . . . . . . . . . . . . . . . 19-46
Insert LaTeX Equation . . . . . . . . . . . . . . . . . . . . . . . . 19-49
xix
Add Interactive Controls to a Live Script . . . . . . . . . . . 19-58
Create Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-62
Add Help for Live Functions . . . . . . . . . . . . . . . . . . . . . . 19-65
Share Live Scripts and Functions . . . . . . . . . . . . . . . . . . 19-69
Live Code File Format (.mlx) . . . . . . . . . . . . . . . . . . . . . 19-71
Benefits of Live Code File Format . . . . . . . . . . . . . . . . 19-71
Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-71
Function Basics
20
Create Functions in Files . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Syntax for Function Definition . . . . . . . . . . . . . . . . . . . 20-3
Contents of Functions and Files . . . . . . . . . . . . . . . . . . 20-4
End Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4
Add Help for Your Program . . . . . . . . . . . . . . . . . . . . . . . 20-6
Run Functions in the Editor . . . . . . . . . . . . . . . . . . . . . . . 20-8
Base and Function Workspaces . . . . . . . . . . . . . . . . . . . 20-10
Share Data Between Workspaces . . . . . . . . . . . . . . . . . . 20-11
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-11
Best Practice: Passing Arguments . . . . . . . . . . . . . . . . 20-11
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-13
Evaluating in Another Workspace . . . . . . . . . . . . . . . . 20-14
Check Variable Scope in Editor . . . . . . . . . . . . . . . . . . . 20-16
Use Automatic Function and Variable Highlighting . . . 20-16
Example of Using Automatic Function and Variable
Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-17
xx Contents
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-20
Local and Nested Functions in a File . . . . . . . . . . . . . 20-20
Private Functions in a Subfolder . . . . . . . . . . . . . . . . . 20-21
Anonymous Functions Without a File . . . . . . . . . . . . . 20-21
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-24
What Are Anonymous Functions? . . . . . . . . . . . . . . . . 20-24
Variables in the Expression . . . . . . . . . . . . . . . . . . . . . 20-25
Multiple Anonymous Functions . . . . . . . . . . . . . . . . . . 20-26
Functions with No Inputs . . . . . . . . . . . . . . . . . . . . . . 20-27
Functions with Multiple Inputs or Outputs . . . . . . . . . 20-27
Arrays of Anonymous Functions . . . . . . . . . . . . . . . . . 20-28
Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-30
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-32
What Are Nested Functions? . . . . . . . . . . . . . . . . . . . 20-32
Requirements for Nested Functions . . . . . . . . . . . . . . 20-32
Sharing Variables Between Parent and Nested
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-33
Using Handles to Store Function Parameters . . . . . . . 20-34
Visibility of Nested Functions . . . . . . . . . . . . . . . . . . . 20-37
Variables in Nested and Anonymous Functions . . . . . . 20-39
Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-41
Function Precedence Order . . . . . . . . . . . . . . . . . . . . . . 20-43
Function Arguments
21
Find Number of Function Arguments . . . . . . . . . . . . . . . 21-2
Support Variable Number of Inputs . . . . . . . . . . . . . . . . . 21-4
Support Variable Number of Outputs . . . . . . . . . . . . . . . 21-6
Validate Number of Function Arguments . . . . . . . . . . . . 21-8
xxi
Argument Checking in Nested Functions . . . . . . . . . . . 21-11
Ignore Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . 21-13
Check Function Inputs with validateattributes . . . . . . 21-14
Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-17
Input Parser Validation Functions . . . . . . . . . . . . . . . . . 21-21
Debugging MATLAB Code
22
Debug a MATLAB Program . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Set Breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Run File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-3
Pause a Running File . . . . . . . . . . . . . . . . . . . . . . . . . . 22-4
Find and Fix a Problem . . . . . . . . . . . . . . . . . . . . . . . . . 22-4
Step Through File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-6
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . 22-7
Set Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-8
Standard Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . 22-9
Conditional Breakpoints . . . . . . . . . . . . . . . . . . . . . . . 22-10
Error Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-11
Breakpoints in Anonymous Functions . . . . . . . . . . . . . 22-11
Invalid Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
Disable Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13
Clear Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13
Examine Values While Debugging . . . . . . . . . . . . . . . . . 22-15
Select Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-15
View Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . 22-15
xxii Contents
Presenting MATLAB Code
23
Options for Presenting Your Code . . . . . . . . . . . . . . . . . . 23-2
Publishing MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . 23-4
Publishing Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-7
Markup Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-7
Sections and Section Titles . . . . . . . . . . . . . . . . . . . . . 23-10
Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-11
Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . 23-12
Text and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . 23-13
External File Content . . . . . . . . . . . . . . . . . . . . . . . . . 23-14
External Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-15
Image Snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-17
LaTeX Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-18
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-20
HTML Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-23
LaTeX Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-24
Output Preferences for Publishing . . . . . . . . . . . . . . . . 23-27
How to Edit Publishing Options . . . . . . . . . . . . . . . . . 23-27
Specify Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-28
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . 23-29
Manipulate Graphics in Publishing Output . . . . . . . . . 23-31
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . 23-36
Manage a Publish Configuration . . . . . . . . . . . . . . . . . 23-38
Coding and Productivity Tips
24
Open and Save Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Open Existing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Save Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-3
Check Code for Errors and Warnings . . . . . . . . . . . . . . . . 24-6
Automatically Check Code in the Editor — Code
Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-6
xxiii
Create a Code Analyzer Message Report . . . . . . . . . . 24-10
Adjust Code Analyzer Message Indicators and
Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-11
Understand Code Containing Suppressed Messages . . 24-15
Understand the Limitations of Code Analysis . . . . . . . 24-16
Enable MATLAB Compiler Deployment Messages . . . . 24-19
Improve Code Readability . . . . . . . . . . . . . . . . . . . . . . . . 24-21
Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-21
Right-Side Text Limit Indicator . . . . . . . . . . . . . . . . . . 24-23
Code Folding — Expand and Collapse Code
Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-23
Find and Replace Text in Files . . . . . . . . . . . . . . . . . . . . 24-28
Find Any Text in the Current File . . . . . . . . . . . . . . . . 24-28
Find and Replace Functions or Variables in the Current
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-28
Automatically Rename All Functions or Variables in a
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-30
Find and Replace Any Text . . . . . . . . . . . . . . . . . . . . . 24-32
Find Text in Multiple File Names or Files . . . . . . . . . . 24-32
Function Alternative for Finding Text . . . . . . . . . . . . . 24-32
Perform an Incremental Search in the Editor . . . . . . . 24-32
Go To Location in File . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-33
Navigate to a Specific Location . . . . . . . . . . . . . . . . . . 24-33
Set Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-35
Navigate Backward and Forward in Files . . . . . . . . . . 24-36
Open a File or Variable from Within a File . . . . . . . . . 24-37
Display Two Parts of a File Simultaneously . . . . . . . . . . 24-38
Add Reminders to Files . . . . . . . . . . . . . . . . . . . . . . . . . . 24-41
Working with TODO/FIXME Reports . . . . . . . . . . . . . . 24-41
MATLAB Code Analyzer Report . . . . . . . . . . . . . . . . . . . 24-44
Running the Code Analyzer Report . . . . . . . . . . . . . . . 24-44
Changing Code Based on Code Analyzer Messages . . . 24-46
Other Ways to Access Code Analyzer Messages . . . . . 24-46
MATLAB Code Compatibility Report . . . . . . . . . . . . . . . 24-48
Generate the Code Compatibility Report . . . . . . . . . . . 24-48
Programmatic Use . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-50
xxiv Contents
Programming Utilities
25
Identify Program Dependencies . . . . . . . . . . . . . . . . . . . . 25-2
Simple Display of Program File Dependencies . . . . . . . 25-2
Detailed Display of Program File Dependencies . . . . . . 25-2
Dependencies Within a Folder . . . . . . . . . . . . . . . . . . . 25-3
Protect Your Source Code . . . . . . . . . . . . . . . . . . . . . . . . . 25-7
Building a Content Obscured Format with P-Code . . . . . 25-7
Building a Standalone Executable . . . . . . . . . . . . . . . . . 25-8
Create Hyperlinks that Run Functions . . . . . . . . . . . . . 25-10
Run a Single Function . . . . . . . . . . . . . . . . . . . . . . . . 25-11
Run Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . 25-11
Provide Command Options . . . . . . . . . . . . . . . . . . . . . 25-12
Include Special Characters . . . . . . . . . . . . . . . . . . . . . 25-12
Create and Share Toolboxes . . . . . . . . . . . . . . . . . . . . . . 25-13
Create Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-13
Share Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-19
Manage Java Class Path for Toolboxes . . . . . . . . . . . . . . 25-21
Software Development
Error Handling
26
Exception Handling in a MATLAB Application . . . . . . . . 26-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Getting an Exception at the Command Line . . . . . . . . . 26-2
Getting an Exception in Your Program Code . . . . . . . . . 26-3
Generating a New Exception . . . . . . . . . . . . . . . . . . . . 26-4
Capture Information About Exceptions . . . . . . . . . . . . . . 26-5
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-5
xxv
The MException Class . . . . . . . . . . . . . . . . . . . . . . . . . 26-5
Properties of the MException Class . . . . . . . . . . . . . . . 26-7
Methods of the MException Class . . . . . . . . . . . . . . . . 26-13
Throw an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-15
Respond to an Exception . . . . . . . . . . . . . . . . . . . . . . . . 26-17
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-17
The try/catch Statement . . . . . . . . . . . . . . . . . . . . . . . 26-17
Suggestions on How to Handle an Exception . . . . . . . 26-19
Clean Up When Functions Complete . . . . . . . . . . . . . . . 26-22
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22
Examples of Cleaning Up a Program Upon Exit . . . . . . 26-23
Retrieving Information About the Cleanup Routine . . . 26-25
Using onCleanup Versus try/catch . . . . . . . . . . . . . . . 26-26
onCleanup in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 26-27
Issue Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . 26-28
Issue Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Throw Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Add Run-Time Parameters to Your Warnings and
Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-29
Add Identifiers to Warnings and Errors . . . . . . . . . . . . 26-30
Suppress Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-32
Turn Warnings On and Off . . . . . . . . . . . . . . . . . . . . . 26-33
Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-35
Disable and Restore a Particular Warning . . .