100% found this document useful (1 vote)
7 views

Matlab R2023a Programming Fundamentals Mathworks download

The document is a guide for MATLAB R2023a Programming Fundamentals, providing essential programming concepts and syntax. It includes links to additional resources and user guides for various MATLAB toolboxes. The document also contains contact information for MathWorks and outlines licensing and trademark details.

Uploaded by

galasodhamki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
7 views

Matlab R2023a Programming Fundamentals Mathworks download

The document is a guide for MATLAB R2023a Programming Fundamentals, providing essential programming concepts and syntax. It includes links to additional resources and user guides for various MATLAB toolboxes. The document also contains contact information for MathWorks and outlines licensing and trademark details.

Uploaded by

galasodhamki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Matlab R2023a Programming Fundamentals Mathworks

download

https://ebookbell.com/product/matlab-r2023a-programming-
fundamentals-mathworks-49473928

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Matlab R2023a Financial Instruments Toolbox Users Guide Mathworks

https://ebookbell.com/product/matlab-r2023a-financial-instruments-
toolbox-users-guide-mathworks-49478996

Matlab R2023a Function Reference Mathworks

https://ebookbell.com/product/matlab-r2023a-function-reference-
mathworks-49473926

Matlab R2023a Econometrics Toolbox Users Guide Mathworks

https://ebookbell.com/product/matlab-r2023a-econometrics-toolbox-
users-guide-mathworks-49478912

Matlab R2023a Deep Learning Toolbox Users Guide Mark Hudson Beale

https://ebookbell.com/product/matlab-r2023a-deep-learning-toolbox-
users-guide-mark-hudson-beale-49478930
Matlab R2023a Deep Learning Toolbox Reference Mark Hudson Beale

https://ebookbell.com/product/matlab-r2023a-deep-learning-toolbox-
reference-mark-hudson-beale-49478934

Matlab R2023a Simulink Users Guide Mathworks

https://ebookbell.com/product/matlab-r2023a-simulink-users-guide-
mathworks-49478950

Matlab R2023a Deep Learning Hdl Toolbox Users Guide Mathworks

https://ebookbell.com/product/matlab-r2023a-deep-learning-hdl-toolbox-
users-guide-mathworks-49478982

Matlab R2023a Simulink Reference Mathworks

https://ebookbell.com/product/matlab-r2023a-simulink-reference-
mathworks-49478994

Matlab R2023a Simulink 3d Animation Users Guide Mathworks

https://ebookbell.com/product/matlab-r2023a-simulink-3d-animation-
users-guide-mathworks-49479012
MATLAB®
Programming Fundamentals

R2023a
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–2023 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 MATLAB 7.5 (Release 2007b)
March 2008 Online only Revised for MATLAB 7.6 (Release 2008a)
October 2008 Online only Revised for MATLAB 7.7 (Release 2008b)
March 2009 Online only Revised for MATLAB 7.8 (Release 2009a)
September 2009 Online only Revised for MATLAB 7.9 (Release 2009b)
March 2010 Online only Revised for MATLAB 7.10 (Release 2010a)
September 2010 Online only Revised for MATLAB 7.11 (Release 2010b)
April 2011 Online only Revised for MATLAB 7.12 (Release 2011a)
September 2011 Online only Revised for MATLAB 7.13 (Release 2011b)
March 2012 Online only Revised for MATLAB 7.14 (Release 2012a)
September 2012 Online only Revised for MATLAB 8.0 (Release 2012b)
March 2013 Online only Revised for MATLAB 8.1 (Release 2013a)
September 2013 Online only Revised for MATLAB 8.2 (Release 2013b)
March 2014 Online only Revised for MATLAB 8.3 (Release 2014a)
October 2014 Online only Revised for MATLAB 8.4 (Release 2014b)
March 2015 Online only Revised for MATLAB 8.5 (Release 2015a)
September 2015 Online only Revised for MATLAB 8.6 (Release 2015b)
October 2015 Online only Rereleased for MATLAB 8.5.1 (Release 2015aSP1)
March 2016 Online only Revised for MATLAB 9.0 (Release 2016a)
September 2016 Online only Revised for MATLAB 9.1 (Release 2016b)
March 2017 Online only Revised for MATLAB 9.2 (Release 2017a)
September 2017 Online only Revised for MATLAB 9.3 (Release 2017b)
March 2018 Online only Revised for MATLAB 9.4 (Release 2018a)
September 2018 Online only Revised for MATLAB 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)
September 2020 Online only Revised for MATLAB 9.9 (Release 2020b)
March 2021 Online only Revised for MATLAB 9.10 (Release 2021a)
September 2021 Online only Revised for MATLAB 9.11 (Release 2021b)
March 2022 Online only Revised for MATLAB 9.12 (Release 2022a)
September 2022 Online only Revised for MATLAB 9.13 (Release 2022b)
March 2023 Online only Revised for MATLAB 9.14 (Release 2023a)
Contents

Language

Syntax Basics
1
Continue Long Statements on Multiple Lines . . . . . . . . . . . . . . . . . . . 1-2

Name=Value in Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3

Ignore Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4

Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5


Valid Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Conflicts with Function Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5

Case and Space Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7

Choose Command Syntax or Function Syntax . . . . . . . . . . . . . . . . . . . 1-8


Command Syntax and Function Syntax . . . . . . . . . . . . . . . . . . . . . . . 1-8
Avoid Common Syntax Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
How MATLAB Recognizes Command Syntax . . . . . . . . . . . . . . . . . . . 1-9

Resolve Error: Undefined Function or Variable . . . . . . . . . . . . . . . . . 1-11


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11

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

v
Compatible Array Sizes for Basic Operations . . . . . . . . . . . . . . . . . . 2-25
Inputs with Compatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25
Inputs with Incompatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27

Array Comparison with Relational Operators . . . . . . . . . . . . . . . . . . 2-30


Array Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-30
Logic Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32

Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33


Precedence of AND and OR Operators . . . . . . . . . . . . . . . . . . . . . . 2-33
Overriding Default Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33

Average Similar Data Points Using a Tolerance . . . . . . . . . . . . . . . . 2-35

Group Scattered Data Using a Tolerance . . . . . . . . . . . . . . . . . . . . . . 2-37

Bit-Wise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-39

Perform Cyclic Redundancy Check . . . . . . . . . . . . . . . . . . . . . . . . . . 2-45

Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-48

Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-50

Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-52


What Is a Regular Expression? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-52
Steps for Building Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-53
Operators and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-56

Lookahead Assertions in Regular Expressions . . . . . . . . . . . . . . . . . 2-64


Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-64
Overlapping Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-64
Logical AND Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-65

Tokens in Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-67


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-67
Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-69
Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-70
Tokens in Replacement Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-70
Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-71

Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-73


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-73
Dynamic Match Expressions — (??expr) . . . . . . . . . . . . . . . . . . . . . 2-74
Commands That Modify the Match Expression — (??@cmd) . . . . . . 2-74
Commands That Serve a Functional Purpose — (?@cmd) . . . . . . . . 2-75
Commands in Replacement Expressions — ${cmd} . . . . . . . . . . . . 2-77

Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-80


