MATLAB®
Programming Fundamentals
R2020a
How to Contact MathWorks
Latest news: www.mathworks.com
Sales and services: www.mathworks.com/sales_and_services
User community: www.mathworks.com/matlabcentral
Technical support: www.mathworks.com/support/contact_us
Phone: 508-647-7000
The MathWorks, Inc.
1 Apple Hill Drive
Natick, MA 01760-2098
MATLAB Programming Fundamentals
© COPYRIGHT 1984–2020 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 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)
September 2018 Online only Revised for Version 9.5 (Release 2018b)
March 2019 Online only Revised for MATLAB 9.6 (Release 2019a)
September 2019 Online only Revised for MATLAB 9.7 (Release 2019b)
March 2020 Online only Revised for MATLAB 9.8 (Release 2020a)
Contents
Language
Syntax Basics
1
Continue Long Statements on Multiple Lines . . . . . . . . . . . . . . . . . . . 1-2
Ignore Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Valid Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Conflicts with Function Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Case and Space Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Command vs. Function Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Command and Function Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Avoid Common Syntax Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
How MATLAB Recognizes Command Syntax . . . . . . . . . . . . . . . . . . . 1-7
Common Errors When Calling Functions . . . . . . . . . . . . . . . . . . . . . . 1-9
Conflicting Function and Variable Names . . . . . . . . . . . . . . . . . . . . . 1-9
Undefined Functions or Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Program Components
2
MATLAB Operators and Special Characters . . . . . . . . . . . . . . . . . . . . 2-2
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
String and Character Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Array vs. Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
Compatible Array Sizes for Basic Operations . . . . . . . . . . . . . . . . . . 2-25
Inputs with Compatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25
v
Inputs with Incompatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Array Comparison with Relational Operators . . . . . . . . . . . . . . . . . . 2-29
Array Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29
Logic Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32
Precedence of AND and OR Operators . . . . . . . . . . . . . . . . . . . . . . 2-32
Overriding Default Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32
Average Similar Data Points Using a Tolerance . . . . . . . . . . . . . . . . 2-34
Group Scattered Data Using a Tolerance . . . . . . . . . . . . . . . . . . . . . . 2-36
Bit-Wise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-38
Perform Cyclic Redundancy Check . . . . . . . . . . . . . . . . . . . . . . . . . . 2-44
Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-47
Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-49
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-51
What Is a Regular Expression? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-51
Steps for Building Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-52
Operators and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-54
Lookahead Assertions in Regular Expressions . . . . . . . . . . . . . . . . . 2-63
Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-63
Overlapping Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-63
Logical AND Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-64
Tokens in Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66
Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-68
Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-69
Tokens in Replacement Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-69
Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-70
Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-72
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-72
Dynamic Match Expressions — (??expr) . . . . . . . . . . . . . . . . . . . . . 2-73
Commands That Modify the Match Expression — (??@cmd) . . . . . . 2-73
Commands That Serve a Functional Purpose — (?@cmd) . . . . . . . . 2-74
Commands in Replacement Expressions — ${cmd} . . . . . . . . . . . . 2-76
Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-79
What Is a Comma-Separated List? . . . . . . . . . . . . . . . . . . . . . . . . . 2-79
Generating a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . . 2-79
Assigning Output from a Comma-Separated List . . . . . . . . . . . . . . . 2-81
Assigning to a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . 2-81
How to Use the Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . 2-82
Fast Fourier Transform Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-84
vi Contents
Alternatives to the eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-86
Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-86
Variables with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . 2-86
Files with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-87
Function Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-87
Field Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-88
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-88
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-2
Arithmetic Operations on Integer Classes . . . . . . . . . . . . . . . . . . . . . 4-4
Largest and Smallest Values for Integer Classes . . . . . . . . . . . . . . . . 4-4
Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Double-Precision Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Single-Precision Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Creating Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Arithmetic Operations on Floating-Point Numbers . . . . . . . . . . . . . . 4-8
Largest and Smallest Values for Floating-Point Classes . . . . . . . . . . . 4-9
Accuracy of Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Avoiding Common Problems with Floating-Point Arithmetic . . . . . . 4-11
Create Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13
Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
Display Format for Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19
Single Precision Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
vii
The Logical Class
5
Find Array Elements That Meet a Condition . . . . . . . . . . . . . . . . . . . . 5-2
Reduce Logical Arrays to Single Value . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Characters and Strings
6
Text in String and Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Create String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Cell Arrays of Character Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12
Create Cell Array of Character Vectors . . . . . . . . . . . . . . . . . . . . . . 6-12
Access Character Vectors in Cell Array . . . . . . . . . . . . . . . . . . . . . . 6-12
Convert Cell Arrays to String Arrays . . . . . . . . . . . . . . . . . . . . . . . . 6-13
Analyze Text Data with String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 6-15
Test for Empty Strings and Missing Values . . . . . . . . . . . . . . . . . . . . 6-20
Formatting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Fields of the Formatting Operator . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Setting Field Width and Precision . . . . . . . . . . . . . . . . . . . . . . . . . . 6-28
Restrictions on Using Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30
Compare Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-32
Search and Replace Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-37
Convert Numeric Values to Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-42
Convert Text to Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-46
Unicode and ASCII Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-50
Hexadecimal and Binary Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-52
Frequently Asked Questions About String Arrays . . . . . . . . . . . . . . . 6-56
Why Does Using Command Form With Strings Return An Error? . . 6-56
Why Do Strings in Cell Arrays Return an Error? . . . . . . . . . . . . . . . 6-57
Why Does length() of String Return 1? . . . . . . . . . . . . . . . . . . . . . . 6-57
Why Does isempty("") Return 0? . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-58
Why Does Appending Strings Using Square Brackets Return Multiple
Strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-59
Update Your Code to Accept Strings . . . . . . . . . . . . . . . . . . . . . . . . . 6-61
What Are String Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-61
Recommended Approaches for String Adoption in Old APIs . . . . . . 6-61
viii Contents
How to Adopt String Arrays in Old APIs . . . . . . . . . . . . . . . . . . . . . 6-63
Recommended Approaches for String Adoption in New Code . . . . . 6-63
How to Maintain Compatibility in New Code . . . . . . . . . . . . . . . . . . 6-64
How to Manually Convert Input Arguments . . . . . . . . . . . . . . . . . . 6-65
How to Check Argument Data Types . . . . . . . . . . . . . . . . . . . . . . . . 6-65
Terminology for Character and String Arrays . . . . . . . . . . . . . . . . . 6-67
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-69
Dates and Time
7
Represent Dates and Times in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 7-2
Specify Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
Convert Date and Time to Julian Date or POSIX Time . . . . . . . . . . . . 7-7
Set Date and Time Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10
Formats for Individual Date and Duration Arrays . . . . . . . . . . . . . . 7-10
datetime Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10
duration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
calendarDuration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
Default datetime Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12
Generate Sequence of Dates and Time . . . . . . . . . . . . . . . . . . . . . . . 7-14
Sequence of Datetime or Duration Values Between Endpoints with
Step Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14
Add Duration or Calendar Duration to Create Sequence of Dates . . 7-16
Specify Length and Endpoints of Date or Duration Sequence . . . . . 7-17
Sequence of Datetime Values Using Calendar Rules . . . . . . . . . . . . 7-17
Share Code and Data Across Locales . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
Write Locale-Independent Date and Time Code . . . . . . . . . . . . . . . . 7-20
Write Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Read Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Extract or Assign Date and Time Components of Datetime Array . . 7-23
Combine Date and Time from Separate Variables . . . . . . . . . . . . . . 7-26
Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-28
Compare Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33
Plot Dates and Durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-36
Line Plot with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-36
Line Plot with Durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-37
Scatter Plot with Dates and Durations . . . . . . . . . . . . . . . . . . . . . . 7-39
Plots that Support Dates and Durations . . . . . . . . . . . . . . . . . . . . . 7-40
Core Functions Supporting Date and Time Arrays . . . . . . . . . . . . . . 7-41
ix
Convert Between Datetime Arrays, Numbers, and Text . . . . . . . . . . 7-42
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-42
Convert Between Datetime and Character Vectors . . . . . . . . . . . . . 7-42
Convert Between Datetime and String Arrays . . . . . . . . . . . . . . . . . 7-44
Convert Between Datetime and Date Vectors . . . . . . . . . . . . . . . . . 7-44
Convert Serial Date Numbers to Datetime . . . . . . . . . . . . . . . . . . . 7-45
Convert Datetime Arrays to Numeric Values . . . . . . . . . . . . . . . . . . 7-45
Carryover in Date Vectors and Strings . . . . . . . . . . . . . . . . . . . . . . . . 7-47
Converting Date Vector Returns Unexpected Output . . . . . . . . . . . . 7-48
Categorical Arrays
8
Create Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Convert Text in Table Variables to Categorical . . . . . . . . . . . . . . . . . . 8-6
Plot Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10
Compare Categorical Array Elements . . . . . . . . . . . . . . . . . . . . . . . . 8-16
Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-19
Combine Categorical Arrays Using Multiplication . . . . . . . . . . . . . . 8-22
Access Data Using Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . 8-24
Select Data By Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24
Common Ways to Access Data Using Categorical Arrays . . . . . . . . . 8-24
Work with Protected Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . 8-30
Advantages of Using Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . 8-34
Natural Representation of Categorical Data . . . . . . . . . . . . . . . . . . 8-34
Mathematical Ordering for Character Vectors . . . . . . . . . . . . . . . . . 8-34
Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-34
Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-36
Order of Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-36
How to Create Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . 8-36
Working with Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . 8-38
Core Functions Supporting Categorical Arrays . . . . . . . . . . . . . . . . 8-39
x Contents
Tables
9
Create and Work with Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Add and Delete Table Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11
Add, Delete, and Rearrange Table Variables . . . . . . . . . . . . . . . . . . . 9-14
Clean Messy and Missing Data in Tables . . . . . . . . . . . . . . . . . . . . . . 9-21
Modify Units, Descriptions, and Table Variable Names . . . . . . . . . . 9-26
Add Custom Properties to Tables and Timetables . . . . . . . . . . . . . . 9-29
Access Data in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-34
Summary of Table Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . 9-34
Tables Containing Specified Rows and Variables . . . . . . . . . . . . . . . 9-37
Extract Data Using Dot Notation and Logical Values . . . . . . . . . . . . 9-40
Dot Notation with Any Variable Name or Expression . . . . . . . . . . . . 9-42
Extract Data from Specified Rows and Variables . . . . . . . . . . . . . . . 9-44
Calculations on Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-47
Split Data into Groups and Calculate Statistics . . . . . . . . . . . . . . . . 9-50
Split Table Data Variables and Apply Functions . . . . . . . . . . . . . . . . 9-53
Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-57
Grouping Variables To Split Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-62
Grouping Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-62
Group Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-62
The Split-Apply-Combine Workflow . . . . . . . . . . . . . . . . . . . . . . . . . 9-63
Missing Group Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-63
Changes to DimensionNames Property in R2016b . . . . . . . . . . . . . . 9-65
Timetables
10
Create Timetables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Resample and Aggregate Data in Timetable . . . . . . . . . . . . . . . . . . . 10-5
Combine Timetables and Synchronize Their Data . . . . . . . . . . . . . . 10-8
Retime and Synchronize Timetable Variables Using Different
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
xi
Select Timetable Data by Row Time and Variable Type . . . . . . . . . 10-19
Clean Timetable with Missing, Duplicate, or Nonuniform Times
.................................................... 10-25
Using Row Labels in Table and Timetable Operations . . . . . . . . . . 10-33
Loma Prieta Earthquake Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 10-38
Preprocess and Explore Time-stamped Data Using timetable . . . . 10-48
Structures
11
Create Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Access Data in Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
Access Data in Scalar Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
Access Data by Indexing into Structure Array . . . . . . . . . . . . . . . . . 11-6
Concatenate Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-9
Generate Field Names from Variables . . . . . . . . . . . . . . . . . . . . . . . 11-11
Access Data in Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12
Access Elements of a Nonscalar Struct Array . . . . . . . . . . . . . . . . . 11-14
Ways to Organize Data in Structure Arrays . . . . . . . . . . . . . . . . . . . 11-16
Plane Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-16
Element-by-Element Organization . . . . . . . . . . . . . . . . . . . . . . . . 11-17
Memory Requirements for Structure Array . . . . . . . . . . . . . . . . . . 11-19
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-9
Combine Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-10
xii Contents
Pass Contents of Cell Arrays to Functions . . . . . . . . . . . . . . . . . . . . 12-11
Preallocate Memory for Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . 12-15
Cell vs. Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-16
Multilevel Indexing to Access Parts of Cells . . . . . . . . . . . . . . . . . . 12-20
Function Handles
13
Create Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Is a Function Handle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Creating Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-3
Arrays of Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Saving and Loading Function Handles . . . . . . . . . . . . . . . . . . . . . . 13-4
Pass Function to Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5
Call Local Functions Using Function Handles . . . . . . . . . . . . . . . . . 13-6
Compare Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8
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-4
Create Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6
Construct Empty Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6
Construct Initialized Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6
Combine Map Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-7
Examine Contents of Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-8
Read and Write Using Key Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Read From Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Add Key/Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-10
Build Map with Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-10
Modify Keys and Values in Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-13
Remove Keys and Values from Map . . . . . . . . . . . . . . . . . . . . . . . . 14-13
Modify Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-13
Modify Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-14
xiii
Modify Copy of Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-14
Map to Different Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-15
Map to Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-15
Map to Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16
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-3
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-8
Combining Logical and Double Types . . . . . . . . . . . . . . . . . . . . . . . 15-8
Using Objects
16
Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Handle Object Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Handle Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-3
Testing for Handle or Value Class . . . . . . . . . . . . . . . . . . . . . . . . . . 16-5
xiv Contents
Defining Your Own Classes
17
Scripts and Functions
Scripts
18
Create Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
Add Comments to Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-3
Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-5
Divide Your File into Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . 18-5
Evaluate Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-5
Navigate Among Code Sections in a File . . . . . . . . . . . . . . . . . . . . . 18-6
Example of Evaluating Code Sections . . . . . . . . . . . . . . . . . . . . . . . 18-6
Change the Appearance of Code Sections . . . . . . . . . . . . . . . . . . . . 18-9
Use Code Sections with Control Statements and Functions . . . . . . . 18-9
Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-12
Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-14
Create a Script with Local Functions . . . . . . . . . . . . . . . . . . . . . . 18-14
Run Scripts with Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . 18-14
Restrictions for Local Functions and Variables . . . . . . . . . . . . . . . 18-15
Access Help for Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-15
Live Scripts and Functions
19
What Is a Live Script or Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2
Differences with Plain Code Scripts and Functions . . . . . . . . . . . . . 19-3
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-4
Unsupported Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-5
Create Live Scripts in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . 19-6
Create Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-6
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-6
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-8
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-8
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-9
Save Live Scripts as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . . . 19-10
xv
Run Sections in Live Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Divide Your File Into Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Evaluate Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Debug Code in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-14
Show Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-14
Debug Using Run to Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-15
View Variable Value While Debugging . . . . . . . . . . . . . . . . . . . . . . 19-17
Pause a Running File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-17
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-17
Step Into Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-18
Add Breakpoints and Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-18
Modify Figures in Live Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-22
Explore Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-22
Update Code with Figure Changes . . . . . . . . . . . . . . . . . . . . . . . . 19-24
Add Formatting and Annotations . . . . . . . . . . . . . . . . . . . . . . . . . 19-24
Add and Modify Multiple Subplots . . . . . . . . . . . . . . . . . . . . . . . . 19-26
Save and Print Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-30
Format Files in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-31
Autoformatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-33
Insert Equations into the Live Editor . . . . . . . . . . . . . . . . . . . . . . . 19-35
Insert Equation Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-35
Insert LaTeX Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-37
Add Interactive Controls to a Live Script . . . . . . . . . . . . . . . . . . . . 19-44
Insert Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-44
Modify Control Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-45
Modify Control Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-46
Create Live Script with Multiple Interactive Controls . . . . . . . . . . 19-46
Share Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-48
Add Interactive Tasks to a Live Script . . . . . . . . . . . . . . . . . . . . . . . 19-49
What Are Live Editor Tasks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-49
Insert Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-49
Run Tasks and Surrounding Code . . . . . . . . . . . . . . . . . . . . . . . . . 19-52
Modify Output Argument Name . . . . . . . . . . . . . . . . . . . . . . . . . . 19-53
View and Edit Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-53
Create Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-55
Create Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-55
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-55
Add Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-56
Run Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-56
Save Live Functions as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . 19-57
Add Help for Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-58
Share Live Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-62
Hide Code Before Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-63
Live Code File Format (.mlx) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-64
Benefits of Live Code File Format . . . . . . . . . . . . . . . . . . . . . . . . . 19-64
xvi Contents
Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-64
Introduction to the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-65
Accelerate Exploratory Programming Using the Live Editor . . . . . 19-70
Create an Interactive Narrative with the Live Editor . . . . . . . . . . . 19-74
Create Interactive Course Materials Using the Live Editor . . . . . . 19-81
Create Examples Using the Live Editor . . . . . . . . . . . . . . . . . . . . . . 19-87
Create an Interactive Form Using the Live Editor . . . . . . . . . . . . . 19-88
Create a Real-time Dashboard Using the Live Editor . . . . . . . . . . . 19-91
Function Basics
20
Create Functions in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Syntax for Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Contents of Functions and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-3
End Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4
Add Help for Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-5
Configure the Run Button for Functions . . . . . . . . . . . . . . . . . . . . . . 20-7
Base and Function Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-9
Share Data Between Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10
Best Practice: Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 20-10
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10
Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-11
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Evaluating in Another Workspace . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Check Variable Scope in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-14
Use Automatic Function and Variable Highlighting . . . . . . . . . . . . 20-14
Example of Using Automatic Function and Variable Highlighting . 20-14
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-17
Local and Nested Functions in a File . . . . . . . . . . . . . . . . . . . . . . 20-17
Private Functions in a Subfolder . . . . . . . . . . . . . . . . . . . . . . . . . . 20-18
Anonymous Functions Without a File . . . . . . . . . . . . . . . . . . . . . . 20-18
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-20
What Are Anonymous Functions? . . . . . . . . . . . . . . . . . . . . . . . . . 20-20
Variables in the Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-21
Multiple Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-21
xvii
Functions with No Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-22
Functions with Multiple Inputs or Outputs . . . . . . . . . . . . . . . . . . 20-22
Arrays of Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 20-23
Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-25
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-27
What Are Nested Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-27
Requirements for Nested Functions . . . . . . . . . . . . . . . . . . . . . . . 20-27
Sharing Variables Between Parent and Nested Functions . . . . . . . 20-28
Using Handles to Store Function Parameters . . . . . . . . . . . . . . . . 20-29
Visibility of Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-31
Variables in Nested and Anonymous Functions . . . . . . . . . . . . . . . 20-33
Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-34
Function Precedence Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-35
Change in Rules For Function Precedence Order . . . . . . . . . . . . . 20-36
Update Code for R2019b Changes to Function Precedence Order
.................................................... 20-38
Identifiers cannot be used for two purposes inside a function . . . . 20-38
Identifiers without explicit declarations might not be treated as
variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-38
Variables cannot be implicitly shared between parent and nested
functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-39
Change in precedence of wildcard-based imports . . . . . . . . . . . . . 20-40
Fully qualified import functions cannot have the same name as nested
functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-40
Fully qualified imports shadow outer scope definitions of the same
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-41
Error handling when import not found . . . . . . . . . . . . . . . . . . . . . 20-41
Nested functions inherit import statements from parent functions
................................................ 20-42
Change in precedence of compound name resolution . . . . . . . . . . 20-42
Anonymous functions can include resolved and unresolved identifiers
................................................ 20-43
Indexing into Function Call Results . . . . . . . . . . . . . . . . . . . . . . . . 20-44
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-44
Supported Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-44
Function Arguments
21
Find Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . 21-2
Support Variable Number of Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . 21-4
Support Variable Number of Outputs . . . . . . . . . . . . . . . . . . . . . . . . 21-5
xviii Contents
Validate Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . 21-6
Argument Checking in Nested Functions . . . . . . . . . . . . . . . . . . . . . 21-8
Ignore Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-10
Check Function Inputs with validateattributes . . . . . . . . . . . . . . . 21-11
Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-13
Input Parser Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 21-16
Debugging MATLAB Code
22
Debug a MATLAB Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Set Breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Run File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-3
Pause a Running File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-3
Find and Fix a Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-3
Step Through File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-5
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-6
Set Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-7
Standard Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-7
Conditional Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-8
Error Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-9
Breakpoints in Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . 22-10
Invalid Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-10
Disable Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-10
Clear Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-11
Examine Values While Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
Select Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
View Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
Presenting MATLAB Code
23
Publish and Share MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-2
Create and Share Live Scripts in the Live Editor . . . . . . . . . . . . . . . 23-2
Publish MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-2
Add Help and Create Documentation . . . . . . . . . . . . . . . . . . . . . . . 23-4
Publishing Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-5
Markup Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-5
Sections and Section Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-7
Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-8
Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9
xix
Text and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9
External File Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-10
External Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-11
Image Snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-13
LaTeX Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-13
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-15
HTML Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-17
LaTeX Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-18
Output Preferences for Publishing . . . . . . . . . . . . . . . . . . . . . . . . . 23-20
How to Edit Publishing Options . . . . . . . . . . . . . . . . . . . . . . . . . . 23-20
Specify Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-21
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-21
Manipulate Graphics in Publishing Output . . . . . . . . . . . . . . . . . . 23-23
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-26
Manage a Publish Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 23-27
Coding and Productivity Tips
24
Open and Save Files in the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Open Existing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Save Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-3
Check Code for Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . 24-5
Automatically Check Code in the Editor and Live Editor — Code
Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-5
Create a Code Analyzer Message Report . . . . . . . . . . . . . . . . . . . . . 24-8
Adjust Code Analyzer Message Indicators and Messages . . . . . . . . 24-8
Understand Code Containing Suppressed Messages . . . . . . . . . . . 24-11
Understand the Limitations of Code Analysis . . . . . . . . . . . . . . . . 24-12
Enable MATLAB Compiler Deployment Messages . . . . . . . . . . . . . 24-14
Improve Code Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-16
Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-16
Right-Side Text Limit Indicator . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-17
Code Folding — Expand and Collapse Code Constructs . . . . . . . . . 24-18
Code Refactoring — Automatically convert selected code to a function
................................................ 24-20
Find and Replace Text in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-22
Find Any Text in the Current File . . . . . . . . . . . . . . . . . . . . . . . . . 24-22
Find and Replace Functions or Variables in the Current File . . . . . 24-22
Automatically Rename All Functions or Variables in a File . . . . . . 24-23
Find and Replace Any Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-24
Find Text in Multiple File Names or Files . . . . . . . . . . . . . . . . . . . 24-24
Function Alternative for Finding Text . . . . . . . . . . . . . . . . . . . . . . 24-25
Perform an Incremental Search in the Editor . . . . . . . . . . . . . . . . 24-25
Go To Location in File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-25
Add Reminders to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-29
Working with TODO/FIXME Reports . . . . . . . . . . . . . . . . . . . . . . . 24-29
xx Contents
MATLAB Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-31
Running the Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . 24-31
Changing Code Based on Code Analyzer Messages . . . . . . . . . . . . 24-32
Other Ways to Access Code Analyzer Messages . . . . . . . . . . . . . . 24-32
MATLAB Code Compatibility Report . . . . . . . . . . . . . . . . . . . . . . . . 24-34
Generate the Code Compatibility Report . . . . . . . . . . . . . . . . . . . . 24-34
Programmatic Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-36
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-2
Protect Your Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-6
Building a Content Obscured Format with P-Code . . . . . . . . . . . . . . 25-6
Building a Standalone Executable . . . . . . . . . . . . . . . . . . . . . . . . . . 25-7
Create Hyperlinks that Run Functions . . . . . . . . . . . . . . . . . . . . . . . 25-8
Run a Single Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-8
Run Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-9
Provide Command Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-9
Include Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-9
Create and Share Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-11
Create Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-11
Share Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-15
Function Argument Validation
26
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Introduction to Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Where to Use Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
arguments Block Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-3
Examples of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 26-5
Kinds of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-7
Required and Optional Positional Arguments . . . . . . . . . . . . . . . . . 26-7
Repeating Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-8
Name-Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-11
Name-Value Arguments from Class Properties . . . . . . . . . . . . . . . 26-13
Argument Validation in Class Methods . . . . . . . . . . . . . . . . . . . . . 26-15
Order of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-15
Avoiding Class and Size Conversions . . . . . . . . . . . . . . . . . . . . . . 26-16
nargin in Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-18
Restrictions on Variable and Function Access . . . . . . . . . . . . . . . . 26-19
xxi
Argument Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-21
Define Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22
Parsing Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-24
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-24
validateattributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-24
inputParser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-24
Transparency in MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25
Writing Transparent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25
Software Development
Error Handling
27
Exception Handling in a MATLAB Application . . . . . . . . . . . . . . . . . 27-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-2
Getting an Exception at the Command Line . . . . . . . . . . . . . . . . . . 27-2
Getting an Exception in Your Program Code . . . . . . . . . . . . . . . . . . 27-3
Generating a New Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-3
Throw an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-4
Suggestions on How to Throw an Exception . . . . . . . . . . . . . . . . . . 27-4
Respond to an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-6
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-6
The try/catch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-6
Suggestions on How to Handle an Exception . . . . . . . . . . . . . . . . . 27-7
Clean Up When Functions Complete . . . . . . . . . . . . . . . . . . . . . . . . . 27-9
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-9
Examples of Cleaning Up a Program Upon Exit . . . . . . . . . . . . . . . 27-10
Retrieving Information About the Cleanup Routine . . . . . . . . . . . . 27-11
Using onCleanup Versus try/catch . . . . . . . . . . . . . . . . . . . . . . . . 27-12
onCleanup in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-12
Issue Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-13
Issue Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-13
Throw Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-13
Add Run-Time Parameters to Your Warnings and Errors . . . . . . . . 27-14
Add Identifiers to Warnings and Errors . . . . . . . . . . . . . . . . . . . . . 27-14
Suppress Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-16
Turn Warnings On and Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-16
Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-18
Disable and Restore a Particular Warning . . . . . . . . . . . . . . . . . . . 27-18
Disable and Restore Multiple Warnings . . . . . . . . . . . . . . . . . . . . . 27-19
xxii Contents
Change How Warnings Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-20
Enable Verbose Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-20
Display a Stack Trace on a Specific Warning . . . . . . . . . . . . . . . . . 27-20
Use try/catch to Handle Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-21
Program Scheduling
28
Schedule Command Execution Using Timer . . . . . . . . . . . . . . . . . . . 28-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-2
Example: Displaying a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-2
Timer Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-4
Associating Commands with Timer Object Events . . . . . . . . . . . . . . 28-4
Creating Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-5
Specifying the Value of Callback Function Properties . . . . . . . . . . . 28-6
Handling Timer Queuing Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . 28-8
Drop Mode (Default) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-8
Error Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-9
Queue Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-10
Performance
29
Measure the Performance of Your Code . . . . . . . . . . . . . . . . . . . . . . 29-2
Overview of Performance Timing Functions . . . . . . . . . . . . . . . . . . 29-2
Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2
Time Portions of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2
The cputime Function vs. tic/toc and timeit . . . . . . . . . . . . . . . . . . . 29-2
Tips for Measuring Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-3
Profile Your Code to Improve Performance . . . . . . . . . . . . . . . . . . . . 29-4
What Is Profiling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-4
Profile Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-4
Profile Multiple Statements in Command Window . . . . . . . . . . . . . 29-10
Profile an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-11
Determine Code Coverage Using the Profiler . . . . . . . . . . . . . . . . . 29-12
Techniques to Improve Performance . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Programming Practices for Performance . . . . . . . . . . . . . . . . . . . . 29-14
Tips on Specific MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . 29-15
Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-16
Preallocating a Nondouble Matrix . . . . . . . . . . . . . . . . . . . . . . . . 29-16
xxiii
Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-18
Using Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-18
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-19
Logical Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-20
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-21
Ordering, Setting, and Counting Operations . . . . . . . . . . . . . . . . . 29-22
Functions Commonly Used in Vectorization . . . . . . . . . . . . . . . . . 29-23
Memory Usage
30
Strategies for Efficient Use of Memory . . . . . . . . . . . . . . . . . . . . . . . 30-2
Use Appropriate Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-2
Avoid Temporary Copies of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-3
Reclaim Used Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-4
Resolve “Out of Memory” Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Leverage tall Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Leverage the Memory of Multiple Machines . . . . . . . . . . . . . . . . . . 30-7
Load Only as Much Data as You Need . . . . . . . . . . . . . . . . . . . . . . . 30-7
Increase System Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-8
Set the Process Limit on Linux Systems . . . . . . . . . . . . . . . . . . . . . 30-8
Disable Java VM on Linux Systems . . . . . . . . . . . . . . . . . . . . . . . . . 30-9
How MATLAB Allocates Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-10
Memory Allocation for Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-10
Data Structures and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-13
Avoid Unnecessary Copies of Data . . . . . . . . . . . . . . . . . . . . . . . . . . 30-17
Passing Values to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-17
Why Pass-by-Value Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-19
Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-19
Custom Help and Documentation
31
Create Help for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-2
Help Text from the doc Command . . . . . . . . . . . . . . . . . . . . . . . . . . 31-2
Custom Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-3
Check Which Programs Have Help . . . . . . . . . . . . . . . . . . . . . . . . . . 31-8
Create Help Summary Files — Contents.m . . . . . . . . . . . . . . . . . . . 31-10
What Is a Contents.m File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-10
Create a Contents.m File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-10
Check an Existing Contents.m File . . . . . . . . . . . . . . . . . . . . . . . . 31-11
Customize Code Suggestions and Completions . . . . . . . . . . . . . . . 31-12
Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-13
xxiv Contents
Signature Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-13
Argument Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-14
Create Function Signature File . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-17
How Function Signature Information is Used . . . . . . . . . . . . . . . . 31-18
Multiple Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-19
Display Custom Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-21
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-21
Create HTML Help Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-22
Create info.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-23
Create helptoc.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-24
Build a Search Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-26
Address Validation Errors for info.xml Files . . . . . . . . . . . . . . . . . 31-27
Display Custom Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-29
How to Display Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-29
Elements of the demos.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . 31-30
Projects
32
Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
What Are Projects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Create Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Open Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Set up Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-3
Add Files to Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-5
Other Ways to Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-6
Automate Startup and Shutdown Tasks . . . . . . . . . . . . . . . . . . . . . . . 32-8
Specify Project Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-8
Set Startup Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-8
Specify Startup and Shutdown Files . . . . . . . . . . . . . . . . . . . . . . . . 32-8
Manage Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-10
Automatic Updates When Renaming, Deleting, or Removing Files
................................................ 32-11
Find Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Group and Sort Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Search for and Filter Project Files . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Search the Content in Project Files . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Create Shortcuts to Frequent Tasks . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Run Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Create Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Organize Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Add Labels to Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
Add Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
View and Edit Label Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
Create Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-17
xxv
Create Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18
Create a Custom Task Function . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18
Run a Custom Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18
Save Custom Task Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-19
Componentize Large Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-20
Add or Remove Reference to a Project . . . . . . . . . . . . . . . . . . . . . 32-20
View, Edit, or Run Referenced Project Files . . . . . . . . . . . . . . . . . 32-20
Extract Folder to Create a Referenced Project . . . . . . . . . . . . . . . 32-21
Manage Changes in Referenced Project Using Checkpoints . . . . . 32-21
Share Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-23
Create an Export Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-26
Upgrade Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-27
Run Upgrade Project Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-27
Examine Upgrade Project Report . . . . . . . . . . . . . . . . . . . . . . . . . 32-28
Analyze Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-30
Run a Dependency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-30
Explore the Dependency Graph, Views, and Filters . . . . . . . . . . . . 32-32
Investigate and Resolve Problems . . . . . . . . . . . . . . . . . . . . . . . . . 32-37
Find Required Products and Toolboxes . . . . . . . . . . . . . . . . . . . . . 32-39
Find File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-40
Save and Compare Dependency Graphs . . . . . . . . . . . . . . . . . . . . 32-42
Use Source Control with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . 32-44
Setup Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-44
Perform Source Control Operations . . . . . . . . . . . . . . . . . . . . . . . 32-46
Work with Derived Files in Projects . . . . . . . . . . . . . . . . . . . . . . . 32-53
Find Project Files With Unsaved Changes . . . . . . . . . . . . . . . . . . . 32-54
Manage Open Files When Closing a Project . . . . . . . . . . . . . . . . . 32-54
Create and Edit Projects Programmatically . . . . . . . . . . . . . . . . . . 32-55
Explore an Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-62
Source Control Interface
33
About MathWorks Source Control Integration . . . . . . . . . . . . . . . . . 33-2
Classic and Distributed Source Control . . . . . . . . . . . . . . . . . . . . . . 33-2
Select or Disable Source Control System . . . . . . . . . . . . . . . . . . . . . 33-4
Select Source Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-4
Disable Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-4
Create New Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-5
Create Git Repository on Your Local System . . . . . . . . . . . . . . . . . . 33-5
Create SVN Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-5
Review Changes in Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-7
xxvi Contents
Mark Files for Addition to Source Control . . . . . . . . . . . . . . . . . . . . 33-8
Resolve Source Control Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-9
Examining and Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . 33-9
Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-9
Merge Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10
Extract Conflict Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10
Commit Modified Files to Source Control . . . . . . . . . . . . . . . . . . . . 33-12
Revert Changes in Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Revert Local Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Revert a File to a Specified Revision . . . . . . . . . . . . . . . . . . . . . . . 33-13
Set Up SVN Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-14
SVN Source Control Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-14
Register Binary Files with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-14
Standard Repository Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-17
Tag Versions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-17
Enforce Locking Files Before Editing . . . . . . . . . . . . . . . . . . . . . . 33-17
Share a Subversion Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-18
Check Out from SVN Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-19
Retrieve Tagged Version of Repository . . . . . . . . . . . . . . . . . . . . . 33-20
Update SVN File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . 33-21
Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Get SVN File Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-22
Manage SVN Repository Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-22
Set Up Git Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23
Install Command-Line Git Client and Configure MATLAB Installation
................................................ 33-23
Use SSH Authentication with MATLAB . . . . . . . . . . . . . . . . . . . . . 33-24
Register Binary Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-25
Add Git Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-26
Clone from Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-28
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-28
Update Git File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . . 33-29
Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29
Branch and Merge with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30
Create Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30
Switch Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-31
Compare Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-32
Merge Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-32
Revert to Head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-33
Delete Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-33
xxvii
Pull, Push and Fetch Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . 33-34
Pull and Push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-34
Fetch and Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-35
Use Git Stashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-35
Move, Rename, or Delete Files Under Source Control . . . . . . . . . . 33-37
Customize External Source Control to Use MATLAB for Diff and
Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-38
Finding the Full Paths for MATLAB Diff and Merge . . . . . . . . . . . . 33-38
Integration with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-39
Integration with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-40
Integration with Other Source Control Tools . . . . . . . . . . . . . . . . . 33-40
MSSCCI Source Control Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 33-42
Set Up MSSCCI Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-43
Create Projects in Source Control System . . . . . . . . . . . . . . . . . . . 33-43
Specify Source Control System with MATLAB Software . . . . . . . . 33-44
Register Source Control Project with MATLAB Software . . . . . . . . 33-45
Add Files to Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-47
Check Files In and Out from MSSCCI Source Control . . . . . . . . . . 33-48
Check Files Into Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-48
Check Files Out of Source Control . . . . . . . . . . . . . . . . . . . . . . . . 33-48
Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-49
Additional MSSCCI Source Control Actions . . . . . . . . . . . . . . . . . . 33-50
Getting the Latest Version of Files for Viewing or Compiling . . . . . 33-50
Removing Files from the Source Control System . . . . . . . . . . . . . . 33-51
Showing File History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-51
Comparing the Working Copy of a File to the Latest Version in Source
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-52
Viewing Source Control Properties of a File . . . . . . . . . . . . . . . . . 33-53
Starting the Source Control System . . . . . . . . . . . . . . . . . . . . . . . 33-54
Access MSSCCI Source Control from Editors . . . . . . . . . . . . . . . . . 33-56
Troubleshoot MSSCCI Source Control Problems . . . . . . . . . . . . . . 33-57
Source Control Error: Provider Not Present or Not Installed Properly
................................................ 33-57
Restriction Against @ Character . . . . . . . . . . . . . . . . . . . . . . . . . . 33-58
Add to Source Control Is the Only Action Available . . . . . . . . . . . . 33-58
More Solutions for Source Control Problems . . . . . . . . . . . . . . . . 33-58
Unit Testing
34
Write Test Using Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-3
Write Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-6
xxviii Contents
Write Script-Based Test Using Local Functions . . . . . . . . . . . . . . . 34-11
Extending Script-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-14
Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-14
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-14
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 34-15
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-15
Run Tests in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-17
Write Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-20
Create Test Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-20
Run the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22
Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22
Write Simple Test Case Using Functions . . . . . . . . . . . . . . . . . . . . . 34-24
Write Test Using Setup and Teardown Functions . . . . . . . . . . . . . . 34-28
Extending Function-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-33
Fixtures for Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . 34-33
Test Logging and Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34
Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34
Test Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-35
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 34-35
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-36
Author Class-Based Unit Tests in MATLAB . . . . . . . . . . . . . . . . . . . 34-37
The Test Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-37
The Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-37
Additional Features for Advanced Test Classes . . . . . . . . . . . . . . . 34-38
Write Simple Test Case Using Classes . . . . . . . . . . . . . . . . . . . . . . . 34-40
Write Setup and Teardown Code Using Classes . . . . . . . . . . . . . . . 34-44
Test Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-44
Test Case with Method-Level Setup Code . . . . . . . . . . . . . . . . . . . 34-44
Test Case with Class-Level Setup Code . . . . . . . . . . . . . . . . . . . . . 34-45
Table of Verifications, Assertions, and Other Qualifications . . . . . 34-47
Tag Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-49
Tag Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-49
Select and Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-50
Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . 34-53
Create Basic Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-56
Create Advanced Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-58
Create Basic Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-63
Create Advanced Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . 34-68
xxix
Use External Parameters in Parameterized Test . . . . . . . . . . . . . . . 34-75
Create Simple Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-79
Run Tests for Various Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-81
Set Up Example Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-81
Run All Tests in Class or Function . . . . . . . . . . . . . . . . . . . . . . . . . 34-81
Run Single Test in Class or Function . . . . . . . . . . . . . . . . . . . . . . . 34-81
Run Test Suites by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-82
Run Test Suites from Test Array . . . . . . . . . . . . . . . . . . . . . . . . . . 34-82
Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . . 34-83
Programmatically Access Test Diagnostics . . . . . . . . . . . . . . . . . . . 34-84
Add Plugin to Test Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-85
Write Plugins to Extend TestRunner . . . . . . . . . . . . . . . . . . . . . . . . 34-87
Custom Plugins Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-87
Extending Test Session Level Plugin Methods . . . . . . . . . . . . . . . . 34-87
Extending Test Suite Level Plugin Methods . . . . . . . . . . . . . . . . . . 34-88
Extending Test Class Level Plugin Methods . . . . . . . . . . . . . . . . . 34-88
Extending Test Level Plugin Methods . . . . . . . . . . . . . . . . . . . . . . 34-89
Create Custom Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-90
Run Tests in Parallel with Custom Plugin . . . . . . . . . . . . . . . . . . . . 34-95
Write Plugin to Add Data to Test Results . . . . . . . . . . . . . . . . . . . 34-103
Write Plugin to Save Diagnostic Details . . . . . . . . . . . . . . . . . . . . 34-108
Plugin to Generate Custom Test Output Format . . . . . . . . . . . . . . 34-112
Analyze Test Case Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-115
Analyze Failed Test Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-118
Rerun Failed Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-120
Dynamically Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-123
Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-123
Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . 34-125
Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . 34-127
Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-129
Create Custom Boolean Constraint . . . . . . . . . . . . . . . . . . . . . . . . 34-132
Create Custom Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-136
Overview of App Testing Framework . . . . . . . . . . . . . . . . . . . . . . . 34-140
App Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-140
Gesture Support of UI Components . . . . . . . . . . . . . . . . . . . . . . 34-140
Write a Test for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-141
xxx Contents
Write Test for App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-144
Write Test That Uses App Testing and Mocking Frameworks . . . 34-148
Create App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-148
Test App With Manual Intervention . . . . . . . . . . . . . . . . . . . . . . . 34-149
Create Fully Automated Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-150
Overview of Performance Testing Framework . . . . . . . . . . . . . . . . 34-153
Determine Bounds of Measured Code . . . . . . . . . . . . . . . . . . . . . 34-153
Types of Time Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-154
Write Performance Tests with Measurement Boundaries . . . . . . . 34-154
Run Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-155
Understand Invalid Test Results . . . . . . . . . . . . . . . . . . . . . . . . . 34-155
Test Performance Using Scripts or Functions . . . . . . . . . . . . . . . . 34-157
Test Performance Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 34-161
Measure Fast Executing Test Code . . . . . . . . . . . . . . . . . . . . . . . . 34-167
Create Mock Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-170
Specify Mock Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-177
Define Mock Method Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 34-177
Define Mock Property Behavior . . . . . . . . . . . . . . . . . . . . . . . . . 34-178
Define Repeating and Subsequent Behavior . . . . . . . . . . . . . . . . 34-179
Summary of Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-181
Qualify Mock Object Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . 34-182
Qualify Mock Method Interaction . . . . . . . . . . . . . . . . . . . . . . . . 34-182
Qualify Mock Property Interaction . . . . . . . . . . . . . . . . . . . . . . . 34-183
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-184
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-186
Develop and Integrate Software with Continuous Integration . . 34-188
Continuous Integration Workflow . . . . . . . . . . . . . . . . . . . . . . . . 34-188
Continuous Integration with MathWorks Products . . . . . . . . . . . 34-190
System object Usage and Authoring
35
What Are System Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-2
Running a System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-3
System Object Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-3
System Objects vs MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-5
System Objects vs. MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 35-5
Process Audio Data Using Only MATLAB Functions Code . . . . . . . . . . . . 35-5
Process Audio Data Using System Objects . . . . . . . . . . . . . . . . . . . . . . . 35-6
System Design in MATLAB Using System Objects . . . . . . . . . . . . . . . . . . 35-7
System Design and Simulation in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 35-7
xxxi
Create Individual Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-7
Configure Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-8
Create and Configure Components at the Same Time . . . . . . . . . . . . . . . 35-8
Assemble Components Into System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-9
Run Your System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-9
Reconfiguring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-10
Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-11
Create System Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-11
Define Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-11
Change the Number of Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-13
Validate Property and Input Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-16
Validate a Single Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-16
Validate Interdependent Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-16
Validate Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-16
Complete Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-16
Initialize Properties and Setup One-Time Calculations . . . . . . . . . . . . . 35-18
Set Property Values at Construction Time . . . . . . . . . . . . . . . . . . . . . . . 35-20
Reset Algorithm and Release Resources . . . . . . . . . . . . . . . . . . . . . . . . . 35-22
Reset Algorithm State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-22
Release System Object Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-22
Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24
Specify Property as Nontunable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24
Specify Property as DiscreteState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24
Example Class with Various Property Attributes . . . . . . . . . . . . . . . . . . 35-24
Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-26
Specify Inactive Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-26
Complete Class Definition File with Inactive Properties Method . . . . . . 35-26
Limit Property Values to Finite List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Property Validation with mustBeMember . . . . . . . . . . . . . . . . . . . . . . . 35-28
Enumeration Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Create a Whiteboard System object . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-29
Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-32
Define Composite System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-34
Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-36
Use the FiniteSource Class and Specify End of the Source . . . . . . . . . . 35-36
Complete Class Definition File with Finite Source . . . . . . . . . . . . . . . . . 35-36
Save and Load System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-38
Save System Object and Child Object . . . . . . . . . . . . . . . . . . . . . . . . . . 35-38
Load System Object and Child Object . . . . . . . . . . . . . . . . . . . . . . . . . . 35-38
Complete Class Definition Files with Save and Load . . . . . . . . . . . . . . . 35-38
Define System Object Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41
xxxii Contents
Handle Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43
React to Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43
Restrict Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43
Summary of Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-45
Setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-45
Running the Object or Step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 35-45
Reset Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-46
Release Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-47
Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-48
setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-48
Running the Object or step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 35-48
reset Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-49
release Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-49
Tips for Defining System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
Inputs and Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
Using ~ as an Input Argument in Method Definitions . . . . . . . . . . . . . . 35-50
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
Text Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-51
Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-51
Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-52
Insert System Object Code Using MATLAB Editor . . . . . . . . . . . . . . . . . 35-53
Define System Objects with Code Insertion . . . . . . . . . . . . . . . . . . . . . . 35-53
Create a Temperature Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-55
Create Custom Property for Freezing Point . . . . . . . . . . . . . . . . . . . . . . 35-56
Add Method to Validate Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-57
Analyze System Object Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-58
View and Navigate System object Code . . . . . . . . . . . . . . . . . . . . . . . . 35-58
Example: Go to StepImpl Method Using Analyzer . . . . . . . . . . . . . . . . . 35-58
Use Global Variables in System Objects . . . . . . . . . . . . . . . . . . . . . . . . . 35-60
System Object Global Variables in MATLAB . . . . . . . . . . . . . . . . . . . . . 35-60
System Object Global Variables in Simulink . . . . . . . . . . . . . . . . . . . . . 35-60
Create Moving Average System object . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-64
Create New System Objects for File Input and Output . . . . . . . . . . . . . 35-69
xxxiii
Language
35
1
Syntax Basics
• “Continue Long Statements on Multiple Lines” on page 1-2
• “Ignore Function Outputs” on page 1-3
• “Variable Names” on page 1-4
• “Case and Space Sensitivity” on page 1-5
• “Command vs. Function Syntax” on page 1-6
• “Common Errors When Calling Functions” on page 1-9
1 Syntax Basics
Continue Long Statements on Multiple Lines
This example shows how to continue a statement to the next line using ellipsis (...).
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;
Build a long character vector by concatenating shorter vectors together:
mytext = ['Accelerating the pace of ' ...
'engineering and science'];
The start and end quotation marks for a character vector must appear on the same line. For example,
this code returns an error, because each line contains only one quotation mark:
mytext = 'Accelerating the pace of ...
engineering and science'
An ellipsis outside a quoted text is equivalent to a space. For example,
x = [1.23...
4.56];
is the same as
x = [1.23 4.56];
1-2
Ignore Function Outputs
Ignore Function Outputs
This example shows how to ignore specific outputs from a function using the tilde (~) operator.
Request all three possible outputs from the fileparts function.
helpFile = which('help');
[helpPath,name,ext] = fileparts(helpFile);
The current workspace now contains three variables from fileparts: helpPath, name, and ext. In
this case, the variables are small. However, some functions return results that use much more
memory. If you do not need those variables, they waste space on your system.
If you do not use the tilde operator, you can request only the first N outputs of a function (where N is
less than or equal to the number of possible outputs) and ignore any remaining outputs. For example,
request only the first output, ignoring the second and third.
helpPath = fileparts(helpFile);
If you request more than one output, enclose the variable names in square brackets, []. The
following code ignores the output argument ext.
[helpPath,name] = fileparts(helpFile);
To ignore function outputs in any position in the argument list, use the tilde operator. For example,
ignore the first output using a tilde.
[~,name,ext] = fileparts(helpFile);
You can ignore any number of function outputs using the tilde operator. Separate consecutive tildes
with a comma. For example, this code ignores the first two output arguments.
[~,~,ext] = fileparts(helpFile);
See Also
More About
• “Ignore Function Inputs” on page 21-10
1-3
1 Syntax Basics
Variable Names
In this section...
“Valid Names” on page 1-4
“Conflicts with Function Names” on page 1-4
Valid Names
A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB is case
sensitive, so A and a are not the same variable. The maximum length of a variable name is the value
that the namelengthmax command returns.
You cannot define variables with the same names as MATLAB keywords, such as if or end. For a
complete list, run the iskeyword command.
Examples of valid names: Invalid names:
x6 6x
lastValue end
n_factorial n!
Conflicts with Function Names
Avoid creating variables with the same name as a function (such as i, j, mode, char, size, and
path). In general, variable names take precedence over function names. If you create a variable that
uses the name of a function, you sometimes get unexpected results.
Check whether a proposed name is already in use with the exist or which function. exist returns
0 if there are no existing variables, functions, or other artifacts with the proposed name. For example:
exist checkname
ans =
0
If you inadvertently create a variable with a name conflict, remove the variable from memory with the
clear function.
Another potential source of name conflicts occurs when you define a function that calls load or eval
(or similar functions) to add variables to the workspace. In some cases, load or eval add variables
that have the same names as functions. Unless these variables are in the function workspace before
the call to load or eval, the MATLAB parser interprets the variable names as function names. For
more information, see:
• “Unexpected Results When Loading Variables Within a Function”
• “Alternatives to the eval Function” on page 2-86
See Also
clear | exist | iskeyword | isvarname | namelengthmax | which
1-4
Case and Space Sensitivity
Case and Space Sensitivity
MATLAB code is sensitive to casing, and insensitive to blank spaces except when defining arrays.
Uppercase and Lowercase
In MATLAB code, use an exact match with regard to case for variables, files, and functions. For
example, if you have a variable, a, you cannot refer to that variable as A. It is a best practice to use
lowercase only when naming functions. This is especially useful when you use both Microsoft®
Windows® and UNIX®1 platforms because their file systems behave differently with regard to case.
When you use the help function, the help displays some function names in all uppercase, for
example, PLOT, solely to distinguish the function name from the rest of the text. Some functions for
interfacing to Oracle® Java® software do use mixed case and the command-line help and the
documentation accurately reflect that.
Spaces
Blank spaces around operators such as -, :, and ( ), are optional, but they can improve readability.
For example, MATLAB interprets the following statements the same way.
y = sin (3 * pi) / 2
y=sin(3*pi)/2
However, blank spaces act as delimiters in horizontal concatenation. When defining row vectors, you
can use spaces and commas interchangeably to separate elements:
A = [1, 0 2, 3 3]
A =
1 0 2 3 3
Because of this flexibility, check to ensure that MATLAB stores the correct values. For example, the
statement [1 sin (pi) 3] produces a much different result than [1 sin(pi) 3] does.
[1 sin (pi) 3]
Error using sin
Not enough input arguments.
[1 sin(pi) 3]
ans =
1.0000 0.0000 3.0000
1. UNIX is a registered trademark of The Open Group in the United States and other countries.
1-5
1 Syntax Basics
Command vs. Function Syntax
Command and Function Syntaxes
In MATLAB, these statements are equivalent:
load durer.mat % Command syntax
load('durer.mat') % Function syntax
This equivalence is sometimes referred to as command-function duality.
All functions support this standard function syntax:
[output1, ..., outputM] = functionName(input1, ..., inputN)
If you do not require any outputs from the function, and all of the inputs are character vectors (that
is, text enclosed in single quotation marks), you can use this simpler command syntax:
functionName input1 ... inputN
With command syntax, you separate inputs with spaces rather than commas, and do not enclose input
arguments in parentheses. Command syntax always passes inputs as character vectors. To use strings
as inputs, use the function syntax. If a character vector contains a space, use the function syntax.
When a function input is a variable, you must use function syntax to pass the value to the function.
Command syntax always passes inputs as character vectors and cannot pass variable values. For
example, create a variable and call the disp function with function syntax to pass the value of the
variable:
A = 123;
disp(A)
This code returns the expected result,
123
You cannot use command syntax to pass the value of A, because this call
disp A
is equivalent to
disp('A')
and returns
Avoid Common Syntax Mistakes
Suppose that your workspace contains these variables:
filename = 'accounts.txt';
A = int8(1:8);
B = A;
1-6
Command vs. Function Syntax
The following table illustrates common misapplications of command syntax.
This Command... Is Equivalent to... Correct Syntax for Passing Value
open filename open('filename') open(filename)
isequal A B isequal('A','B') isequal(A,B)
strcmp class(A) int8 strcmp('class(A)','int8') strcmp(class(A),'int8')
cd matlabroot cd('matlabroot') cd(matlabroot)
isnumeric 500 isnumeric('500') isnumeric(500)
round 3.499 round('3.499'), which is round(3.499)
equivalent to round([51 46 52
57 57])
disp hello world disp('hello','world') disp('hello world')
disp "string" disp('"string"') disp("string")
Passing Variable Names
Some functions expect character vectors for variable names, such as save, load, clear, and whos.
For example,
whos -file durer.mat X
requests information about variable X in the example file durer.mat. This command is equivalent to
whos('-file','durer.mat','X')
How MATLAB Recognizes Command Syntax
Consider the potentially ambiguous statement
ls ./d
This could be a call to the ls function with the folder ./d as its argument. It also could request
element-wise division on the array ls, using the variable d as the divisor.
If you issue such a statement at the command line, MATLAB can access the current workspace and
path to determine whether ls and d are functions or variables. However, some components, such as
the Code Analyzer and the Editor/Debugger, operate without reference to the path or workspace. In
those cases, MATLAB uses syntactic rules to determine whether an expression is a function call using
command syntax.
In general, when MATLAB recognizes an identifier (which might name a function or a variable), it
analyzes the characters that follow the identifier to determine the type of expression, as follows:
• An equal sign (=) implies assignment. For example:
ls =d
• An open parenthesis after an identifier implies a function call. For example:
ls('./d')
• Space after an identifier, but not after a potential operator, implies a function call using command
syntax. For example:
1-7
1 Syntax Basics
ls ./d
• Spaces on both sides of a potential operator, or no spaces on either side of the operator, imply an
operation on variables. For example, these statements are equivalent:
ls ./ d
ls./d
Therefore, the potentially ambiguous statement ls ./d is a call to the ls function using command
syntax.
The best practice is to avoid defining variable names that conflict with common functions, to prevent
any ambiguity.
1-8
Common Errors When Calling Functions
Common Errors When Calling Functions
In this section...
“Conflicting Function and Variable Names” on page 1-9
“Undefined Functions or Variables” on page 1-9
Conflicting Function and Variable Names
MATLAB throws an error if a variable and function have been given the same name and there is
insufficient information available for MATLAB to resolve the conflict. You may see an error message
something like the following:
Error: <functionName> was previously used as a variable,
conflicting with its use here as the name of a function
or command.
where <functionName> is the name of the function.
Certain uses of the eval and load functions can also result in a similar conflict between variable and
function names. For more information, see:
• “Conflicts with Function Names” on page 1-4
• “Unexpected Results When Loading Variables Within a Function”
• “Alternatives to the eval Function” on page 2-86
Undefined Functions or Variables
You may encounter the following error message, or something similar, while working with functions
or variables in MATLAB:
Undefined function or variable 'x'.
These errors usually indicate that MATLAB cannot find a particular variable or MATLAB program file
in the current directory or on the search path. The root cause is likely to be one of the following:
• The name of the function has been misspelled.
• The function name and name of the file containing the function are not the same.
• The toolbox to which the function belongs is not installed.
• The search path to the function has been changed.
• The function is part of a toolbox that you do not have a license for.
Follow the steps described in this section to resolve this situation.
Verify the Spelling of the Function Name
One of the most common errors is misspelling the function name. Especially with longer function
names or names containing similar characters (e.g., letter l and numeral one), it is easy to make an
error that is not easily detected.
If you misspell a MATLAB function, a suggested function name appears in the Command Window. For
example, this command fails because it includes an uppercase letter in the function name:
1-9
1 Syntax Basics
accumArray
Undefined function or variable 'accumArray'.
Did you mean:
>> accumarray
Press Enter to execute the suggested command or Esc to dismiss it.
Make Sure the Function Name Matches the File Name
You establish the name for a function when you write its function definition line. This name should
always match the name of the file you save it to. For example, if you create a function named
curveplot,
function curveplot(xVal, yVal)
- program code -
then you should name the file containing that function curveplot.m. If you create a pcode file for
the function, then name that file curveplot.p. In the case of conflicting function and file names, the
file name overrides the name given to the function. In this example, if you save the curveplot
function to a file named curveplotfunction.m, then attempts to invoke the function using the
function name will fail:
curveplot
Undefined function or variable 'curveplot'.
If you encounter this problem, change either the function name or file name so that they are the
same. If you have difficulty locating the file that uses this function, use the MATLAB Find Files utility
as follows:
1
On the Home tab, in the File section, click Find Files.
2 Under Find files named: enter *.m
3 Under Find files containing text: enter the function name.
4 Click the Find button
1-10
Common Errors When Calling Functions
Make Sure the Toolbox Is Installed
If you are unable to use a built-in function from MATLAB or its toolboxes, make sure that the function
is installed.
If you do not know which toolbox supports the function you need, search for the function
documentation at https://www.mathworks.com/help. The toolbox name appears at the top of the
function reference page.
Once you know which toolbox the function belongs to, use the ver function to see which toolboxes
are installed on the system from which you run MATLAB. The ver function displays a list of all
currently installed MathWorks® products. If you can locate the toolbox you need in the output
displayed by ver, then the toolbox is installed. For help with installing MathWorks products, see the
Installation Guide documentation.
If you do not see the toolbox and you believe that it is installed, then perhaps the MATLAB path has
been set incorrectly. Go on to the next section.
Verify the Path Used to Access the Function
This step resets the path to the default.
Because MATLAB stores the toolbox information in a cache file, you will need to first update this
cache and then reset the path. To do this,
1
On the Home tab, in the Environment section, click Preferences.
The Preference dialog box appears.
2 Under the MATLAB > General node, click the Update Toolbox Path Cache button.
3
On the Home tab, in the Environment section, click Set Path....
1-11
1 Syntax Basics
The Set Path dialog box opens.
4 Click Default.
A small dialog box opens warning that you will lose your current path settings if you proceed.
Click Yes if you decide to proceed.
(If you have added any custom paths to MATLAB, you will need to restore those later)
Run ver again to see if the toolbox is installed. If not, you may need to reinstall this toolbox to use
this function. For more information about installing a toolbox, see How do I install additional
toolboxes into an existing installation of MATLAB.
Once ver shows your toolbox, run the following command to see if you can find the function:
which -all <functionname>
replacing <functionname> with the name of the function. You should be presented with the path(s)
of the function file. If you get a message indicating that the function name was not found, you may
need to reinstall that toolbox to make the function active.
1-12
2
Program Components
• “MATLAB Operators and Special Characters” on page 2-2
• “Array vs. Matrix Operations” on page 2-20
• “Compatible Array Sizes for Basic Operations” on page 2-25
• “Array Comparison with Relational Operators” on page 2-29
• “Operator Precedence” on page 2-32
• “Average Similar Data Points Using a Tolerance” on page 2-34
• “Group Scattered Data Using a Tolerance” on page 2-36
• “Bit-Wise Operations” on page 2-38
• “Perform Cyclic Redundancy Check” on page 2-44
• “Conditional Statements” on page 2-47
• “Loop Control Statements” on page 2-49
• “Regular Expressions” on page 2-51
• “Lookahead Assertions in Regular Expressions” on page 2-63
• “Tokens in Regular Expressions” on page 2-66
• “Dynamic Regular Expressions” on page 2-72
• “Comma-Separated Lists” on page 2-79
• “Alternatives to the eval Function” on page 2-86
2 Program Components
MATLAB Operators and Special Characters
This page contains a comprehensive listing of all MATLAB operators, symbols, and special characters.
Arithmetic Operators
Symbol Role More Information
+ Addition plus
+ Unary plus uplus
- Subtraction minus
- Unary minus uminus
.* Element-wise multiplication times
* Matrix multiplication mtimes
./ Element-wise right division rdivide
/ Matrix right division mrdivide
.\ Element-wise left division ldivide
\ Matrix left division mldivide
(also known as backslash)
.^ Element-wise power power
^ Matrix power mpower
.' Transpose transpose
' Complex conjugate transpose ctranspose
Relational Operators
Symbol Role More Information
== Equal to eq
~= Not equal to ne
> Greater than gt
>= Greater than or equal to ge
< Less than lt
<= Less than or equal to le
Logical Operators
Symbol Role More Information
& Logical AND and
| Logical OR or
&& Logical AND (with short- Logical Operators: Short-
circuiting) Circuit && ||
2-2