What Is a Comma-Separated List? . . . . . . . . . . . . . . . . . . . . . . . . . 2-80
Generating a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . . 2-80
Assigning Output from a Comma-Separated List . . . . . . . . . . . . . . . 2-82
Assigning to a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . 2-82
How to Use Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . 2-83

vi Contents
Fast Fourier Transform Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-86
Troubleshooting Operations with Comma-Separated Lists . . . . . . . . 2-86

Alternatives to the eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-91


Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-91
Variables with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . 2-91
Files with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-92
Function Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-92
Field Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-93
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-93

Classes (Data Types)

Overview of MATLAB Classes


3
Fundamental MATLAB Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2

Use is* Functions to Detect State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5

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
Loss of Precision Due to Conversion . . . . . . . . . . . . . . . . . . . . . . . . . 4-5

Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7


Double-Precision Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Single-Precision Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Creating Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Arithmetic Operations on Floating-Point Numbers . . . . . . . . . . . . . . 4-9
Largest and Smallest Values for Floating-Point Classes . . . . . . . . . . 4-10
Accuracy of Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Avoiding Common Problems with Floating-Point Arithmetic . . . . . . 4-12

Create Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15

Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16


Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16

Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18

vii
Display Format for Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19

Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21

Single Precision Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28

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

Build Pattern Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-40

Convert Numeric Values to Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-45

Convert Text to Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-48

Unicode and ASCII Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-52

Hexadecimal and Binary Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-54

viii Contents
Frequently Asked Questions About String Arrays . . . . . . . . . . . . . . . 6-58
Why Does Using Command Form With Strings Return An Error? . . 6-58
Why Do Strings in Cell Arrays Return an Error? . . . . . . . . . . . . . . . 6-59
Why Does length() of String Return 1? . . . . . . . . . . . . . . . . . . . . . . 6-59
Why Does isempty("") Return 0? . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-60
Why Does Appending Strings Using Square Brackets Return Multiple
Strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-61

Update Your Code to Accept Strings . . . . . . . . . . . . . . . . . . . . . . . . . 6-63


What Are String Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-63
Recommended Approaches for String Adoption in Old APIs . . . . . . 6-63
How to Adopt String Arrays in Old APIs . . . . . . . . . . . . . . . . . . . . . 6-65
Recommended Approaches for String Adoption in New Code . . . . . 6-65
How to Maintain Compatibility in New Code . . . . . . . . . . . . . . . . . . 6-66
How to Manually Convert Input Arguments . . . . . . . . . . . . . . . . . . 6-67
How to Check Argument Data Types . . . . . . . . . . . . . . . . . . . . . . . . 6-67
Terminology for Character and String Arrays . . . . . . . . . . . . . . . . . 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-12
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-15
Specify Length and Endpoints of Date or Duration Sequence . . . . . 7-16
Sequence of Datetime Values Using Calendar Rules . . . . . . . . . . . . 7-17

Share Code and Data Across Locales . . . . . . . . . . . . . . . . . . . . . . . . . 7-19


Write Locale-Independent Date and Time Code . . . . . . . . . . . . . . . . 7-19
Write Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
Read Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20

Extract or Assign Date and Time Components of Datetime Array . . 7-22

Combine Date and Time from Separate Variables . . . . . . . . . . . . . . 7-25

Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-27

ix
Compare Dates and Times Using Relational Operators . . . . . . . . . . 7-32
Compare Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-32

Plot Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-39

Core Functions Supporting Date and Time Arrays . . . . . . . . . . . . . . 7-50

Convert Between Text and datetime or duration Values . . . . . . . . . 7-51

Replace Discouraged Instances of Serial Date Numbers and Date


Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-60
Convert Serial Date Numbers and Date Strings . . . . . . . . . . . . . . . 7-60
Replace Functions That Use Date Numbers . . . . . . . . . . . . . . . . . . 7-60
Discouraged Syntaxes for Date and Time Components . . . . . . . . . . 7-64
Guidelines for Updating Your Own Functions . . . . . . . . . . . . . . . . . 7-65

Carryover in Date Vectors and Strings . . . . . . . . . . . . . . . . . . . . . . . . 7-67

Converting Date Vector Returns Unexpected Output . . . . . . . . . . . . 7-69

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

x Contents
Core Functions Supporting Categorical Arrays . . . . . . . . . . . . . . . . 8-39

Tables
9
Create Tables and Assign Data to Them . . . . . . . . . . . . . . . . . . . . . . . 9-2

Add and Delete Table Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9

Add, Delete, and Rearrange Table Variables . . . . . . . . . . . . . . . . . . . 9-12

Clean Messy and Missing Data in Tables . . . . . . . . . . . . . . . . . . . . . . 9-18

Rename and Describe Table Variables . . . . . . . . . . . . . . . . . . . . . . . . 9-26

Add Custom Properties to Tables and Timetables . . . . . . . . . . . . . . 9-33

Access Data in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-38


Summary of Table Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . 9-38
Table Indexing with Specified Rows and Variables . . . . . . . . . . . . . 9-43
Find Rows Where Values Meet Logical Conditions . . . . . . . . . . . . . 9-47
Dot Notation with Any Variable Name or Expression . . . . . . . . . . . . 9-50
Extract Data from Specified Rows and Variables . . . . . . . . . . . . . . . 9-52

Direct Calculations on Tables and Timetables . . . . . . . . . . . . . . . . . 9-54

Rules for Table and Timetable Mathematics . . . . . . . . . . . . . . . . . . . 9-61


Functions and Operators That Support Tables and Timetables . . . . 9-61
Rules for Operations on Tables and Timetables . . . . . . . . . . . . . . . . 9-61
Rules for Tables and Timetables with Units . . . . . . . . . . . . . . . . . . . 9-62

Calculations When Tables Have Both Numeric and Nonnumeric Data


..................................................... 9-65

Split Data into Groups and Calculate Statistics . . . . . . . . . . . . . . . . 9-73

Perform Calculations by Group in Table . . . . . . . . . . . . . . . . . . . . . . 9-76

Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-88

Grouping Variables To Split Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-93


Grouping Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-93
Group Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-93
The Split-Apply-Combine Workflow . . . . . . . . . . . . . . . . . . . . . . . . . 9-94
Missing Group Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-94

Changes to DimensionNames Property in R2016b . . . . . . . . . . . . . . 9-96

Data Cleaning and Calculations in Tables . . . . . . . . . . . . . . . . . . . . . 9-98

Grouped Calculations in Tables and Timetables . . . . . . . . . . . . . . . 9-116

xi
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

Select Times in Timetable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19

Clean Timetable with Missing, Duplicate, or Nonuniform Times


.................................................... 10-27

Using Row Labels in Table and Timetable Operations . . . . . . . . . . 10-36

Loma Prieta Earthquake Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 10-41

Preprocess and Explore Time-Stamped Data Using timetable . . . 10-51

Add Event Table from External Data to Timetable . . . . . . . . . . . . . 10-71

Find Events in Timetable Using Event Table . . . . . . . . . . . . . . . . . . 10-87

Structures
11
Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Create Scalar Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Access Values in Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Index into Nonscalar Structure Array . . . . . . . . . . . . . . . . . . . . . . . 11-4

Concatenate Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-8

Generate Field Names from Variables . . . . . . . . . . . . . . . . . . . . . . . 11-10

Access Data in Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 11-11

Access Elements of a Nonscalar Structure Array . . . . . . . . . . . . . . 11-13

Ways to Organize Data in Structure Arrays . . . . . . . . . . . . . . . . . . . 11-15


Plane Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-15
Element-by-Element Organization . . . . . . . . . . . . . . . . . . . . . . . . 11-16

Memory Requirements for Structure Array . . . . . . . . . . . . . . . . . . 11-18

xii Contents
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

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

Dictionaries
14
Map Data with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2

Dictionaries and Custom Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-7


Hash Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-7
Overload keyHash and keyMatch for Custom Classes . . . . . . . . . . . 14-7

xiii
Combining Unlike Classes
15
Valid Combinations of Unlike Classes . . . . . . . . . . . . . . . . . . . . . . . . 15-2

Combining Unlike Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-3


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-3
Example of Combining Unlike Integer Sizes . . . . . . . . . . . . . . . . . . 15-3
Example of Combining Signed with Unsigned . . . . . . . . . . . . . . . . . 15-4

Combining Integer and Noninteger Data . . . . . . . . . . . . . . . . . . . . . 15-5

Combining Cell Arrays with Non-Cell Arrays . . . . . . . . . . . . . . . . . . 15-6

Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-7

Concatenation Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-8


Combining Single and Double Types . . . . . . . . . . . . . . . . . . . . . . . . 15-8
Combining Integer and Double Types . . . . . . . . . . . . . . . . . . . . . . . 15-8
Combining Character and Double Types . . . . . . . . . . . . . . . . . . . . . 15-8
Combining Logical and Double Types . . . . . . . . . . . . . . . . . . . . . . . 15-8

Using Objects
16
Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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

Defining Your Own Classes


17

Scripts and Functions

Scripts
18
Create Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2

xiv Contents
Add Comments to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-3

Create and Run Sections in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-5


Divide Your File into Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-5
Run Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-6
Navigate Between Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-8
Behavior of Sections in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-8
Behavior of Sections in Loops and Conditional Statements . . . . . . . 18-8

Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-10

Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-12


Create a Script with Local Functions . . . . . . . . . . . . . . . . . . . . . . 18-12
Run Scripts with Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . 18-12
Restrictions for Local Functions and Variables . . . . . . . . . . . . . . . 18-13
Access Help for Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-13

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-7
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-7
Change View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-8
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-10
Save Live Scripts as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . . . 19-11

Modify Figures in Live Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12


Explore Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Add Formatting and Annotations . . . . . . . . . . . . . . . . . . . . . . . . . 19-13
Update Code with Figure Changes . . . . . . . . . . . . . . . . . . . . . . . . 19-15
Save and Print Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-16

Format Text in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-18


Change Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-20
Autoformatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-21

Insert Equations into the Live Editor . . . . . . . . . . . . . . . . . . . . . . . 19-24


Insert Equation Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-24
Insert LaTeX Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-26
Supported LaTeX Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-27

Add Interactive Controls to a Live Script . . . . . . . . . . . . . . . . . . . . 19-34


Insert Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-34
Modify Control Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-35

xv
Link Variables to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-35
Specify Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-37
Modify Control Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-37
Create Live Script with Multiple Interactive Controls . . . . . . . . . . 19-38
Share Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-41

Add Interactive Tasks to a Live Script . . . . . . . . . . . . . . . . . . . . . . . 19-43


What Are Live Editor Tasks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-43
Insert Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-43
Run Tasks and Surrounding Code . . . . . . . . . . . . . . . . . . . . . . . . . 19-46
Modify Output Argument Name . . . . . . . . . . . . . . . . . . . . . . . . . . 19-47
View and Edit Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-47
Custom Live Editor Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-48

Create Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-49


Create Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-49
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-49
Add Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-50
Run Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-50
Save Live Functions as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . 19-51

Add Help for Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-52


Add Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-52
View Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-53
Add Formatted Text and Examples . . . . . . . . . . . . . . . . . . . . . . . . 19-54

Share Live Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-57


Hide Code Before Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-58

Live Code File Format (.mlx) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-59


Benefits of Live Code File Format . . . . . . . . . . . . . . . . . . . . . . . . . 19-59
Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-59

Introduction to the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-60

Accelerate Exploratory Programming Using the Live Editor . . . . . 19-65

Create an Interactive Narrative Using the Live Editor . . . . . . . . . . 19-70

Create Interactive Course Materials Using the Live Editor . . . . . . 19-78

Create Runnable Examples Using the Live Editor . . . . . . . . . . . . . 19-84

Create an Interactive Form Using the Live Editor . . . . . . . . . . . . . 19-86

Create a Real-Time Dashboard Using the Live Editor . . . . . . . . . . 19-92

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-96

xvi Contents
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
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-27
Using Handles to Store Function Parameters . . . . . . . . . . . . . . . . 20-29
Visibility of Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-31

Resolve Error: Attempt to Add Variable to a Static Workspace. . . 20-33


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-33
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-33

Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-36

xvii
Function Precedence Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-37
Change in Rules For Function Precedence Order . . . . . . . . . . . . . 20-38

Update Code for R2019b Changes to Function Precedence Order


.................................................... 20-40
Identifiers cannot be used for two purposes inside a function . . . . 20-40
Identifiers without explicit declarations might not be treated as
variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-40
Variables cannot be implicitly shared between parent and nested
functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-41
Change in precedence of wildcard-based imports . . . . . . . . . . . . . 20-42
Fully qualified import functions cannot have the same name as nested
functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-42
Fully qualified imports shadow outer scope definitions of the same
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-43
Error handling when import not found . . . . . . . . . . . . . . . . . . . . . 20-43
Nested functions inherit import statements from parent functions
................................................ 20-44
Change in precedence of compound name resolution . . . . . . . . . . 20-44
Anonymous functions can include resolved and unresolved identifiers
................................................ 20-45

Indexing into Function Call Results . . . . . . . . . . . . . . . . . . . . . . . . 20-46


Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-46
Supported Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-46

Function Arguments
21
Find Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . 21-2

Support Variable Number of Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . 21-4

Support Variable Number of Outputs . . . . . . . . . . . . . . . . . . . . . . . . 21-5

Validate Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . 21-6

Checking Number of Arguments in Nested Functions . . . . . . . . . . . 21-8

Ignore Inputs in Function Definitions . . . . . . . . . . . . . . . . . . . . . . . 21-10

Check Function Inputs with validateattributes . . . . . . . . . . . . . . . 21-11

Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-13

Input Parser Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 21-17

xviii Contents
Debugging MATLAB Code
22
Debug MATLAB Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-2
Debug Using Run to Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-3
View Variable Value While Debugging . . . . . . . . . . . . . . . . . . . . . . . 22-5
Pause a Running File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-5
Step Into Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-5
Add Breakpoints and Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-6
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-7
Debug by Using Keyboard Shortcuts or Functions . . . . . . . . . . . . . . 22-8

Set Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-9


Standard Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-9
Conditional Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-10
Error Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-11
Breakpoints in Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . 22-11
Invalid Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
Disable Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12
Clear Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13

Examine Values While Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 22-14


View Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-14
View Variable Value Outside Current Workspace . . . . . . . . . . . . . . 22-15

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 Files (.m) . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-2
Add Help and Create Documentation . . . . . . . . . . . . . . . . . . . . . . . 23-4

Publishing Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-6


Markup Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-6
Sections and Section Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-8
Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9
Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-10
Text and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-10
External File Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-11
External Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-12
Image Snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-14
LaTeX Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-14
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-16
HTML Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-18
LaTeX Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-19

Output Preferences for Publishing . . . . . . . . . . . . . . . . . . . . . . . . . 23-21


How to Edit Publishing Options . . . . . . . . . . . . . . . . . . . . . . . . . . 23-21
Specify Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-22

xix
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-23
Manipulate Graphics in Publishing Output . . . . . . . . . . . . . . . . . . 23-24
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-28
Manage a Publish Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 23-29

Coding and Productivity Tips


24
Save and Back Up Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Save Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Back Up Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Recommendations on Saving Files . . . . . . . . . . . . . . . . . . . . . . . . . 24-3
File Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-3

Check Code for Errors and Warnings Using the Code Analyzer . . . 24-5
Enable Continuous Code Checking . . . . . . . . . . . . . . . . . . . . . . . . . 24-5
View Code Analyzer Status for File . . . . . . . . . . . . . . . . . . . . . . . . . 24-6
View Code Analyzer Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-7
Fix Problems in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-7
Analyze Files Using the Code Analyzer App . . . . . . . . . . . . . . . . . . 24-9
Identify and Store Issues in Files With codeIssues Object . . . . . . . 24-10
Analyze Files Using the Code Issues tool in MATLAB Online . . . . . 24-11
Adjust Code Analyzer Message Indicators and Messages . . . . . . . 24-13
Enable custom checks and configure existing checks . . . . . . . . . . 24-15
Understand Code Containing Suppressed Messages . . . . . . . . . . . 24-15
Understand the Limitations of Code Analysis . . . . . . . . . . . . . . . . 24-15
Enable MATLAB Compiler Deployment Messages . . . . . . . . . . . . . 24-17

Edit and Format Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19


Column Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Change Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Automatically Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Refactor Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-20
Indent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-20
Fold Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-21
Change the Right-Side Text Limit Indicator . . . . . . . . . . . . . . . . . . 24-22

Find and Replace Text in Files and Go to Location . . . . . . . . . . . . . 24-24


Find and Replace Any Text in Current File . . . . . . . . . . . . . . . . . . 24-24
Find and Replace Functions or Variables in Current File . . . . . . . . 24-24
Automatically Rename All Variables or Functions in a File . . . . . . 24-25
Find Text in Multiple File Names or Files . . . . . . . . . . . . . . . . . . . 24-26
Go To Location in File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-27

MATLAB Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-30


Open the Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . 24-30
Run the Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-30
Change Code Based on Code Analyzer Messages . . . . . . . . . . . . . 24-33
Other Ways to Access Code Analyzer Messages . . . . . . . . . . . . . . 24-33
Configure Code Analyzer Messages . . . . . . . . . . . . . . . . . . . . . . . 24-34

xx Contents
MATLAB Code Compatibility Analyzer . . . . . . . . . . . . . . . . . . . . . . . 24-35
Open the Code Compatibility Analyzer . . . . . . . . . . . . . . . . . . . . . 24-35
Programmatic Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-37
Unsupported Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-37

Code Generation Readiness Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-38


Issues Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-38
Files Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-39

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

Security Considerations to Protect Your Source Code . . . . . . . . . . . 25-4


Create P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-4
Build Standalone Executables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-4
Use Model Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-5
Convert Code to Native Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-5
Host Compiled Application on Remote Protected Server . . . . . . . . . 25-5
Utilize Secure OS Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-5

Create a Content-Obscured File with P-Code . . . . . . . . . . . . . . . . . . 25-7


Create P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-7
Invoke P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-7
Run Older P-Code Files on Later Versions of MATLAB . . . . . . . . . . . 25-7

Create Hyperlinks that Run Functions . . . . . . . . . . . . . . . . . . . . . . . 25-9


Run a Single Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-9
Run Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-10
Provide Command Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-10
Include Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-10

Create and Share Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-12


Create Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-12
Share Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-16

Run Parallel Language in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 25-18


Run Parallel Language in Serial . . . . . . . . . . . . . . . . . . . . . . . . . . 25-18
Use Parallel Language Without a Pool . . . . . . . . . . . . . . . . . . . . . . 25-19

Measure Code Complexity Using Cyclomatic Complexity . . . . . . . 25-21

xxi
Function Argument Validation
26
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Where to Use Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
arguments Block Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Validate Size and Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-3
Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-4
Default Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-5
Conversion to Declared Class and Size . . . . . . . . . . . . . . . . . . . . . . 26-5
Output Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-6
Kinds of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-7
Order of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-7
Restrictions on Variable and Function Access . . . . . . . . . . . . . . . . . 26-8
Debugging Arguments Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-9

Validate Required and Optional Positional Arguments . . . . . . . . . 26-11


Set Default Value for Optional Arguments . . . . . . . . . . . . . . . . . . . 26-11
Ignored Positional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-11

Validate Repeating Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-13


Avoid Using varargin for Repeating Arguments . . . . . . . . . . . . . . . 26-14

Validate Name-Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-16


Default Values for Name-Value Arguments . . . . . . . . . . . . . . . . . . 26-17
Using Repeating and Name-Value Arguments . . . . . . . . . . . . . . . . 26-17
Multiple Name-Value Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 26-18
Robust Handling of Name-Value Arguments . . . . . . . . . . . . . . . . . 26-19
Name-Value Arguments from Class Properties . . . . . . . . . . . . . . . 26-20

Use Validation Functions to Avoid Unwanted Class and Size


Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22

Use nargin Functions During Argument Validation . . . . . . . . . . . . 26-25

Argument Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-27


Numeric Value Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-27
Comparison with Other Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Membership and Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-29
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-29
Define Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-29

Transparency in MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-31


Writing Transparent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-31

xxii Contents
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-14


Issue Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-14
Throw Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-14
Add Run-Time Parameters to Your Warnings and Errors . . . . . . . . 27-15
Add Identifiers to Warnings and Errors . . . . . . . . . . . . . . . . . . . . . 27-15

Suppress Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-17


Turn Warnings On and Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-18

Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-20


Disable and Restore a Particular Warning . . . . . . . . . . . . . . . . . . . 27-20
Disable and Restore Multiple Warnings . . . . . . . . . . . . . . . . . . . . . 27-21

Change How Warnings Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-22


Enable Verbose Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-22
Display a Stack Trace on a Specific Warning . . . . . . . . . . . . . . . . . 27-22

Use try/catch to Handle Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-23

xxiii
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

Techniques to Improve Performance . . . . . . . . . . . . . . . . . . . . . . . . 29-12


Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-12
Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-12
Programming Practices for Performance . . . . . . . . . . . . . . . . . . . . 29-12
Tips on Specific MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . 29-13

Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Preallocating a Nondouble Matrix . . . . . . . . . . . . . . . . . . . . . . . . 29-14

Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-16
Using Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-16
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-17
Logical Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-18
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-19
Ordering, Setting, and Counting Operations . . . . . . . . . . . . . . . . . 29-20
Functions Commonly Used in Vectorization . . . . . . . . . . . . . . . . . 29-21

xxiv Contents
Background Processing
30
Asynchronous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-2
Asynchronous Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-2
Background Workers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-4

Run MATLAB Functions in Thread-Based Environment . . . . . . . . . 30-6


Run Functions in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Run Functions on a Thread Pool . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Automatically Scale Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Check Thread Supported Functions . . . . . . . . . . . . . . . . . . . . . . . . 30-6

Create Responsive Apps by Running Calculations in the Background


..................................................... 30-8
Open App Designer App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-8
Add a Future Array to the Properties . . . . . . . . . . . . . . . . . . . . . . . 30-8
Create y-axis Data in the Background . . . . . . . . . . . . . . . . . . . . . . . 30-9
Automatically Update Plot After Data Is Calculated in the Background
................................................. 30-9
Make Your App More Responsive by Canceling the Future Array . 30-10
Responsive App That Calculates and Plots Simple Curves . . . . . . . 30-11

Run Functions in Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-13

Update Wait Bar While Functions Run in the Background . . . . . . 30-14

Memory Usage
31
Strategies for Efficient Use of Memory . . . . . . . . . . . . . . . . . . . . . . . 31-2
Use Appropriate Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-2
Avoid Temporary Copies of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-3
Reclaim Used Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-4

Resolve “Out of Memory” Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-6


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-6
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-6

How MATLAB Allocates Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-12

Avoid Unnecessary Copies of Data . . . . . . . . . . . . . . . . . . . . . . . . . . 31-16


Passing Values to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-16
Why Pass-by-Value Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-19
Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-19

xxv
Custom Help and Documentation
32
Create Help for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Help Text from the doc Command . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Custom Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-3

Create Help Summary Files — Contents.m . . . . . . . . . . . . . . . . . . . . 32-8


What Is a Contents.m File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-8
Create a Contents.m File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-8

Customize Code Suggestions and Completions . . . . . . . . . . . . . . . 32-11


Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Signature Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Argument Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-13
Create Function Signature File . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
How Function Signature Information is Used . . . . . . . . . . . . . . . . 32-17
Multiple Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18

Display Custom Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-20


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-20
Create HTML Help Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-21
Create info.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-22
Create helptoc.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-23
Build a Search Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-25
Address Validation Errors for info.xml Files . . . . . . . . . . . . . . . . . 32-26

Display Custom Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-28


How to Display Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-28
Elements of the demos.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . 32-29

Projects
33
Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-2
What Are Projects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-2
Create Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-2
Open Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-2
Set Up Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-3
Add Files to Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-5
Other Ways to Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-6

Automate Startup and Shutdown Tasks . . . . . . . . . . . . . . . . . . . . . . . 33-8


Specify Project Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-8
Set Startup Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-8
Specify Startup and Shutdown Files . . . . . . . . . . . . . . . . . . . . . . . . 33-8

Set MATLAB Projects Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10

Determine Order for Resolving Conflicts Using Dependency Analyzer


.................................................... 33-14

xxvi Contents
Manage Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-16
Automatic Updates When Renaming, Deleting, or Removing Files
................................................ 33-17

Find Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-18


Group and Sort Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-18
Search for and Filter Project Files . . . . . . . . . . . . . . . . . . . . . . . . 33-18
Search the Content in Project Files . . . . . . . . . . . . . . . . . . . . . . . . 33-18
Find Project Files in MATLAB Online . . . . . . . . . . . . . . . . . . . . . . 33-19

Create Shortcuts to Frequent Tasks . . . . . . . . . . . . . . . . . . . . . . . . 33-21


Run Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Create Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Organize Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21

Add Labels to Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23


Add Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23
View and Edit Label Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23
Create Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-24

Create Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-25


Create a Custom Task Function . . . . . . . . . . . . . . . . . . . . . . . . . . 33-25
Run a Custom Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-25
Save Custom Task Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-26

Componentize Large Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-27


Add or Remove Reference to a Project . . . . . . . . . . . . . . . . . . . . . 33-27
View, Edit, or Run Referenced Project Files . . . . . . . . . . . . . . . . . 33-27
Extract Folder to Create a Referenced Project . . . . . . . . . . . . . . . 33-28
Manage Changes in Referenced Project Using Checkpoints . . . . . 33-28

Share Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30


Create an Export Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-33

Upgrade Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-34


Run Upgrade Project Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-34
Examine Upgrade Project Report . . . . . . . . . . . . . . . . . . . . . . . . . 33-35

Analyze Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-37


Run a Dependency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-37
Explore the Dependency Graph, Views, and Filters . . . . . . . . . . . . 33-39
Investigate and Resolve Problems . . . . . . . . . . . . . . . . . . . . . . . . . 33-45
Find Required Products and Add-Ons . . . . . . . . . . . . . . . . . . . . . . 33-48
Find File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-49
Export Dependency Analysis Results . . . . . . . . . . . . . . . . . . . . . . 33-51

Clone Remote Git Repository into New Project . . . . . . . . . . . . . . . 33-54

Use Source Control with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . 33-55


Setup Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-55
Perform Source Control Operations . . . . . . . . . . . . . . . . . . . . . . . 33-57
Work with Derived Files in Projects . . . . . . . . . . . . . . . . . . . . . . . 33-63
Find Project Files With Unsaved Changes . . . . . . . . . . . . . . . . . . . 33-64
Manage Open Files When Closing a Project . . . . . . . . . . . . . . . . . 33-64

xxvii
Create and Edit Projects Programmatically . . . . . . . . . . . . . . . . . . 33-65

Explore an Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-72

Source Control Interface


34
Source Control Integration in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 34-2
Classic and Distributed Source Control . . . . . . . . . . . . . . . . . . . . . . 34-2

Set Source Control Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-4


Set Git Preferences in MATLAB Online . . . . . . . . . . . . . . . . . . . . . . 34-4

Use Git in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-5


Clone Remote Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-5
Mark Files for Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-6
Review Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-6
Commit Modified Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-7
Push Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-8
Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-9
Manage Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-10
Create Local Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-10
Store Uncommitted Changes Using Git Stashes . . . . . . . . . . . . . . 34-11

Set Up Git Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-13


Register Binary Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-13
Configure MATLAB for Git on Windows . . . . . . . . . . . . . . . . . . . . . 34-13
Configure MATLAB to Use Git SSH Authentication . . . . . . . . . . . . 34-14
Install Git Credential Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-18
Disable Compression for Simulink Models . . . . . . . . . . . . . . . . . . 34-18
Configure Git to Use Git LFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-18
Configure Git to use MATLAB for Diff and Merge . . . . . . . . . . . . . 34-18
Install Command-Line Git Client . . . . . . . . . . . . . . . . . . . . . . . . . . 34-19

Branch and Merge with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-20


Create Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-20
Switch Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-21
Compare Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22
Merge Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22
Revert to Head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22
Delete Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-23

Add Git Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-24


Update Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-24
Use Fetch and Merge with Submodules . . . . . . . . . . . . . . . . . . . . 34-24
Use Push to Send Changes to the Submodule Repository . . . . . . . 34-24

Review Changes in SVN Source Control . . . . . . . . . . . . . . . . . . . . . 34-26

Mark Files for Addition to SVN Source Control . . . . . . . . . . . . . . . 34-27

Move, Rename, or Delete Files Under SVN Source Control . . . . . . 34-28

xxviii Contents
Resolve SVN Source Control Conflicts . . . . . . . . . . . . . . . . . . . . . . . 34-29
Examining and Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . 34-29
Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-29
Merge Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-29
Extract Conflict Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-30

Commit Modified Files to SVN Source Control . . . . . . . . . . . . . . . . 34-32

Revert Changes in SVN Source Control . . . . . . . . . . . . . . . . . . . . . . 34-33


Revert Local Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-33
Revert a File to a Specified Revision . . . . . . . . . . . . . . . . . . . . . . . 34-33

Set Up SVN Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34


SVN Source Control Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34
Register Binary Files with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-34
Standard Repository Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-37
Tag Versions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-37
Enforce Locking Files Before Editing . . . . . . . . . . . . . . . . . . . . . . 34-37
Share a Subversion Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-38

Check Out from SVN Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-39


Retrieve Tagged Version of Repository . . . . . . . . . . . . . . . . . . . . . 34-39

Update SVN File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . 34-41


Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-41
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-41

Get SVN File Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-42


Manage SVN Repository Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-42

Continuous Integration Using MATLAB Projects and Jenkins . . . 34-43


Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-43
Set Up MATLAB Project for Continuous Integration in Jenkins . . . 34-43
Reduce Test Runtime Using Dependency Cache and Impact Analysis
................................................ 34-44
Enhance Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-45

Customize External Source Control to Use MATLAB for Diff and


Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-47
Finding the Full Paths for MATLAB Diff, Merge, and AutoMerge . . 34-47
Integration with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-48
Integration with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-49
Integration with Other Source Control Tools . . . . . . . . . . . . . . . . . 34-50

MSSCCI Source Control Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 34-52

Set Up MSSCCI Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-53


Create Projects in Source Control System . . . . . . . . . . . . . . . . . . . 34-53
Specify Source Control System with MATLAB Software . . . . . . . . 34-54
Register Source Control Project with MATLAB Software . . . . . . . . 34-55
Add Files to Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-57

Check Files In and Out from MSSCCI Source Control . . . . . . . . . . 34-58


Check Files Into Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 34-58
Check Files Out of Source Control . . . . . . . . . . . . . . . . . . . . . . . . 34-58

xxix
Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-59

Additional MSSCCI Source Control Actions . . . . . . . . . . . . . . . . . . 34-60


Getting the Latest Version of Files for Viewing or Compiling . . . . . 34-60
Removing Files from the Source Control System . . . . . . . . . . . . . . 34-61
Showing File History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-61
Comparing the Working Copy of a File to the Latest Version in Source
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-62
Viewing Source Control Properties of a File . . . . . . . . . . . . . . . . . 34-63
Starting the Source Control System . . . . . . . . . . . . . . . . . . . . . . . 34-64

Access MSSCCI Source Control from Editors . . . . . . . . . . . . . . . . . 34-66

Troubleshoot MSSCCI Source Control Problems . . . . . . . . . . . . . . 34-67


Source Control Error: Provider Not Present or Not Installed Properly
................................................ 34-67
Restriction Against @ Character . . . . . . . . . . . . . . . . . . . . . . . . . . 34-68
Add to Source Control Is the Only Action Available . . . . . . . . . . . . 34-68
More Solutions for Source Control Problems . . . . . . . . . . . . . . . . 34-68

Unit Testing
35
Write Test Using Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-3

Write Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-6

Write Script-Based Test Using Local Functions . . . . . . . . . . . . . . . 35-11

Extend Script-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-14


Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-14
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-14
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 35-15
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-15

Run Tests in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-17

Run Tests Using Test Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-20


Create Test Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-20
Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-22
Debug Test Failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24
Customize Test Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24
Generate Code Coverage Report . . . . . . . . . . . . . . . . . . . . . . . . . . 35-26

Write Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28


Create Test Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Run the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-30
Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-31

Write Simple Test Case Using Functions . . . . . . . . . . . . . . . . . . . . . 35-32

Write Test Using Setup and Teardown Functions . . . . . . . . . . . . . . 35-35

xxx Contents
Extend Function-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-40
Fixtures for Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . 35-40
Test Logging and Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41
Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41
Test Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-42
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 35-42
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43

Author Class-Based Unit Tests in MATLAB . . . . . . . . . . . . . . . . . . . 35-44


The Test Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-44
The Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-44
Additional Features for Advanced Test Classes . . . . . . . . . . . . . . . 35-45

Write Simple Test Case Using Classes . . . . . . . . . . . . . . . . . . . . . . . 35-47

Write Setup and Teardown Code Using Classes . . . . . . . . . . . . . . . 35-50


Test Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
Test Case with Method-Level Setup Code . . . . . . . . . . . . . . . . . . . 35-50
Test Case with Class-Level Setup Code . . . . . . . . . . . . . . . . . . . . . 35-51

Table of Verifications, Assertions, and Other Qualifications . . . . . 35-53

Tag Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-56


Tag Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-56
Select and Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-57

Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . 35-60

Create Basic Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-63

Create Advanced Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-65

Use Parameters in Class-Based Tests . . . . . . . . . . . . . . . . . . . . . . . 35-70


How to Write Parameterized Tests . . . . . . . . . . . . . . . . . . . . . . . . 35-70
How to Initialize Parameterization Properties . . . . . . . . . . . . . . . . 35-71
Specify Parameterization Level . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-72
Specify How Parameters Are Combined . . . . . . . . . . . . . . . . . . . . 35-73
Use External Parameters in Tests . . . . . . . . . . . . . . . . . . . . . . . . . 35-75

Create Basic Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-76

Create Advanced Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . 35-81

Use External Parameters in Parameterized Test . . . . . . . . . . . . . . . 35-88

Define Parameters at Suite Creation Time . . . . . . . . . . . . . . . . . . . 35-92

Create Simple Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-99

Run Tests for Various Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . 35-101


Set Up Example Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-101
Run All Tests in Class or Function . . . . . . . . . . . . . . . . . . . . . . . . 35-101
Run Single Test in Class or Function . . . . . . . . . . . . . . . . . . . . . . 35-101
Run Test Suites by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-102

xxxi
Run Test Suites from Test Array . . . . . . . . . . . . . . . . . . . . . . . . . 35-102
Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . 35-103

Programmatically Access Test Diagnostics . . . . . . . . . . . . . . . . . . 35-104

Add Plugin to Test Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-105

Write Plugins to Extend TestRunner . . . . . . . . . . . . . . . . . . . . . . . 35-108


Custom Plugins Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-108
Extending Test Session Level Plugin Methods . . . . . . . . . . . . . . . 35-108
Extending Test Suite Level Plugin Methods . . . . . . . . . . . . . . . . 35-109
Extending Test Class Level Plugin Methods . . . . . . . . . . . . . . . . 35-109
Extending Test Level Plugin Methods . . . . . . . . . . . . . . . . . . . . . 35-110

Create Custom Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-111

Run Tests in Parallel with Custom Plugin . . . . . . . . . . . . . . . . . . . 35-116

Write Plugin to Add Data to Test Results . . . . . . . . . . . . . . . . . . . 35-124

Write Plugin to Save Diagnostic Details . . . . . . . . . . . . . . . . . . . . 35-129

Plugin to Generate Custom Test Output Format . . . . . . . . . . . . . . 35-133

Analyze Test Case Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-136

Analyze Failed Test Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-139

Rerun Failed Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-141

Dynamically Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-144


Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-144
Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . 35-146
Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . 35-147

Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-150

Create Custom Boolean Constraint . . . . . . . . . . . . . . . . . . . . . . . . 35-153

Overview of App Testing Framework . . . . . . . . . . . . . . . . . . . . . . . 35-157


App Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-157
Gesture Support of UI Components . . . . . . . . . . . . . . . . . . . . . . 35-157
Example: Write a Test for an App . . . . . . . . . . . . . . . . . . . . . . . . 35-159

Write Tests for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-162

Write Tests That Use App Testing and Mocking Frameworks . . . 35-166
Create App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-166
Test App With Manual Intervention . . . . . . . . . . . . . . . . . . . . . . . 35-167
Create Fully Automated Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-168

Overview of Performance Testing Framework . . . . . . . . . . . . . . . . 35-171


Determine Bounds of Measured Code . . . . . . . . . . . . . . . . . . . . . 35-171
Types of Time Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-172

xxxii Contents
Write Performance Tests with Measurement Boundaries . . . . . . . 35-172
Run Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-173
Understand Invalid Test Results . . . . . . . . . . . . . . . . . . . . . . . . . 35-173

Test Performance Using Scripts or Functions . . . . . . . . . . . . . . . . 35-175

Test Performance Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 35-179

Measure Fast Executing Test Code . . . . . . . . . . . . . . . . . . . . . . . . 35-184

Create Mock Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-187

Specify Mock Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-194


Define Mock Method Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 35-194
Define Mock Property Behavior . . . . . . . . . . . . . . . . . . . . . . . . . 35-195
Define Repeating and Subsequent Behavior . . . . . . . . . . . . . . . . 35-196
Summary of Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-198

Qualify Mock Object Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . 35-199


Qualify Mock Method Interaction . . . . . . . . . . . . . . . . . . . . . . . . 35-199
Qualify Mock Property Interaction . . . . . . . . . . . . . . . . . . . . . . . 35-200
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-201
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-203

Ways to Write Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-205


Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-205
Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-206
Class-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-206
Extend Unit Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . 35-207

Compile MATLAB Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-208


Run Tests with Standalone Applications . . . . . . . . . . . . . . . . . . . 35-208
Run Tests in Parallel with Standalone Applications . . . . . . . . . . . 35-209
TestRand Class Definition Summary . . . . . . . . . . . . . . . . . . . . . . 35-209

Types of Code Coverage for MATLAB Source Code . . . . . . . . . . . . 35-211


Statement Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-211
Function Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-211

Collect Statement and Function Coverage Metrics for MATLAB


Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-213

Insert Test Code Using Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-218


Create Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-218
Add Parameters and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-218
Run Tests in Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-221

Develop and Integrate Software with Continuous Integration . . 35-222


Continuous Integration Workflow . . . . . . . . . . . . . . . . . . . . . . . . 35-222
Continuous Integration with MathWorks Products . . . . . . . . . . . 35-224

Generate Artifacts Using MATLAB Unit Test Plugins . . . . . . . . . . 35-226

Continuous Integration with MATLAB on CI Platforms . . . . . . . . 35-230


Azure DevOps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-230

xxxiii
Bamboo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-230
CircleCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-230
GitHub Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-230
GitLab CI/CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-231
Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-231
Travis CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-231
Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-231

Build Automation
36
Overview of MATLAB Build Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-2
Create Plan with Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-2
Run Tasks in Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-3

Create and Run Tasks Using Build Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-4


Create Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-4
Summary of Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-5
Visualize Task Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-6
Run Tasks in Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-6

Create and Run Tasks That Accept Arguments . . . . . . . . . . . . . . . . . . . . . 36-8


Create Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-8
Summary of Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-9
Run Tasks with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-10

Improve Performance with Incremental Builds . . . . . . . . . . . . . . . . . . . 36-12


MATLAB Incremental Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-12
Create and Run Tasks That Support Incremental Builds . . . . . . . . . . . . 36-12

System object Usage and Authoring


37
What Are System Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-2
Running a System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-3
System Object Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-3

System Objects vs MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-5


System Objects vs. MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 37-5
Process Audio Data Using Only MATLAB Functions Code . . . . . . . . . . . . 37-5
Process Audio Data Using System Objects . . . . . . . . . . . . . . . . . . . . . . . 37-6

System Design in MATLAB Using System Objects . . . . . . . . . . . . . . . . . . 37-7


System Design and Simulation in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 37-7
Create Individual Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-7
Configure Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-8
Create and Configure Components at the Same Time . . . . . . . . . . . . . . . 37-8
Assemble Components Into System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-9
Run Your System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-9

xxxiv Contents
Reconfiguring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-10

Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-11


Create System Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-11
Define Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-11

Change the Number of Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-13

Validate Property and Input Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-16


Validate a Single Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-16
Validate Interdependent Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-16
Validate Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-16
Complete Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-16

Initialize Properties and Setup One-Time Calculations . . . . . . . . . . . . . 37-18

Set Property Values at Construction Time . . . . . . . . . . . . . . . . . . . . . . . 37-20

Reset Algorithm and Release Resources . . . . . . . . . . . . . . . . . . . . . . . . . 37-22


Reset Algorithm State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-22
Release System Object Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-22

Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-24


Specify Property as Nontunable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-24
Specify Property as DiscreteState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-24
Example Class with Various Property Attributes . . . . . . . . . . . . . . . . . . 37-24

Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-26


Specify Inactive Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-26
Complete Class Definition File with Inactive Properties Method . . . . . . 37-26

Limit Property Values to Finite List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-28


Property Validation with mustBeMember . . . . . . . . . . . . . . . . . . . . . . . 37-28
Enumeration Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-28
Create a Whiteboard System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-29

Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-32

Define Composite System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-34

Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-36


Use the FiniteSource Class and Specify End of the Source . . . . . . . . . . 37-36
Complete Class Definition File with Finite Source . . . . . . . . . . . . . . . . . 37-36

Save and Load System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-38


Save System Object and Child Object . . . . . . . . . . . . . . . . . . . . . . . . . . 37-38
Load System Object and Child Object . . . . . . . . . . . . . . . . . . . . . . . . . . 37-38
Complete Class Definition Files with Save and Load . . . . . . . . . . . . . . . 37-38

Define System Object Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-41

Handle Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43


React to Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43
Restrict Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43

xxxv
Summary of Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-45
Setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-45
Running the Object or Step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 37-45
Reset Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-46
Release Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-47

Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-48


setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-48
Running the Object or step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 37-48
reset Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-49
release Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-49

Tips for Defining System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-50


General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-50
Inputs and Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-50
Using ~ as an Input Argument in Method Definitions . . . . . . . . . . . . . . 37-50
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-50
Text Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-51
Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-51
Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-52

Insert System Object Code Using MATLAB Editor . . . . . . . . . . . . . . . . . 37-53


Define System Objects with Code Insertion . . . . . . . . . . . . . . . . . . . . . . 37-53
Create a Temperature Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-55
Create Custom Property for Freezing Point . . . . . . . . . . . . . . . . . . . . . . 37-56
Add Method to Validate Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-57

Analyze System Object Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-58

Use Global Variables in System Objects . . . . . . . . . . . . . . . . . . . . . . . . . 37-61


System Object Global Variables in MATLAB . . . . . . . . . . . . . . . . . . . . . 37-61
System Object Global Variables in Simulink . . . . . . . . . . . . . . . . . . . . . 37-61

Create Moving Average System Object . . . . . . . . . . . . . . . . . . . . . . . . . . 37-65

Create New System Objects for File Input and Output . . . . . . . . . . . . . 37-70

Create Composite System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-76

xxxvi Contents
Language

37
1

Syntax Basics

• “Continue Long Statements on Multiple Lines” on page 1-2


• “Name=Value in Function Calls” on page 1-3
• “Ignore Function Outputs” on page 1-4
• “Variable Names” on page 1-5
• “Case and Space Sensitivity” on page 1-7
• “Choose Command Syntax or Function Syntax” on page 1-8
• “Resolve Error: Undefined Function or Variable” on page 1-11
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
Name=Value in Function Calls

Name=Value in Function Calls


Since R2021a

MATLAB supports two syntaxes for passing name-value arguments.

plot(x,y,LineWidth=2) name=value syntax

plot(x,y,"LineWidth",2) comma-separated syntax

Use the name=value syntax to help identify name-value arguments for functions and to clearly
distinguish names from values in lists of name-value arguments.

Most functions and methods support both syntaxes, but there are some limitations on where and how
the name=value syntax can be used:

Mixing name,value and name=value syntaxes: The recommended practice is to use only one
syntax in any given function call. However, if you do mix name=value and name,value syntaxes
in a single call, all name=value arguments must appear after the name,value arguments. For
example, plot(x,y,"Color","red",LineWidth=2) is a valid combination, but
plot(x,y,Color="red","LineWidth",2) errors.
• Using positional arguments after name-value arguments: Some functions have positional
arguments that appear after name-value arguments. For example, this call to the verifyEqual
method uses the RelTol name-value argument, followed by a string input:

verifyEqual(testCase,1.5,2,"RelTol",0.1,...
"Difference exceeds relative tolerance.")

Using the name=value syntax (RelTol=0.1) causes the statement to error. In cases where a
positional argument follows name-value arguments, use the name,value syntax.
• Names that are invalid variable names: Name-value arguments with names that are invalid
MATLAB variable names cannot be used with the name=value syntax. See “Variable Names” on
page 1-5 for more info. For example, a name-value argument like "allow-empty",true errors
if passed as allow-empty=true. Use the name,value syntax in these cases.

Function authors do not need to code differently to support both the name,value and name=value
syntaxes. For information on using argument validation with name-value arguments, see “Validate
Name-Value Arguments” on page 26-16.

1-3
1 Syntax Basics

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 Inputs in Function Definitions” on page 21-10

1-4
Variable Names

Variable Names

In this section...
“Valid Names” on page 1-5
“Conflicts with Function Names” on page 1-5

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: Examples of 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-91

1-5
1 Syntax Basics

See Also
clear | exist | iskeyword | namelengthmax | which | isvarname

1-6
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-7
1 Syntax Basics

Choose Command Syntax or Function Syntax

MATLAB has two ways of calling functions, called function syntax and command syntax. This page
discusses the differences between these syntax formats and how to avoid common mistakes
associated with command syntax.

For introductory information on calling functions, see “Calling Functions”. For information related to
defining functions, see “Create Functions in Files” on page 20-2.

Command Syntax and Function Syntax


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)

In function syntax, inputs can be data, variables, and even MATLAB expressions. If an input is data,
such as the numeric value 2 or the string array ["a" "b" "c"], MATLAB passes it to the function
as-is. If an input is a variable MATLAB will pass the value assigned to it. If an input is an expression,
like 2+2 or sin(2*pi), MATLAB evaluates it first, and passes the result to the function. If the
functions has outputs, you can assign them to variables as shown in the example syntax above.

Command syntax is simpler but more limited. To use it, separate inputs with spaces rather than
commas, and do not enclose them in parentheses.

functionName input1 ... inputN

With command syntax, MATLAB passes all inputs as character vectors (that is, as if they were
enclosed in single quotation marks) and does not assign outputs to user defined variables. If the
function returns an output, it is assigned to the ans variable. To pass a data type other than a
character vector, use the function syntax. To pass a value that contains a space, you have two options.
One is to use function syntax. The other is to put single quotes around the value. Otherwise, MATLAB
treats the space as splitting your value into multiple inputs.

If a value is assigned to 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

1-8
Choose Command Syntax or Function Syntax

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;

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 tempdir cd('tempdir') cd(tempdir)
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')

or

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

1-9
1 Syntax Basics

ls ./d

This could be a call to the ls function with './d' as its argument. It also could represent element-
wise division on the array ls, using the variable d as the divisor.

If you issue this statement at the command line, MATLAB uses syntactic rules, 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.
When you are using those components, 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:

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, MATLAB treats the potentially ambiguous statement ls ./d as a call to the ls function
using command syntax.

The best practices is to avoid defining variable names that conflict with common functions to prevent
ambiguity and have consistent whitespace around operators or to call functions with explicit
parentheses..

See Also
“Calling Functions” | “Create Functions in Files” on page 20-2

1-10
Resolve Error: Undefined Function or Variable

Resolve Error: Undefined Function or Variable

Issue
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.

Possible Solutions
Verify Spelling of Function or Variable Name

One of the most common causes is misspelling the function or variable name. Especially with longer
names or names containing similar characters (such as the letter l and numeral one), it is easy to
make mistakes and hard to detect them.

Often, when 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:

accumArray

Undefined function or variable 'accumArray'.

Did you mean:


>> accumarray

When this happens, press Enter to execute the suggested command or Esc to dismiss it.

Verify Inputs Correspond to the Function Syntax

Object methods are typically called using function syntax: for instance method(object,inputs).
Alternatively, they can be called using dot notation: for instance object.method(inputs). One
common error is to mix these syntaxes. For instance, you might call the method using function syntax,
but to provide inputs following dot notation syntax and leave out the object as an input: for instance,
method(inputs). To avoid this, when calling an object method, make sure you specify the object
first, either through the first input of function syntax or through the first identifier of dot notation.

Make Sure Function Name Matches File Name

When you write a function, you establish its name 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

1-11
1 Syntax Basics

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.

To Locate the file that defines 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

Make Sure Necessary Toolbox Is Installed and Correct Version

If you are unable to use a built-in function from MATLAB or its toolboxes, make sure that the function
is installed and is the correct version.

If you do not know which toolbox contains 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. Alternatively, for steps to identify toolboxes that a function depends on, see
“Identify Program Dependencies” on page 25-2.

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. If you cannot, you need to install it in order to use it.
For help with installing MathWorks products, see “Install License Manager on License Server”.

1-12
Random documents with unrelated
content Scribd suggests to you:
*** END OF THE PROJECT GUTENBERG EBOOK A MATTER OF
ORDER ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like