0% found this document useful (0 votes)
73 views1,712 pages

Matlab Prog

Uploaded by

RAJESH
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
0% found this document useful (0 votes)
73 views1,712 pages

Matlab Prog

Uploaded by

RAJESH
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/ 1712

MATLAB®

Programming Fundamentals

R2025a
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–2025 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)
September 2023 Online only Revised for Version 23.2 (R2023b)
March 2024 Online only Revised for Version 24.1 (R2024a)
September 2024 Online only Revised for Version 24.2 (R2024b)
March 2025 Online only Revised for Version 25.1 (R2025a)
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: Unrecognized 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-4

Array vs. Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9

v
Compatible Array Sizes for Basic Operations . . . . . . . . . . . . . . . . . . 2-12
Inputs with Compatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
Inputs with Incompatible Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14

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


Array Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Logic Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18

Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-19


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

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

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

Bit-Wise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25

Perform Cyclic Redundancy Check . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31

Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-34

Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-36

Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-38


What Is a Regular Expression? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-38
Steps for Building Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-39
Operators and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-42

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


Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-50
Overlapping Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-50
Logical AND Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-51

Tokens in Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-53


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-53
Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-55
Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-56
Tokens in Replacement Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-56
Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-57

Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-59


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-59
Dynamic Match Expressions — (??expr) . . . . . . . . . . . . . . . . . . . . . 2-60
Commands That Modify the Match Expression — (??@cmd) . . . . . . 2-60
Commands That Serve a Functional Purpose — (?@cmd) . . . . . . . . 2-61
Commands in Replacement Expressions — ${cmd} . . . . . . . . . . . . 2-63

Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66


What Is a Comma-Separated List? . . . . . . . . . . . . . . . . . . . . . . . . . 2-66
Generating a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . . 2-66
Assigning Output from a Comma-Separated List . . . . . . . . . . . . . . . 2-68
Assigning to a Comma-Separated List . . . . . . . . . . . . . . . . . . . . . . . 2-68
How to Use Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . 2-69

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

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


Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-77
Variables with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . 2-77
Files with Sequential Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-78
Function Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-78
Field Names in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-79
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-79

Classes (Data Types)

Overview of MATLAB Classes


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

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

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


Floating-Point Numbers in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Largest and Smallest Values for Floating-Point Data Types . . . . . . . . 4-8
Accuracy of Floating-Point Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Arithmetic Operations on Floating-Point Numbers . . . . . . . . . . . . . 4-11
Unexpected Results with Floating-Point Arithmetic . . . . . . . . . . . . . 4-12

Create Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14

Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15


Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15

Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17

Display Format for Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18

vii
Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-20

Single Precision Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26

The Logical Class


5
Find Array Elements That Meet Conditions . . . . . . . . . . . . . . . . . . . . 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

Frequently Asked Questions About String Arrays . . . . . . . . . . . . . . . 6-58


Why Does Using Command Form With Strings Return An Error? . . 6-58

viii Contents
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-12

Set Date and Time Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15


Formats for Individual Date and Duration Arrays . . . . . . . . . . . . . . 7-15
datetime Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15
duration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
calendarDuration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . 7-17
Default datetime Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-17

Generate Sequence of Dates and Time . . . . . . . . . . . . . . . . . . . . . . . 7-19


Sequence of Datetime or Duration Values Between Endpoints with
Step Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-19
Add Duration or Calendar Duration to Create Sequence of Dates . . 7-20
Specify Length and Endpoints of Date or Duration Sequence . . . . . 7-21
Sequence of Datetime Values Using Calendar Rules . . . . . . . . . . . . 7-22

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


Write Locale-Independent Date and Time Code . . . . . . . . . . . . . . . . 7-24
Write Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-25
Read Dates in Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-25

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

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

Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-32

Compare Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-37

ix
Plot Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-44

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

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

Replace Discouraged Instances of Serial Date Numbers and Date


Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-63
Convert Serial Date Numbers and Date Strings . . . . . . . . . . . . . . . 7-63
Replace Functions That Use Date Numbers . . . . . . . . . . . . . . . . . . 7-63
Discouraged Syntaxes for Date and Time Components . . . . . . . . . . 7-67
Guidelines for Updating Your Own Functions . . . . . . . . . . . . . . . . . 7-68

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

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

Categorical Arrays
8
Create Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2

Convert Text in Table Variables to Categorical . . . . . . . . . . . . . . . . . . 8-8

Plot Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12

Compare Categorical Array Elements . . . . . . . . . . . . . . . . . . . . . . . . 8-18

Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-21

Produce All Combinations of Categories from Two Categorical Arrays


..................................................... 8-27

Access Data Using Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . 8-32


Select Data By Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-32
Common Ways to Access Data Using Categorical Arrays . . . . . . . . . 8-32

Work with Protected Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . 8-41

Advantages of Using Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . 8-45


Natural Representation of Categorical Data . . . . . . . . . . . . . . . . . . 8-45
Mathematical Ordering for Categories . . . . . . . . . . . . . . . . . . . . . . 8-45
Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-45

Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-47


Order of Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-47
How to Create Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . 8-47
Working with Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . 8-49

Core Functions Supporting Categorical Arrays . . . . . . . . . . . . . . . . 8-50

x Contents
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-32

Access Data in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-37


Table Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-37
Recommended Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . 9-38
Index by Specifying Rows and Variables . . . . . . . . . . . . . . . . . . . . . 9-39
Assign Values to Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-43
Find Table Rows Where Values Meet Conditions . . . . . . . . . . . . . . . 9-46
Summary of Table Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . 9-48

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
Rules for Operations and Functions on Event Tables . . . . . . . . . . . . 9-64

Calculations When Tables Have Both Numeric and Nonnumeric Data


..................................................... 9-66

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

Tables of Mixed Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-86

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

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

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

Timetables
10
Create Timetables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2

Resample and Aggregate Data in Timetable . . . . . . . . . . . . . . . . . . 10-10

xi
Combine Timetables and Synchronize Their Data . . . . . . . . . . . . . 10-13

Retime and Synchronize Timetable Variables Using Different


Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19

Select Times in Timetable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24

Clean Timetable with Missing, Duplicate, or Nonuniform Times


.................................................... 10-31

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

Loma Prieta Earthquake Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 10-46

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

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

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

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

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

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

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

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


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

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

Cell Arrays
12
Create Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2

Access Data in Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-4

xii Contents
Add or Delete Cells in Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-9

Preallocate Memory for Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11

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

Compare Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-9

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

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

Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6

Concatenation Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-7


Combining Single and Double Types . . . . . . . . . . . . . . . . . . . . . . . . 15-7

xiii
Combining Integer and Double Types . . . . . . . . . . . . . . . . . . . . . . . 15-7
Combining Character and Double Types . . . . . . . . . . . . . . . . . . . . . 15-7
Combining Logical and Double Types . . . . . . . . . . . . . . . . . . . . . . . 15-7

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

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


Add Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-3
Comment Out Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-3
Wrap Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-4
Checking Spelling in Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-4

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


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

Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-11

Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-13


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

xiv Contents
Access Help for Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-14

Live Scripts and Functions


19
What Is a Live Script or Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2
Differences Between Scripts and Live Scripts . . . . . . . . . . . . . . . . . 19-2
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-3

Create Live Scripts in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . 19-4


Create Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-4
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-4
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-5
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-5
Change View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-6
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-8
Save Live Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-9

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


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

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


Insert Text Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-17
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-19
Checking Spelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-20
Change Fonts and Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-20
Autoformatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-22

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


Insert Equation Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-25
Insert LaTeX Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-27
Supported LaTeX Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-28

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


Insert Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-35
Modify Control Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-38
Link Variables to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-38
Specify Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-40
Modify Control Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-40
Create Live Script with Multiple Interactive Controls . . . . . . . . . . 19-41
Share Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-44

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


What Are Live Editor Tasks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-46
Insert Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-46
Run Tasks and Surrounding Code . . . . . . . . . . . . . . . . . . . . . . . . . 19-49
Modify Output Argument Name . . . . . . . . . . . . . . . . . . . . . . . . . . 19-50
View and Edit Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-50
Custom Live Editor Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-51

xv
Create Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-52
Create Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-52
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-52
Add Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-53
Run Live Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-53
Save Live Functions as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . 19-54

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


Add Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-55
View Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-56
Add Formatted Text and Examples . . . . . . . . . . . . . . . . . . . . . . . . 19-57

Ways to Share and Export Live Scripts and Functions . . . . . . . . . . 19-60


Hide Code Before Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-61

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


Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-63

Live Code File Format (.m) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-64


Benefits to Plain Text Live Code File Format (.m) . . . . . . . . . . . . . 19-64
Save Live Scripts as Plain Text . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-64
Structure of Plain Text Live Code File . . . . . . . . . . . . . . . . . . . . . . 19-65
Markup Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-67
Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-69

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

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

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

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

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

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

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

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-104

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

xvi Contents
Configure the Run Button for Functions . . . . . . . . . . . . . . . . . . . . . . 20-7

Base and Function Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-9


What Is the Base Workspace? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-9
Function Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-9
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10

Share Data Between Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Best Practice: Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-12
Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-13
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-14
Evaluating in Another Workspace . . . . . . . . . . . . . . . . . . . . . . . . . 20-14
Store Variables in Workspace Object . . . . . . . . . . . . . . . . . . . . . . . 20-15

Check Variable Scope in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-16


Use Automatic Function and Variable Highlighting . . . . . . . . . . . . 20-16
Example of Using Automatic Function and Variable Highlighting . 20-16

Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-19


Local and Nested Functions in a File . . . . . . . . . . . . . . . . . . . . . . 20-19
Private Functions in a Subfolder . . . . . . . . . . . . . . . . . . . . . . . . . . 20-20
Anonymous Functions Without a File . . . . . . . . . . . . . . . . . . . . . . 20-20

Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-22


What Are Anonymous Functions? . . . . . . . . . . . . . . . . . . . . . . . . . 20-22
Variables in the Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-23
Multiple Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-23
Functions with No Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-24
Functions with Multiple Inputs or Outputs . . . . . . . . . . . . . . . . . . 20-24
Arrays of Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 20-25

Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-27

Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-29


What Are Nested Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-29
Requirements for Nested Functions . . . . . . . . . . . . . . . . . . . . . . . 20-29
Sharing Variables Between Parent and Nested Functions . . . . . . . 20-29
Using Handles to Store Function Parameters . . . . . . . . . . . . . . . . 20-31
Visibility of Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-33

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


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-35
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-35

Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-38

Function Precedence Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-39


Change in Rules For Function Precedence Order . . . . . . . . . . . . . 20-40

Update Code for R2019b Changes to Function Precedence Order


.................................................... 20-42
Identifiers cannot be used for two purposes inside a function . . . . 20-42

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

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


Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-48
Supported Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-48

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

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

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

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

Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-14

Input Parser Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 21-18

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

xviii Contents
Step Into Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-6
Add Breakpoints and Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-7
Manage Breakpoints in Debugger Panel . . . . . . . . . . . . . . . . . . . . . 22-8
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-9
Debug Using Keyboard Shortcuts or Functions . . . . . . . . . . . . . . . . 22-9

Set Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-11


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

Examine Values While Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 22-17


View Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-17
View Variable Value Outside Current Workspace . . . . . . . . . . . . . . 22-18

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
Show Files as Full-Screen Presentation . . . . . . . . . . . . . . . . . . . . . . 23-4
Add Help and Create Documentation . . . . . . . . . . . . . . . . . . . . . . . 23-4

Publishing Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-5


Markup Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-5
Sections and Section Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-7
Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-8
Bulleted and Numbered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9
Text and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9
External File Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-10
External Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-11
Image Snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-13
LaTeX Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-13
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-15
HTML Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-17
LaTeX Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-18

Output Settings for Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-20


How to Edit Publishing Options . . . . . . . . . . . . . . . . . . . . . . . . . . 23-20
Specify Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-20
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-21
Manipulate Graphics in Publishing Output . . . . . . . . . . . . . . . . . . 23-23
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-26
Manage a Publish Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 23-27

xix
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-4
Enable Continuous Code Checking . . . . . . . . . . . . . . . . . . . . . . . . . 24-4
View Code Analyzer Status for File . . . . . . . . . . . . . . . . . . . . . . . . . 24-5
View Code Analyzer Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-6
Fix Problems in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-7
Analyze Files Using the Code Issues Panel . . . . . . . . . . . . . . . . . . . 24-9
Analyze Files Using the Code Analyzer App . . . . . . . . . . . . . . . . . 24-11
Identify and Store Issues in Files With codeIssues Object . . . . . . . 24-11
Adjust Code Analyzer Message Indicators and Messages . . . . . . . 24-12
Enable custom checks and configure existing checks . . . . . . . . . . 24-14
Understand Code Containing Suppressed Messages . . . . . . . . . . . 24-14
Understand the Limitations of Code Analysis . . . . . . . . . . . . . . . . 24-15
Enable MATLAB Compiler Deployment Messages . . . . . . . . . . . . . 24-17

Edit and Format Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-18


Column Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-18
Change Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-18
Duplicate and Copy Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-18
Automatically Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Refactor Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Indent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-19
Fold Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-20
Change the Right-Side Text Limit Indicator . . . . . . . . . . . . . . . . . . 24-21
View Outline of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-21

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


Find and Replace Any Text in Current File . . . . . . . . . . . . . . . . . . 24-23
Find and Replace Functions or Variables in Current File . . . . . . . . 24-25
Automatically Rename All Variables or Functions in a File . . . . . . 24-26
Find Text in Multiple Filenames or Files . . . . . . . . . . . . . . . . . . . . 24-27
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

MATLAB Code Compatibility Analyzer . . . . . . . . . . . . . . . . . . . . . . . 24-35


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

xx Contents
Code Generation Readiness Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-38
Run the Code Generation Readiness Tool . . . . . . . . . . . . . . . . . . . 24-38
Issues Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-39
Files Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-40
Limitations of the Code Generation Readiness Tool . . . . . . . . . . . . 24-40

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-6

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


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

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
Upgrade Toolbox Created in Previous Release . . . . . . . . . . . . . . . 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
Membership and Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-28
Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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-4
Specifying the Value of Callback Function Properties . . . . . . . . . . . 28-5

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-15


Passing Values to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-15
Why Pass-by-Value Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-18
Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-18

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-18
Multiple Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18

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


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-20
Create HTML Help Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-21
Create info.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-21
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 from Existing Folder . . . . . . . . . . . . . . . . . . . . . . . . 33-2
Set Up Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-4
Add Files to Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-7
Open Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-8
Other Ways to Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-9

Manage Project Settings, Path, Labels, and Startup and Shutdown


Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-12
Specify Project Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-12
Specify Project Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-12
Automate Startup and Shutdown Tasks . . . . . . . . . . . . . . . . . . . . . 33-12
Specify Startup Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Create and Manage Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Create and Manage Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . 33-14
Specify Folders for Derived Files . . . . . . . . . . . . . . . . . . . . . . . . . 33-15

xxvi Contents
Configure Global MATLAB Projects Settings . . . . . . . . . . . . . . . . . 33-17

Identify and Run Tests in MATLAB Projects . . . . . . . . . . . . . . . . . . 33-20


Label Test Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-20
Identify and Run All Tests in Project . . . . . . . . . . . . . . . . . . . . . . . 33-21
Create Test Suite from Project Test Files . . . . . . . . . . . . . . . . . . . 33-21

Manage Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-24


Automatic Updates When Renaming, Deleting, or Removing Files
................................................ 33-26
Find Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-27
Add Labels to Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-27
Run Custom Tasks on Project Files . . . . . . . . . . . . . . . . . . . . . . . . 33-28
Manage Open Files When Closing Project . . . . . . . . . . . . . . . . . . . 33-29

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


Create Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30
Run Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30
Organize Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30

Componentize Large Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-32


Add or Remove Reference to a Project . . . . . . . . . . . . . . . . . . . . . 33-33
Extract Folder to Create a Referenced Project . . . . . . . . . . . . . . . 33-33
View, Edit, and Run Referenced Project Files and Shortcuts . . . . . 33-34

Share Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-36


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

Check for Compatibility Issues Using Project Upgrade . . . . . . . . . 33-40


Open Project Upgrade Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-40
Run Upgrade Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-40
Understand Upgrade Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-41
Save Upgrade Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-41

Analyze Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-43


Run a Dependency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-43
Explore the Dependency Graph, Views, and Filters . . . . . . . . . . . . 33-45
Investigate and Resolve Problems . . . . . . . . . . . . . . . . . . . . . . . . . 33-51
Find Required Products and Add-Ons . . . . . . . . . . . . . . . . . . . . . . 33-56
Find File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-57
Export Dependency Analysis Results . . . . . . . . . . . . . . . . . . . . . . 33-59

Use Source Control with MATLAB Projects . . . . . . . . . . . . . . . . . . . 33-62


Perform Source Control Operations . . . . . . . . . . . . . . . . . . . . . . . 33-62
Work with Derived Files in Projects Under Source Control . . . . . . 33-64

Run Project Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-65


Project Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-65
Run Project Integrity Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-67
Project Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-70

Create and Edit Projects Programmatically . . . . . . . . . . . . . . . . . . 33-72

Explore an Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-80

xxvii
Compare MATLAB Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-84

Merge Git Branches and Resolve Conflicts Programmatically . . . 33-86

Packages
34
Organize and Distribute Code Using MATLAB Package Manager
..................................................... 34-2
What Is a Package? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-2
Package Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-2
Share Packages in Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-3

Find and Install Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-5


Find Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-5
Install Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-5
Package Resolution During Installation . . . . . . . . . . . . . . . . . . . . . . 34-6
Package Installation Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-6
Package Registration and the MATLAB Path . . . . . . . . . . . . . . . . . . 34-7

Create and Manage Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-8


Create A New Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-8
Install Package In Editable Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 34-8
Edit Package Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-8
Manage Package Code and Subfolders . . . . . . . . . . . . . . . . . . . . . 34-10
Manage Package Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . 34-10

Distribute Packages Using Folder-Based Repositories . . . . . . . . . 34-12


Share Packages Using Repositories . . . . . . . . . . . . . . . . . . . . . . . 34-12
Designate Folder As Repository . . . . . . . . . . . . . . . . . . . . . . . . . . 34-12
Display List of Known Repositories . . . . . . . . . . . . . . . . . . . . . . . . 34-13
Remove Repository from List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-13
Security Considerations for Shared Repositories . . . . . . . . . . . . . . 34-13

Semantic Version Syntax for Packages . . . . . . . . . . . . . . . . . . . . . . 34-15


Semantic Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-15
Version Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-15
MATLAB Release Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-15

Source Control Interface


35
Source Control Integration in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 35-2
Work with Multiple Repositories at Once . . . . . . . . . . . . . . . . . . . . 35-2

Configure Source Control Settings . . . . . . . . . . . . . . . . . . . . . . . . . . 35-6


Configure Git Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-6
Configure SVN Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-9

xxviii Contents
Collaborate Using Git in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . 35-10

Clone Git Repository in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-12

Create, Manage, and Merge Git Branches . . . . . . . . . . . . . . . . . . . 35-14

Resolve Git Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-18

Push to Git Remote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-25


Pull Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-25
Fetch Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-26

Track Work Locally with Git in MATLAB . . . . . . . . . . . . . . . . . . . . . 35-27

Create Local Git Repository in MATLAB . . . . . . . . . . . . . . . . . . . . . 35-28

Review and Commit Modified Files to Git . . . . . . . . . . . . . . . . . . . . 35-37

Share Git Repository to Remote . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-40

Annotate Lines in MATLAB Editor Using Git History . . . . . . . . . . . 35-42


Enable Blame View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-42
Investigate Line Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43

Work with Git Submodules in MATLAB . . . . . . . . . . . . . . . . . . . . . . 35-45


Understand Git Repository and Submodules Hierarchy . . . . . . . . . 35-46
Add Git Submodules to Repository . . . . . . . . . . . . . . . . . . . . . . . . 35-48
Modify Files in Submodule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-50
Update Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-55

Set Up Git Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-57


Register Binary Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-57
Enable Support for Long Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-58
Enable Signing Commits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-58
Configure MATLAB to Use Git SSH Authentication . . . . . . . . . . . . 35-58
Manage Git Credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-59
Configure Git to Use Git LFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-59
Additional Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-59

Set Up SVN Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-62


Use Standard SVN Repository Structure . . . . . . . . . . . . . . . . . . . . 35-62
Register Binary Files with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-62
Enforce Locking Files Before Editing . . . . . . . . . . . . . . . . . . . . . . 35-64
Share a Subversion Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-65

Work with Files Under SVN in MATLAB . . . . . . . . . . . . . . . . . . . . . 35-66

Manage SVN Externals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-72

Resolve SVN Source Control Conflicts . . . . . . . . . . . . . . . . . . . . . . . 35-73


Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-73
Merge Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-74
Extract Conflict Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-74

Use Git Hooks in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-76

xxix
Rebase Git Branch in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-82

Squash Git Commits in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-89

Reduce Test Runtime on CI Servers . . . . . . . . . . . . . . . . . . . . . . . . . 35-94


Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-94
Set Up MATLAB Project for Continuous Integration in Jenkins . . . 35-95
Reduce Test Runtime Using Dependency Cache and Impact Analysis
................................................ 35-95
Enhance Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-96

Customize External Source Control to Use MATLAB for Diff and


Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-98
Finding Full Paths for MATLAB Diff, Merge, and AutoMerge . . . . . 35-98
Integration with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-98
Integration with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-100
Integration with Other Source Control Tools . . . . . . . . . . . . . . . . 35-101

Write a Source Control Integration with the SDK . . . . . . . . . . . . 35-102

Extension Points
36
Extend MATLAB Using Extension Points . . . . . . . . . . . . . . . . . . . . . . 36-2
Create extensions.json . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-2
Add JSON Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-2
Enable Your Customizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-3
Use References with Extension Points . . . . . . . . . . . . . . . . . . . . . . . 36-3

Add Items to Quick Access Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . 36-5


Create User-Defined MATLAB Function . . . . . . . . . . . . . . . . . . . . . 36-5
Enable Your Customizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-5

Customize How Files Display in MATLAB . . . . . . . . . . . . . . . . . . . . . 36-7


Specify Icon and Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-7
Specify Filename Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-7
Create Groups of File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-8
Enable Your File Type Customizations . . . . . . . . . . . . . . . . . . . . . . 36-8

Add Items to Files Panel Context Menu . . . . . . . . . . . . . . . . . . . . . 36-10


Add Simple Item to Context Menu . . . . . . . . . . . . . . . . . . . . . . . . 36-10
Add Menu Item with Submenu . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-12

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

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

xxx Contents
Write Script-Based Test Using Local Functions . . . . . . . . . . . . . . . 37-11

Extend Script-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-14


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

Run Tests in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-17

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


Create Test Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-20
Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-22
Debug Test Failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-25
Customize Test Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-25
Generate Code Coverage Report . . . . . . . . . . . . . . . . . . . . . . . . . . 37-27

Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-30


Create Test Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-30
Run the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-32
Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-32

Write Simple Test Case Using Functions . . . . . . . . . . . . . . . . . . . . . 37-34

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

Extend Function-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-42


Fixtures for Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . 37-42
Test Logging and Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43
Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-43
Test Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-44
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 37-44
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-45

Class-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-46


Test Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-46
How Test Classes Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-47
Test Independence and Repeatability . . . . . . . . . . . . . . . . . . . . . . 37-49
Features of Class-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-50

Write Independent and Repeatable Tests . . . . . . . . . . . . . . . . . . . . 37-51


Specify Symmetric Setup and Teardown Actions . . . . . . . . . . . . . . 37-51
Access Global State Using Methods . . . . . . . . . . . . . . . . . . . . . . . 37-52
Use Value Objects as Test Parameter Values . . . . . . . . . . . . . . . . . 37-53

Write Simple Test Case Using Classes . . . . . . . . . . . . . . . . . . . . . . . 37-55

Write Setup and Teardown Code Using Classes . . . . . . . . . . . . . . . 37-58


Test Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-58
Test Case with Method-Level Setup Code . . . . . . . . . . . . . . . . . . . 37-58
Test Case with Class-Level Setup Code . . . . . . . . . . . . . . . . . . . . . 37-59

Table of Verifications, Assertions, and Other Qualifications . . . . . 37-61

xxxi
Tag Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-64
Tag Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-64
Select and Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-65

Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . 37-68

Create Basic Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-71

Create Advanced Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-73

Use Parameters in Class-Based Tests . . . . . . . . . . . . . . . . . . . . . . . 37-78


How to Write Parameterized Tests . . . . . . . . . . . . . . . . . . . . . . . . 37-78
How to Initialize Parameterization Properties . . . . . . . . . . . . . . . . 37-79
Specify Parameterization Level . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-80
Specify How Parameters Are Combined . . . . . . . . . . . . . . . . . . . . 37-81
Use External Parameters in Tests . . . . . . . . . . . . . . . . . . . . . . . . . 37-83

Create Basic Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-85

Create Advanced Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . 37-90

Use External Parameters in Parameterized Test . . . . . . . . . . . . . . . 37-97

Define Parameters at Suite Creation Time . . . . . . . . . . . . . . . . . . 37-101

Create Simple Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-108

Run Tests for Various Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . 37-110


Set Up Example Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-110
Run All Tests in Class or Function . . . . . . . . . . . . . . . . . . . . . . . . 37-110
Run Single Test in Class or Function . . . . . . . . . . . . . . . . . . . . . . 37-110
Run Test Suites by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-111
Run Test Suites from Test Array . . . . . . . . . . . . . . . . . . . . . . . . . 37-111
Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . 37-112

Programmatically Access Test Diagnostics . . . . . . . . . . . . . . . . . . 37-113

Add Plugin to Test Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-114

Write Plugins to Extend TestRunner . . . . . . . . . . . . . . . . . . . . . . . 37-117


Custom Plugins Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-117
Extending Test Session Level Plugin Methods . . . . . . . . . . . . . . . 37-117
Extending Test Suite Level Plugin Methods . . . . . . . . . . . . . . . . 37-118
Extending Test Class Level Plugin Methods . . . . . . . . . . . . . . . . 37-118
Extending Test Level Plugin Methods . . . . . . . . . . . . . . . . . . . . . 37-119

Create Custom Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-120

Run Tests in Parallel with Custom Plugin . . . . . . . . . . . . . . . . . . . 37-125


Create Plugin Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-125
Extend Running of Test Session . . . . . . . . . . . . . . . . . . . . . . . . . 37-125
Extend Creation of Shared Test Fixtures and Test Cases . . . . . . . 37-126
Extend Running of Test Suite Portion . . . . . . . . . . . . . . . . . . . . . 37-126
Extend Reporting of Finalized Test Suite Portion . . . . . . . . . . . . 37-127
Define Helper Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-128

xxxii Contents
Plugin Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-128
Create Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-130
Add Plugin to Test Runner and Run Tests . . . . . . . . . . . . . . . . . . 37-130

Write Plugin to Add Data to Test Results . . . . . . . . . . . . . . . . . . . 37-133

Write Plugin to Save Diagnostic Details . . . . . . . . . . . . . . . . . . . . 37-138

Plugin to Generate Custom Test Output Format . . . . . . . . . . . . . . 37-142

Analyze Test Case Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-145

Analyze Failed Test Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-148

Rerun Failed Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-150

Dynamically Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-153


Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-153
Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . 37-155
Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . 37-156

Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-159

Create Custom Boolean Constraint . . . . . . . . . . . . . . . . . . . . . . . . 37-162

Overview of App Testing Framework . . . . . . . . . . . . . . . . . . . . . . . 37-166


App Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-166
Gesture Support of UI Components . . . . . . . . . . . . . . . . . . . . . . 37-166
Example: Write a Test for an App . . . . . . . . . . . . . . . . . . . . . . . . 37-168

Write Tests for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-171

Write Tests That Use App Testing and Mocking Frameworks . . . 37-175
Create App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-175
Test App with Manual Intervention . . . . . . . . . . . . . . . . . . . . . . . 37-176
Create Fully Automated Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-177

Overview of Performance Testing Framework . . . . . . . . . . . . . . . . 37-180


Determine Bounds of Measured Code . . . . . . . . . . . . . . . . . . . . . 37-180
Types of Time Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-181
Write Performance Tests with Measurement Boundaries . . . . . . . 37-181
Run Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-182
Understand Invalid Test Results . . . . . . . . . . . . . . . . . . . . . . . . . 37-182

Test Performance Using Scripts or Functions . . . . . . . . . . . . . . . . 37-184

Test Performance Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 37-188

Measure Fast Executing Test Code . . . . . . . . . . . . . . . . . . . . . . . . 37-193

Create Mock Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-196

Specify Mock Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-203


Define Mock Method Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 37-203

xxxiii
Define Mock Property Behavior . . . . . . . . . . . . . . . . . . . . . . . . . 37-204
Define Repeating and Subsequent Behavior . . . . . . . . . . . . . . . . 37-205
Summary of Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-207

Qualify Mock Object Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . 37-208


Qualify Mock Method Interaction . . . . . . . . . . . . . . . . . . . . . . . . 37-208
Qualify Mock Property Interaction . . . . . . . . . . . . . . . . . . . . . . . 37-209
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-210
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-212

Ways to Write Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-214


Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-214
Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-215
Class-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-215
Extend Unit Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . 37-216

Compile MATLAB Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-217


Run Tests with Standalone Applications . . . . . . . . . . . . . . . . . . . 37-217
Run Tests in Parallel with Standalone Applications . . . . . . . . . . . 37-218
TestRand Class Definition Summary . . . . . . . . . . . . . . . . . . . . . . 37-218

Types of Code Coverage for MATLAB Source Code . . . . . . . . . . . . 37-220


Statement Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-220
Function Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-220

Collect Statement and Function Coverage Metrics for MATLAB


Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-222

Insert Test Code Using Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-227


Create Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-227
Add Parameters and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-227
Run Tests in Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-230

Develop and Integrate Software with Continuous Integration . . 37-231


Continuous Integration Workflow . . . . . . . . . . . . . . . . . . . . . . . . 37-231
Continuous Integration with MathWorks Products . . . . . . . . . . . 37-232

Generate Artifacts Using MATLAB Unit Test Plugins . . . . . . . . . . 37-235

Continuous Integration with MATLAB on CI Platforms . . . . . . . . 37-239


Azure DevOps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-239
Bamboo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-239
CircleCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-239
GitHub Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-239
GitLab CI/CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-239
Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-240
TeamCity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-240
Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37-240

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

Create and Run Tasks Using Build Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-5


Create Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-5
Summary of Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-6
Run Tasks in Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-7

Create Tasks That Accept Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-9


Create Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-9
Summary of Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-10
Run Tasks with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-11

Create Groups of Similar Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-14


Create Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-14
Summary of Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-15
Visualize Task Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-16
Run Task Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-16
Source and Test Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-18

Create Custom Reusable Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-20


Create Task Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-20
Create Task from Task Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-22
Run the Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-22

Improve Performance with Incremental Builds . . . . . . . . . . . . . . . . . . . 38-24


Create and Run Tasks That Support Incremental Builds . . . . . . . . . . . . 38-24
MATLAB Incremental Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-27

Run Build from Toolstrip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-29


Run Build in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-29
Run Build in Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-32

System object Usage and Authoring


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

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


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

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


System Design and Simulation in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 39-7

xxxv
Create Individual Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-7
Configure Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-8
Create and Configure Components at the Same Time . . . . . . . . . . . . . . . 39-8
Assemble Components Into System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-9
Run Your System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-9
Reconfiguring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-10

Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-11


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

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

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


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

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

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

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


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

Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-24


Specify Property as Nontunable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-24
Specify Property as DiscreteState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-24
Insert Custom Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-25
Example Class with Various Property Attributes . . . . . . . . . . . . . . . . . . 39-25

Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-27


Specify Inactive Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-27
Complete Class Definition File with Inactive Properties Method . . . . . . 39-27

Limit Property Values to Finite List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-29


Property Validation with mustBeMember . . . . . . . . . . . . . . . . . . . . . . . 39-29
Enumeration Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-29
Create a Whiteboard System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-30

Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-33

Define Composite System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-35

Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-37


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

Save and Load System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-39


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

xxxvi Contents
Define System Object Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-42

Handle Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-44


React to Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 39-44
Restrict Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . 39-44

Summary of Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-46


Setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-46
Running the Object or Step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 39-46
Reset Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-47
Release Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-47

Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-49


setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-49
Running the Object or step Call Sequence . . . . . . . . . . . . . . . . . . . . . . 39-49
reset Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-50
release Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-50

Tips for Defining System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-51


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

Insert System Object Code Using MATLAB Editor . . . . . . . . . . . . . . . . . 39-54


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

Inspect System Object Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-59

Use Global Variables in System Objects . . . . . . . . . . . . . . . . . . . . . . . . . 39-62


System Object Global Variables in MATLAB . . . . . . . . . . . . . . . . . . . . . 39-62
System Object Global Variables in Simulink . . . . . . . . . . . . . . . . . . . . . 39-62

Create Moving Average System Object . . . . . . . . . . . . . . . . . . . . . . . . . . 39-66

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

Create Composite System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39-76

xxxvii
Language
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: Unrecognized 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-11

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-77

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 practice 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: Unrecognized Function or Variable

Resolve Error: Unrecognized Function or Variable

Issue
A frequently encountered error message indicates that MATLAB cannot find a particular program file
or variable:

Unrecognized function or variable 'x'.

Many situations can lead to this error. This topic describes how to identify and address common
scenarios.

Possible Solutions
Look for Typos

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 name, a suggested name appears in the Command Window. For example,
this command fails because it includes an uppercase letter in the function name.

accumArray(1,10)

Unrecognized function or variable 'accumArray'.

Did you mean:


>> accumarray(1,10)

Press Enter to execute the suggested command or Esc to dismiss it.

Check Variables in Workspace

To check whether a variable you want to use is available in the current workspace, use the whos
command.

If the variable is not listed, here are possible causes:

• The code to create the variable has not yet run in the current session, or a call to the clear or
clearvars command removed the variable. If you previously created the variable, the code is
often available in the Command History, which you can access from the command line by using the
up arrow key.
• If you are editing a script file, the code might be trying to use the variable before assigning a
value to that variable. When viewing a file in the Editor, you can click a variable name to find all
the references to the variable and check the order of operations.

1-11
1 Syntax Basics

If the variable is in the workspace and you are writing a function that throws the error, check that
you are passing the variable to the function as an input argument. Functions do not use the base
workspace, so variables must be explicitly passed into the function workspace. For instance, this
function cannot find variable c even if it exists in the base workspace.
function y = myfunction(x)
y = x + c;
end

To fix this problem, add c to the function definition line.


function y = myfunction(x,c)
y = x + c;
end

Then, include the variable in the function call.


Y = myfunction(X,c);

For more information about workspaces, see “Base and Function Workspaces” on page 20-9.

Check Function Availability

If the unrecognized item is a function and you are unfamiliar with that function, check whether it is
included in your installed software.

First, search the most recent MathWorks documentation for the function. If there is no
documentation for that function, check other resources, such as MathWorks File Exchange, to locate
the software that includes the function.

If you find the function in MathWorks documentation:


1 Run example code from the documentation to check whether the function is available. If the code
does not generate the Unrecognized function error, skip to “Check Function Inputs” on page
1-13 to continue diagnosing the problem.
2 Check whether the function is newer than your version of the software. To identify when a
function was introduced, refer to the Version History section of its reference page.

1-12
Resolve Error: Unrecognized Function or Variable

To check your version of MATLAB, use the version command. The release is in parentheses.

version

ans =

'24.1.0.2661297 (R2024a) Update 5'


3 Check whether the required product is installed. The required product is in the left pane of the
documentation page.

To check whether that product is installed, use the ver command.

ver

.
.
.
MATLAB Version 24.1 (R2024a)
Deep Learning Toolbox Version 24.1 (R2024a)
Statistics and Machine Learning Toolbox Version 24.1 (R2024a)
Text Analytics Toolbox Version 24.1 (R2024a)

If the product is not listed, it is likely not installed. For information on installing products, see
“Get and Manage Add-Ons”.
4 If you have installed the product, but it does not appear in a regenerated list from ver, update
the toolbox cache. On the Home tab, in the Environment section, click Settings. Select
MATLAB > General, and then click Update Toolbox Path Cache.
5 If the product still does not appear in the list from ver, make sure that you have an active license
for that toolbox. Use the license command to display currently active licenses and visit the
License Center to view the associated products. For more information on managing licenses, see
“Manage Your Licenses”.

Check Function Inputs

Calling an available function with existing variables can fail when the inputs are in a form that the
function cannot recognize. Refer to the function documentation for expected syntaxes and input
arguments. You can access function documentation directly with the doc command.

doc functionName

There are two common issues that can be difficult to identify: using unexpected indexing expressions
and omitting an object input.

1-13
1 Syntax Basics

Unexpected Indexing

If you type a colon in the position of an input argument, MATLAB interprets the code as an indexing
expression instead of a function call. For example, the isnumeric function accepts a single input
array, isnumeric(A). In many cases, isnumeric can detect when there are multiple arguments.

isnumeric(A,B)

Error using isnumeric


Too many input arguments.

However, with a colon in place of an argument, MATLAB interprets isnumeric as an undefined


variable instead of a function.

isnumeric(A,:)

Unrecognized function or variable 'isnumeric'.

Omitted Object Input

Object methods are typically documented using function syntax in the form
method(object,inputs). If you are accustomed to using dot notation for methods in the form
object.method(inputs), make sure to include the object as the first input when using function
form.

Make Your Function Available

When you are writing a function that you plan to call from the command line or from another file:

• Save the function in a file with the same name as the function. If the names of the function and file
are different, the filename overrides the function name.

For example, save a function named curveplot in a file named curveplot with the extension .m
or .mlx. If function curveplot is in a file named curveplotfunction.m, calls to curveplot
fail.
• Make sure that the function is the first or only function defined in the file and that the file does not
include any code outside function definitions. If the function does not meet these conditions, it is a
local function and cannot be called from outside that file. For more information, see “Local
Functions” on page 20-27.

For more information on writing functions, see “Create Functions in Files” on page 20-2.

1-14
2

Program Components

• “MATLAB Operators and Special Characters” on page 2-2


• “Array vs. Matrix Operations” on page 2-7
• “Compatible Array Sizes for Basic Operations” on page 2-12
• “Array Comparison with Relational Operators” on page 2-16
• “Operator Precedence” on page 2-19
• “Average Similar Data Points Using a Tolerance” on page 2-21
• “Group Scattered Data Using a Tolerance” on page 2-23
• “Bit-Wise Operations” on page 2-25
• “Perform Cyclic Redundancy Check” on page 2-31
• “Conditional Statements” on page 2-34
• “Loop Control Statements” on page 2-36
• “Regular Expressions” on page 2-38
• “Lookahead Assertions in Regular Expressions” on page 2-50
• “Tokens in Regular Expressions” on page 2-53
• “Dynamic Regular Expressions” on page 2-59
• “Comma-Separated Lists” on page 2-66
• “Alternatives to the eval Function” on page 2-77
2 Program Components

MATLAB Operators and Special Characters


This page contains a comprehensive listing of all MATLAB operators, symbols, and special characters.

Arithmetic Operators
Symbol Role
+ Addition
+A Unary plus
- Subtraction
-A Unary minus
.* Element-wise multiplication
* Matrix multiplication
./ Element-wise right division
/ Matrix right division
.\ Element-wise left division
\ Matrix left division

(also known as backslash)


.^ Element-wise power
^ Matrix power
.' Transpose
' Complex conjugate transpose

Relational Operators
Symbol Role
== Equal to

The = character is for assignment, whereas the == character is for


comparing the elements in two arrays.
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

Logical Operators
Symbol Role
& Find logical AND

2-2
MATLAB Operators and Special Characters

Symbol Role
| Find logical OR
&& Find logical AND (with short-circuiting)
|| Find logical OR (with short-circuiting)
~ Find logical NOT

Special Characters
Symbol Role
@ Create anonymous functions and function
handles, call superclass methods
. Decimal point, element-wise operations, indexing
... Line continuation
, Separate row elements in an array, array
subscripts, function input and output arguments,
commands entered on the same line
: Vector creation, for-loop iteration, indexing
; Separate rows in an array creation command,
suppress output of a line of code
( ) Operator precedence, function argument
enclosure, indexing
[ ] Array construction and concatenation, empty
matrix creation, element deletion, multiple output
argument assignment
{ } Create cell array, indexing
% Code comments, conversion specifier
{% %} Block of comments that extend beyond one line
! Issue operating system command
? Retrieve metaclass information for class name
'' Create character array
"" Create string
~ Represent logical NOT, suppress specific input or
output arguments.
= Variable creation and indexing assignment.

The = character is for assignment, whereas the


== character is for comparing the elements in
two arrays.
< & Specify one or more superclasses in a class
definition.
.? on page 26-20 Specify the fields of a name-value structure as the
names of all writable properties of the class.

2-3
2 Program Components

String and Character Formatting


Some special characters can only be used in the text of a character vector or string. You can use
these special characters to insert new lines or carriage returns, specify folder paths, and more.

Use the special characters in this table to specify a folder path using a character vector or string.

/ Name: Slash and Backslash

\ Uses: File or folder path separation

Description: In addition to their use as mathematical operators, the slash and backslash
characters separate the elements of a path or folder. On Microsoft Windows based
systems, both slash and backslash have the same effect. On The Open Group UNIX based
systems, you must use slash only.

Examples

On a Windows system, you can use either backslash or slash:

dir([matlabroot '\toolbox\matlab\elmat\shiftdim.m'])
dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])

On a UNIX system, use only the forward slash:

dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])
.. Name: Dot dot

Uses: Parent folder

Description: Two dots in succession refers to the parent of the current folder. Use this
character to specify folder paths relative to the current folder.

Examples

To go up two levels in the folder tree and down into the test folder, use:

cd ..\..\test

More Information

• cd

2-4
MATLAB Operators and Special Characters

* Name: Asterisk

Uses: Wildcard character

Description: In addition to being the symbol for matrix multiplication, the asterisk * is
used as a wildcard character.

Wildcards are generally used in file operations that act on multiple files or folders.
MATLAB matches all characters in the name exactly except for the wildcard character *,
which can match any one or more characters.

Examples

Locate all files with names that start with january_ and have a .mat file extension:

dir('january_*.mat')
@ Name: At symbol

Uses: Class folder indicator

Description: An @ sign indicates the name of a class folder.

Examples

Refer to a class folder:

\@myClass\get.m

More Information

• “Folders Containing Class Definitions”


+ Name: Plus

Uses: Namespace directory indicator

Description: A + sign indicates the name of a namespace folder.

Examples

Namespace folders always begin with the + character:

+myfolder
+myfolder/pkfcn.m % a namespace function
+myfolder/@myClass % class folder in a namespace

More Information

• “Create Namespaces”

There are certain special characters that you cannot enter as ordinary text. Instead, you must use
unique character sequences to represent them. Use the symbols in this table to format strings and
character vectors on their own or in conjunction with formatting functions like compose, sprintf,
and error. For more information, see “Formatting Text” on page 6-24.

2-5
2 Program Components

Symbol Effect on Text


'' Single quotation mark
%% Single percent sign
\\ Single backslash
\a Alarm
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\xN Hexadecimal number, N
\N Octal number, N

See Also

More About
• “Array vs. Matrix Operations” on page 2-7
• “Array Comparison with Relational Operators” on page 2-16
• “Compatible Array Sizes for Basic Operations” on page 2-12
• “Operator Precedence” on page 2-19
• “Find Array Elements That Meet Conditions” on page 5-2
• “Greek Letters and Special Characters in Chart Text”

2-6
Array vs. Matrix Operations

Array vs. Matrix Operations


In this section...
“Introduction” on page 2-7
“Array Operations” on page 2-7
“Matrix Operations” on page 2-9

Introduction
MATLAB has two different types of arithmetic operations: array operations and matrix operations.
You can use these arithmetic operations to perform numeric computations, for example, adding two
numbers, raising the elements of an array to a given power, or multiplying two matrices.

Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by
element operations and support multidimensional arrays. The period character (.) distinguishes the
array operations from the matrix operations. However, since the matrix and array operations are the
same for addition and subtraction, the character pairs .+ and .- are unnecessary.

Array Operations
Array operations execute element by element operations on corresponding elements of vectors,
matrices, and multidimensional arrays. If the operands have the same size, then each element in the
first operand gets matched up with the element in the same location in the second operand. If the
operands have compatible sizes, then each input is implicitly expanded as needed to match the size of
the other.

As a simple example, you can add two vectors with the same size.
A = [1 1 1]

A =

1 1 1

B = [1 2 3]

B =

1 2 3

A+B

ans =

2 3 4

If one operand is a scalar and the other is not, then MATLAB implicitly expands the scalar to be the
same size as the other operand. For example, you can compute the element-wise product of a scalar
and a matrix.
A = [1 2 3; 1 2 3]

A =

2-7
2 Program Components

1 2 3
1 2 3

3.*A

ans =

3 6 9
3 6 9

Implicit expansion also works if you subtract a 1-by-3 vector from a 3-by-3 matrix because the two
sizes are compatible. When you perform the subtraction, the vector is implicitly expanded to become
a 3-by-3 matrix.

A = [1 1 1; 2 2 2; 3 3 3]

A =

1 1 1
2 2 2
3 3 3

m = [2 4 6]

m =

2 4 6

A - m

ans =

-1 -3 -5
0 -2 -4
1 -1 -3

A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-by-1 vector,
then each vector implicitly expands into a 2-by-3 matrix before MATLAB executes the element-wise
addition.

x = [1 2 3]

x =

1 2 3

y = [10; 15]

y =

10
15

x + y

ans =

11 12 13
16 17 18

2-8
Array vs. Matrix Operations

If the sizes of the two operands are incompatible, then you get an error.

A = [8 1 6; 3 5 7; 4 9 2]

A =

8 1 6
3 5 7
4 9 2

m = [2 4]

m =

2 4

A - m

Arrays have incompatible sizes for this operation.

For more information, see “Compatible Array Sizes for Basic Operations” on page 2-12.

The following table provides a summary of arithmetic array operators in MATLAB. For function-
specific information, click the link to the function reference page in the last column.

Operator Purpose Description Reference


Page
+ Addition A+B adds A and B. plus
+ Unary plus +A returns A. uplus
- Subtraction A-B subtracts B from A minus
- Unary minus -A negates the elements of A. uminus
.* Element-wise A.*B is the element-by-element product of A and times
multiplication B.
.^ Element-wise A.^B is the matrix with elements A(i,j) to the power
power B(i,j) power.
./ Right array A./B is the matrix with elements A(i,j)/ rdivide
division B(i,j).
.\ Left array A.\B is the matrix with elements B(i,j)/ ldivide
division A(i,j).
.' Array transpose A.' is the array transpose of A. For complex transpose
matrices, this does not involve conjugation.

Matrix Operations
Matrix operations follow the rules of linear algebra and are not compatible with multidimensional
arrays. The required size and shape of the inputs in relation to one another depends on the operation.
For nonscalar inputs, the matrix operators generally calculate different answers than their array
operator counterparts.

For example, if you use the matrix right division operator, /, to divide two matrices, the matrices
must have the same number of columns. But if you use the matrix multiplication operator, *, to

2-9
2 Program Components

multiply two matrices, then the matrices must have a common inner dimension. That is, the number
of columns in the first input must be equal to the number of rows in the second input. The matrix
multiplication operator calculates the product of two matrices with the formula,

n
C(i, j) = ∑ A(i, k)B(k, j) .
k=1

To see this, you can calculate the product of two matrices.

A = [1 3;2 4]

A =

1 3
2 4

B = [3 0;1 5]

B =

3 0
1 5

A*B

ans =

6 15
10 20

The previous matrix product is not equal to the following element-wise product.

A.*B

ans =

3 0
2 20

The following table provides a summary of matrix arithmetic operators in MATLAB. For function-
specific information, click the link to the function reference page in the last column.

Operator Purpose Description Reference


Page
* Matrix C = A*B is the linear algebraic product of the mtimes
multiplication matrices A and B. The number of columns of A
must equal the number of rows of B.
\ Matrix left x = A\B is the solution to the equation Ax = B. mldivide
division Matrices A and B must have the same number of
rows.
/ Matrix right x = B/A is the solution to the equation xA = B. mrdivide
division Matrices A and B must have the same number of
columns. In terms of the left division operator,
B/A = (A'\B')'.

2-10
Array vs. Matrix Operations

Operator Purpose Description Reference


Page
^ Matrix power A^B is A to the power B, if B is a scalar. For other mpower
values of B, the calculation involves eigenvalues
and eigenvectors.
' Complex A' is the linear algebraic transpose of A. For ctranspose
conjugate complex matrices, this is the complex conjugate
transpose transpose.

See Also

More About
• “Compatible Array Sizes for Basic Operations” on page 2-12
• “MATLAB Operators and Special Characters” on page 2-2
• “Operator Precedence” on page 2-19

2-11
2 Program Components

Compatible Array Sizes for Basic Operations

Most binary (two-input) operators and functions in MATLAB support numeric arrays that have
compatible sizes. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the
inputs are either the same or one of them is 1. In the simplest cases, two array sizes are compatible if
they are exactly the same or if one is a scalar. MATLAB implicitly expands arrays with compatible
sizes to be the same size during the execution of the element-wise operation or function.

Inputs with Compatible Sizes


2-D Inputs

These are some combinations of scalars, vectors, and matrices that have compatible sizes:

• Two inputs which are exactly the same size.

• One input is a scalar.

• One input is a matrix, and the other is a column vector with the same number of rows.

• One input is a column vector, and the other is a row vector.

2-12
Compatible Array Sizes for Basic Operations

Multidimensional Arrays

Every array in MATLAB has trailing dimensions of size 1. For multidimensional arrays, this means
that a 3-by-4 matrix is the same as a matrix of size 3-by-4-by-1-by-1-by-1. Examples of
multidimensional arrays with compatible sizes are:

• One input is a matrix, and the other is a 3-D array with the same number of rows and columns.

• One input is a matrix, and the other is a 3-D array. The dimensions are all either the same or one
of them is 1.

Empty Arrays

The rules are the same for empty arrays or arrays that have a dimension size of zero. The size of the
dimension that is not equal to 1 determines the size of the output. This means that dimensions with a

2-13
2 Program Components

size of zero must be paired with a dimension of size 1 or 0 in the other array, and that the output has
a dimension size of 0.

A: 1-by-0
B: 3-by-1
Result: 3-by-0

Inputs with Incompatible Sizes


Incompatible inputs have sizes that cannot be implicitly expanded to be the same size. For example:

• One of the dimension sizes are not equal, and neither is 1.

A: 3-by-2
B: 4-by-2
• Two nonscalar row vectors with lengths that are not the same.

A: 1-by-3
B: 1-by-4

Examples
Subtract Vector from Matrix

To simplify vector-matrix operations, use implicit expansion with dimensional functions such as sum,
mean, min, and others.

For example, calculate the mean value of each column in a matrix, then subtract the mean value from
each element.

A = magic(3)

A =

8 1 6
3 5 7
4 9 2

C = mean(A)

C =

5 5 5

A - C

ans =

3 -4 1
-2 0 2
-1 4 -3

Add Row and Column Vector

Row and column vectors have compatible sizes, and when you perform an operation on them the
result is a matrix.

2-14
Compatible Array Sizes for Basic Operations

For example, add a row and column vector. The result is the same as bsxfun(@plus,a,b).

a = [1 2 3 4]

ans =

1 2 3 4

b = [5; 6; 7]

ans =

5
6
7

a + b

ans =

6 7 8 9
7 8 9 10
8 9 10 11

See Also
bsxfun

More About
• “Array vs. Matrix Operations” on page 2-7
• “MATLAB Operators and Special Characters” on page 2-2

2-15
2 Program Components

Array Comparison with Relational Operators


In this section...
“Array Comparison” on page 2-16
“Logic Statements” on page 2-18

Relational operators compare operands quantitatively, using operators like “less than”, “greater
than”, and “not equal to.” The result of a relational comparison is a logical array indicating the
locations where the relation is true.

These are the relational operators in MATLAB.

Symbol Function Equivalent Description


< lt Less than
<= le Less than or equal to
> gt Greater than
>= ge Greater than or equal to
== eq Equal to
~= ne Not equal to

Array Comparison
Numeric Arrays

The relational operators perform element-wise comparisons between two arrays. The arrays must
have compatible sizes to facilitate the operation. Arrays with compatible sizes are implicitly expanded
to be the same size during execution of the calculation. In the simplest cases, the two operands are
arrays of the same size, or one is a scalar. For more information, see “Compatible Array Sizes for
Basic Operations” on page 2-12.

For example, if you compare two matrices of the same size, then the result is a logical matrix of the
same size with elements indicating where the relation is true.

A = [2 4 6; 8 10 12]

A =

2 4 6
8 10 12

B = [5 5 5; 9 9 9]

B =

5 5 5
9 9 9

A < B

ans =

2-16
Array Comparison with Relational Operators

1 1 0
1 0 0

Similarly, you can compare one of the arrays to a scalar.

A > 7

ans =

0 0 0
1 1 1

If you compare a 1-by-N row vector to an M-by-1 column vector, then MATLAB expands each vector
into an M-by-N matrix before performing the comparison. The resulting matrix contains the
comparison result for each combination of elements in the vectors.

A = 1:3

A =

1 2 3

B = [2; 3]

B =

2
3

A >= B

ans =

0 1 1
0 0 1

Empty Arrays

The relational operators work with arrays for which any dimension has size zero, as long as both
arrays have compatible sizes. This means that if one array has a dimension size of zero, then the size
of the corresponding dimension in the other array must be 1 or zero, and the size of that dimension in
the output is zero.

A = ones(3,0);
B = ones(3,1);
A == B

ans =

Empty matrix: 3-by-0

However, expressions such as

A == []

return an error if A is not 0-by-0 or 1-by-1. This behavior is consistent with that of all other binary
operators, such as +, -, >, <, &, |, and so on.

To test for empty arrays, use isempty(A).

2-17
2 Program Components

Complex Numbers

• The operators >, <, >=, and <= use only the real part of the operands in performing comparisons.
• The operators == and ~= test both real and imaginary parts of the operands.

Inf, NaN, NaT, and undefined Element Comparisons

• Inf values are equal to other Inf values.


• NaN values are not equal to any other numeric value, including other NaN values.
• NaT values are not equal to any other datetime value, including other NaT values.
• Undefined categorical elements are not equal to any other categorical value, including other
undefined elements.

Logic Statements
Use relational operators in conjunction with the logical operators A & B (AND), A | B (OR),
xor(A,B) (XOR), and ~A (NOT), to string together more complex logical statements.

For example, you can locate where negative elements occur in two arrays.

A = [2 -1; -3 10]

A =

2 -1
-3 10

B = [0 -2; -3 -1]

B =

0 -2
-3 -1

A<0 & B<0

ans =

0 1
1 0

For more examples, see “Find Array Elements That Meet Conditions” on page 5-2.

See Also
gt | lt | ge | le | eq | ne

More About
• “Array vs. Matrix Operations” on page 2-7
• “Compatible Array Sizes for Basic Operations” on page 2-12
• “MATLAB Operators and Special Characters” on page 2-2

2-18
Operator Precedence

Operator Precedence

You can build expressions that use any combination of arithmetic, relational, and logical operators.
Precedence levels determine the order in which MATLAB evaluates an expression. Within each
precedence level, operators have equal precedence and are evaluated from left to right. The
precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level
to lowest precedence level:
1 Parentheses ()
2 Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
3 Power with unary minus (.^-), unary plus (.^+), or logical negation (.^~) as well as matrix
power with unary minus (^-), unary plus (^+), or logical negation (^~).

Note Although most operators work from left to right, the operators (^-), (.^-), (^+), (.^+),
(^~), and (.^~) work from second from the right to left. It is recommended that you use
parentheses to explicitly specify the intended precedence of statements containing these
operator combinations.
4 Unary plus (+), unary minus (-), logical negation (~)
5 Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix
right division (/), matrix left division (\)
6 Addition (+), subtraction (-)
7 Colon operator (:)
8 Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=),
equal to (==), not equal to (~=)
9 Element-wise AND (&)
10 Element-wise OR (|)
11 Short-circuit AND (&&)
12 Short-circuit OR (||)

Precedence of AND and OR Operators


MATLAB always gives the & operator precedence over the | operator. Although MATLAB typically
evaluates expressions from left to right, the expression a|b&c is evaluated as a|(b&c). It is a good
idea to use parentheses to explicitly specify the intended precedence of statements containing
combinations of & and |.

The same precedence rule holds true for the && and || operators.

Overriding Default Precedence


The default precedence can be overridden using parentheses, as shown in this example:
A = [3 9 5];
B = [2 1 5];
C = A./B.^2
C =
0.7500 9.0000 0.2000

2-19
2 Program Components

C = (A./B).^2
C =
2.2500 81.0000 1.0000

See Also

More About
• “Array vs. Matrix Operations” on page 2-7
• “Compatible Array Sizes for Basic Operations” on page 2-12
• “Array Comparison with Relational Operators” on page 2-16
• “MATLAB Operators and Special Characters” on page 2-2

2-20
Average Similar Data Points Using a Tolerance

Average Similar Data Points Using a Tolerance

This example shows how to use uniquetol to find the average z-coordinate of 3-D points that have
similar (within tolerance) x and y coordinates.

Use random points picked from the peaks function in the domain [ − 3, 3] × [ − 3, 3] as the data set.
Add a small amount of noise to the data.

xy = rand(10000,2)*6-3;
z = peaks(xy(:,1),xy(:,2)) + 0.5-rand(10000,1);
A = [xy z];
plot3(A(:,1), A(:,2), A(:,3), '.')
view(-28,32)

Find points that have similar x and y coordinates using uniquetol with these options:

• Specify ByRows as true, since the rows of A contain the point coordinates.
• Specify OutputAllIndices as true to return the indices for all points that are within tolerance
of each other.
• Specify DataScale as [1 1 Inf] to use an absolute tolerance for the x and y coordinates, while
ignoring the z-coordinate.

DS = [1 1 Inf];
[C,ia] = uniquetol(A, 0.3, 'ByRows', true, ...
'OutputAllIndices', true, 'DataScale', DS);

Average each group of points that are within tolerance (including the z-coordinates), producing a
reduced data set that still holds the general shape of the original data.

2-21
2 Program Components

for k = 1:length(ia)
aveA(k,:) = mean(A(ia{k},:),1);
end

Plot the resulting averaged-out points on top of the original data.

hold on
plot3(aveA(:,1), aveA(:,2), aveA(:,3), '.r', 'MarkerSize', 15)

See Also
uniquetol

More About
• “Group Scattered Data Using a Tolerance” on page 2-23

2-22
Group Scattered Data Using a Tolerance

Group Scattered Data Using a Tolerance

This example shows how to group scattered data points based on their proximity to points of interest.

Create a set of random 2-D points. Then create and plot a grid of equally spaced points on top of the
random data.
x = rand(10000,2);
[a,b] = meshgrid(0:0.1:1);
gridPoints = [a(:), b(:)];
plot(x(:,1), x(:,2), '.')
hold on
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)

Use ismembertol to locate the data points in x that are within tolerance of the grid points in
gridPoints. Use these options with ismembertol:

• Specify ByRows as true, since the point coordinates are in the rows of x.
• Specify OutputAllIndices as true to return all of the indices for rows in x that are within
tolerance of the corresponding row in gridPoints.
[LIA,LocB] = ismembertol(gridPoints, x, 0.05, ...
'ByRows', true, 'OutputAllIndices', true);

For each grid point, plot the points in x that are within tolerance of that grid point.
figure
hold on
for k = 1:length(LocB)
plot(x(LocB{k},1), x(LocB{k},2), '.')
end
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)

2-23
2 Program Components

See Also
ismembertol

More About
• “Average Similar Data Points Using a Tolerance” on page 2-21

2-24
Bit-Wise Operations

Bit-Wise Operations

This topic shows how to use bit-wise operations in MATLAB® to manipulate the bits of numbers.
Operating on bits is directly supported by most modern CPUs. In many cases, manipulating the bits of
a number in this way is quicker than performing arithmetic operations like division or multiplication.

Number Representations

Any number can be represented with bits (also known as binary digits). The binary, or base 2, form of
a number contains 1s and 0s to indicate which powers of 2 are present in the number. For example,
the 8-bit binary form of 7 is

00000111

A collection of 8 bits is also called 1 byte. In binary representations, the bits are counted from the
right to the left, so the first bit in this representation is a 1. This number represents 7 because

2 1 0
2 + 2 + 2 = 7.

When you type numbers into MATLAB, it assumes the numbers are double precision (a 64-bit binary
representation). However, you can also specify single-precision numbers (32-bit binary
representation) and integers (signed or unsigned, from 8 to 64 bits). For example, the most memory
efficient way to store the number 7 is with an 8-bit unsigned integer:

a = uint8(7)

a = uint8

You can even specify the binary form directly using the prefix 0b followed by the binary digits (for
more information, see “Hexadecimal and Binary Values” on page 6-54). MATLAB stores the number
in an integer format with the fewest number of bits. Instead of specifying all the bits, you need to
specify only the left-most 1 and all the digits to the right of it. The bits to the left of that bit are
trivially zero. So the number 7 is:

b = 0b111

b = uint8

MATLAB stores negative integers using two's complement. For example, consider the 8-bit signed
integer -8. To find the two's complement bit pattern for this number:

1 Start with the bit pattern of the positive version of the number, 8: 00001000.
2 Next, flip all of the bits: 11110111.
3 Finally, add 1 to the result: 11111000.

The result, 11111000, is the bit pattern for -8:

n = 0b11111000s8

2-25
2 Program Components

n = int8

-8

MATLAB does not natively display the binary format of numbers. For that, you can use the dec2bin
function, which returns a character vector of binary digits for positive integers. Again, this function
returns only the digits that are not trivially zero.

dec2bin(b)

ans =
'111'

You can use bin2dec to switch between the two formats. For example, you can convert the binary
digits 10110101 to decimal format with the commands

data = [1 0 1 1 0 1 0 1];
dec = bin2dec(num2str(data))

dec =
181

The cast and typecast functions are also useful to switch among different data types. These
functions are similar, but they differ in how they treat the underlying storage of the number:

• cast — Changes the underlying data type of a variable.


• typecast — Converts data types without changing the underlying bits.

Because MATLAB does not display the digits of a binary number directly, you must pay attention to
data types when you work with bit-wise operations. Some functions return binary digits as a
character vector (dec2bin), some return the decimal number (bitand), and others return a vector of
the bits themselves (bitget).

Bit Masking with Logical Operators

MATLAB has several functions that enable you to perform logical operations on the bits of two equal-
length binary representations of numbers, known as bit masking:

• bitand — If both digits are 1, then the resulting digit is also a 1. Otherwise, the resulting digit is
0.
• bitor — If either digit is 1, then the resulting digit is also a 1. Otherwise, the resulting digit is 0.
• bitxor — If the digits are different, then the resulting digit is a 1. Otherwise, the resulting digit
is 0.

In addition to these functions, the bit-wise complement is available with bitcmp, but this is a unary
operation that flips the bits in only one number at a time.

One use of bit masking is to query the status of a particular bit. For example, if you use a bit-wise
AND operation with the binary number 00001000, you can query the status of the fourth bit. You can
then shift that bit to the first position so that MATLAB returns a 0 or 1 (the next section describes bit
shifting in more detail).

n = 0b10111001;
n4 = bitand(n,0b1000);
n4 = bitshift(n4,-3)

2-26
Bit-Wise Operations

n4 = uint8

Bit-wise operations can have surprising applications. For example, consider the 8-bit binary
representation of the number n = 8:
00001000

8 is a power of 2, so its binary representation contains a single 1. Now consider the number
n − 1 = 7:
00000111

By subtracting 1, all of the bits starting at the right-most 1 are flipped. As a result, when n is a power
of 2, corresponding digits of n and n − 1 are always different, and the bit-wise AND returns zero.
n = 0b1000;
bitand(n,n-1)

ans = uint8

0
However, when n is not a power of 2, then the right-most 1 is for the 2 bit, so n and n − 1 have all
0
the same bits except for the 2 bit. For this case, the bit-wise AND returns a nonzero number.
n = 0b101;
bitand(n,n-1)

ans = uint8

This operation suggests a simple function that operates on the bits of a given input number to check
whether the number is a power of 2:
function tf = isPowerOfTwo(n)
tf = n && ~bitand(n,n-1);
end

The use of the short-circuit AND operator && checks to make sure that n is not zero. If it is, then the
function does not need to calculate bitand(n,n-1) to know that the correct answer is false.

Shifting Bits

Because bit-wise logical operations compare corresponding bits in two numbers, it is useful to be able
to move the bits around to change which bits are compared. You can use bitshift to perform this
operation:

• bitshift(A,N) shifts the bits of A to the left by N digits. This is equivalent to multiplying A by
N
2 .
• bitshift(A,-N) shifts the bits of A to the right by N digits. This is equivalent to dividing A by
N
2 .

These operations are sometimes written A<<N (left shift) and A>>N (right shift), but MATLAB does not
use << and >> operators for this purpose.

2-27
2 Program Components

When the bits of a number are shifted, some bits fall off the end of the number, and 0s or 1s are
introduced to fill in the newly created space. When you shift bits to the left, the bits are filled in on
the right; when you shift bits to the right, the bits are filled in on the left.

For example, if you shift the bits of the number 8 (binary: 1000) to the right by one digit, you get 4
(binary: 100).

n = 0b1000;
bitshift(n,-1)

ans = uint8

Similarly, if you shift the number 15 (binary: 1111) to the left by two digits, you get 60 (binary:
111100).

n = 0b1111;
bitshift(15,2)

ans =
60

When you shift the bits of a negative number, bitshift preserves the signed bit. For example, if you
shift the signed integer -3 (binary: 11111101) to the right by 2 digits, you get -1 (binary: 11111111).
In these cases, bitshift fills in on the left with 1s rather than 0s.

n = 0b11111101s8;
bitshift(n,-2)

ans = int8

-1

Writing Bits

You can use the bitset function to change the bits in a number. For example, change the first bit of
the number 8 to a 1 (which adds 1 to the number):

bitset(8,1)

ans =
9

By default, bitset flips bits to on or 1. You can optionally use the third input argument to specify the
bit value.

bitset does not change multiple bits at once, so you need to use a for loop to change multiple bits.
Therefore, the bits you change can be either consecutive or nonconsecutive. For example, change the
first two bits of the binary number 1000:

bits = [1 2];
c = 0b1000;
for k = 1:numel(bits)
c = bitset(c,bits(k));
end
dec2bin(c)

2-28
Bit-Wise Operations

ans =
'1011'

Another common use of bitset is to convert a vector of binary digits into decimal format. For
example, use a loop to set the individual bits of the integer 11001101.

data = [1 1 0 0 1 1 0 1];
n = length(data);
dec = 0b0u8;
for k = 1:n
dec = bitset(dec,n+1-k,data(k));
end
dec

dec = uint8

205

dec2bin(dec)

ans =
'11001101'

Reading Consecutive Bits

Another use of bit shifting is to isolate consecutive sections of bits. For example, read the last four
bits in the 16-bit number 0110000010100000. Recall that the last four bits are on the left of the
binary representation.

n = 0b0110000010100000;
dec2bin(bitshift(n,-12))

ans =
'110'

To isolate consecutive bits in the middle of the number, you can combine the use of bit shifting with
logical masking. For example, to extract the 13th and 14th bits, you can shift the bits to the right by
12 and then mask the resulting four bits with 0011. Because the inputs to bitand must be the same
integer data type, you can specify 0011 as an unsigned 16-bit integer with 0b11u16. Without the -
u16 suffix, MATLAB stores the number as an unsigned 8-bit integer.

m = 0b11u16;
dec2bin(bitand(bitshift(n,-12),m))

ans =
'10'

Another way to read consecutive bits is with bitget, which reads specified bits from a number. You
can use colon notation to specify several consecutive bits to read. For example, read the last 8 bits of
n.

bitget(n,16:-1:8)

ans = 1×9 uint16 row vector

0 1 1 0 0 0 0 0 1

2-29
2 Program Components

Reading Nonconsecutive Bits

You can also use bitget to read bits from a number when the bits are not next to each other. For
example, read the 5th, 8th, and 14th bits from n.

bits = [14 8 5];


bitget(n,bits)

ans = 1×3 uint16 row vector

1 1 0

See Also
bitand | bitor | bitxor | bitget | bitset | bitshift | bitcmp

More About
• “Integers” on page 4-2
• “Perform Cyclic Redundancy Check” on page 2-31
• “Hexadecimal and Binary Values” on page 6-54

2-30
Perform Cyclic Redundancy Check

Perform Cyclic Redundancy Check

This example shows how to perform a cyclic redundancy check (CRC) on the bits of a number. CRCs
are used to detect errors in the transmission of data in digital systems. When a piece of data is sent, a
short check value is attached to it. The check value is obtained by polynomial division with the bits in
the data. When the data is received, the polynomial division is repeated, and the result is compared
with the check value. If the results differ, then the data was corrupted during transmission.

Calculate Check Value by Hand

Start with a 16-bit binary number, which is the message to be transmitted:

1101100111011010

To obtain the check value, divide this number by the polynomial x3 + x2 + x + 1. You can represent
this polynomial with its coefficients: 1111.

The division is performed in steps, and after each step the polynomial divisor is aligned with the left-
most 1 in the number. Because the result of dividing by the four term polynomial has three bits (in
general dividing by a polynomial of length n + 1 produces a check value of length n), append the
number with 000 to calculate the remainder. At each step, the result uses the bit-wise XOR of the four
bits being operated on, and all other bits are unchanged.

The first division is

1101100111011010 000
1111
----------------
0010100111011010 000

Each successive division operates on the result of the previous step, so the second division is

0010100111011010 000
1111
----------------
0001010111011010 000

The division is completed once the dividend is all zeros. The complete division, including the above
two steps, is

1101100111011010 000
1111
0010100111011010 000
1111
0001010111011010 000
1111
0000101111011010 000
1111
0000010011011010 000
1111
0000001101011010 000
1111
0000000010011010 000
1111
0000000001101010 000

2-31
2 Program Components

1111
0000000000010010 000
1111
0000000000001100 000
1111
0000000000000011 000
11 11
0000000000000000 110

The remainder bits, 110, are the check value for this message.

Calculate Check Value Programmatically

In MATLAB®, you can perform this same operation to obtain the check value using bit-wise
operations. First, define variables for the message and polynomial divisor. Use unsigned 32-bit
integers so that extra bits are available for the remainder.

message = 0b1101100111011010u32;
messageLength = 16;
divisor = 0b1111u32;
divisorDegree = 3;

Next, initialize the polynomial divisor. Use dec2bin to display the bits of the result.

divisor = bitshift(divisor,messageLength-divisorDegree-1);
dec2bin(divisor)

ans =
'1111000000000000'

Now, shift the divisor and message so that they have the correct number of bits (16 bits for the
message and 3 bits for the remainder).

divisor = bitshift(divisor,divisorDegree);
remainder = bitshift(message,divisorDegree);
dec2bin(divisor)

ans =
'1111000000000000000'

dec2bin(remainder)

ans =
'1101100111011010000'

Perform the division steps of the CRC using a for loop. The for loop always advances a single bit
each step, so include a check to see if the current digit is a 1. If the current digit is a 1, then the
division step is performed; otherwise, the loop advances a bit and continues.

for k = 1:messageLength
if bitget(remainder,messageLength+divisorDegree)
remainder = bitxor(remainder,divisor);
end
remainder = bitshift(remainder,1);
end

Shift the bits of the remainder to the right to get the check value for the operation.

2-32
Perform Cyclic Redundancy Check

CRC_check_value = bitshift(remainder,-messageLength);
dec2bin(CRC_check_value)

ans =
'110'

Check Message Integrity

You can use the check value to verify the integrity of a message by repeating the same division
operation. However, instead of using a remainder of 000 to start, use the check value 110. If the
message is error free, then the result of the division will be zero.

Reset the remainder variable, and add the CRC check value to the remainder bits using a bit-wise OR.
Introduce an error into the message by flipping one of the bit values with bitset.

remainder = bitshift(message,divisorDegree);
remainder = bitor(remainder,CRC_check_value);
remainder = bitset(remainder,6);
dec2bin(remainder)

ans =
'1101100111011110110'

Perform the CRC division operation and then check if the result is zero.

for k = 1:messageLength
if bitget(remainder,messageLength+divisorDegree)
remainder = bitxor(remainder,divisor);
end
remainder = bitshift(remainder,1);
end
if remainder == 0
disp('Message is error free.')
else
disp('Message contains errors.')
end

Message contains errors.

References
[1] Sklar, Bernard. Digital Communications: Fundamentals and Applications. Englewood Cliffs, NJ:
Prentice Hall, 1988.

[2] Wicker, Stephen B. Error Control Systems for Digital Communication and Storage. Upper Saddle
River, NJ: Prentice Hall, 1995.

See Also
bitshift | bitxor

More About
• “Bit-Wise Operations” on page 2-25
• “Hexadecimal and Binary Values” on page 6-54

2-33
2 Program Components

Conditional Statements

Conditional statements enable you to select at run time which block of code to execute. The simplest
conditional statement is an if statement. For example:

% Generate a random number


a = randi(100, 1);

% If it is even, divide by 2
if rem(a, 2) == 0
disp('a is even')
b = a/2;
end

if statements can include alternate choices, using the optional keywords elseif or else. For
example:

a = randi(100, 1);

if a < 30
disp('small')
elseif a < 80
disp('medium')
else
disp('large')
end

Alternatively, when you want to test for equality against a set of known values, use a switch
statement. For example:

[dayNum, dayString] = weekday(date, 'long', 'en_US');

switch dayString
case 'Monday'
disp('Start of the work week')
case 'Tuesday'
disp('Day 2')
case 'Wednesday'
disp('Day 3')
case 'Thursday'
disp('Day 4')
case 'Friday'
disp('Last day of the work week')
otherwise
disp('Weekend!')
end

For both if and switch, MATLAB executes the code corresponding to the first true condition, and
then exits the code block. Each conditional statement requires the end keyword.

In general, when you have many possible discrete, known values, switch statements are easier to
read than if statements. However, you cannot test for inequality between switch and case values.
For example, you cannot implement this type of condition with a switch:

yourNumber = input('Enter a number: ');

2-34
Conditional Statements

if yourNumber < 0
disp('Negative')
elseif yourNumber > 0
disp('Positive')
else
disp('Zero')
end

See Also
if | switch | end | return

External Websites
• Fundamentals of Programming (MathWorks Teaching Resources)

2-35
2 Program Components

Loop Control Statements

With loop control statements, you can repeatedly execute a block of code. There are two types of
loops:

• for statements loop a specific number of times, and keep track of each iteration with an
incrementing index variable.

For example, preallocate a 10-element vector, and calculate five values:

x = ones(1,10);
for n = 2:6
x(n) = 2 * x(n - 1);
end
• while statements loop as long as a condition remains true.

For example, find the first integer n for which factorial(n) is a 100-digit number:

n = 1;
nFactorial = 1;
while nFactorial < 1e100
n = n + 1;
nFactorial = nFactorial * n;
end

Each loop requires the end keyword.

It is a good idea to indent the loops for readability, especially when they are nested (that is, when one
loop contains another loop):

A = zeros(5,100);
for m = 1:5
for n = 1:100
A(m, n) = 1/(m + n - 1);
end
end

You can programmatically exit a loop using a break statement, or skip to the next iteration of a loop
using a continue statement. For example, count the number of lines in the help for the magic
function (that is, all comment lines until a blank line):

fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
line = fgetl(fid);
if isempty(line)
break
elseif ~strncmp(line,'%',1)
continue
end
count = count + 1;
end
fprintf('%d lines in MAGIC help\n',count);
fclose(fid);

2-36
Loop Control Statements

Tip If you inadvertently create an infinite loop (a loop that never ends on its own), stop execution of
the loop by pressing Ctrl+C.

See Also
for | while | break | continue | end

External Websites
• Fundamentals of Programming (MathWorks Teaching Resources)

2-37
2 Program Components

Regular Expressions

In this section...
“What Is a Regular Expression?” on page 2-38
“Steps for Building Expressions” on page 2-39
“Operators and Characters” on page 2-42

This topic describes what regular expressions are and how to use them to search text. Regular
expressions are flexible and powerful, though they use complex syntax. An alternative to regular
expressions is a pattern (since R2020b), which is simpler to define and results in code that is easier
to read. For more information, see “Build Pattern Expressions” on page 6-40.

What Is a Regular Expression?


A regular expression is a sequence of characters that defines a certain pattern. You normally use a
regular expression to search text for a group of words that matches the pattern, for example, while
parsing program input or while processing a block of text.

The character vector 'Joh?n\w*' is an example of a regular expression. It defines a pattern that
starts with the letters Jo, is optionally followed by the letter h (indicated by 'h?'), is then followed
by the letter n, and ends with any number of word characters, that is, characters that are alphabetic,
numeric, or underscore (indicated by '\w*'). This pattern matches any of the following:

Jon, John, Jonathan, Johnny

Regular expressions provide a unique way to search a volume of text for a particular subset of
characters within that text. Instead of looking for an exact character match as you would do with a
function like strfind, regular expressions give you the ability to look for a particular pattern of
characters.

For example, several ways of expressing a metric rate of speed are:

km/h
km/hr
km/hour
kilometers/hour
kilometers per hour

You could locate any of the above terms in your text by issuing five separate search commands:

strfind(text, 'km/h');
strfind(text, 'km/hour');
% etc.

To be more efficient, however, you can build a single phrase that applies to all of these search terms:

2-38
Regular Expressions

Translate this phrase into a regular expression (to be explained later in this section) and you have:

pattern = 'k(ilo)?m(eters)?(/|\sper\s)h(r|our)?';

Now locate one or more of the terms using just a single command:

text = ['The high-speed train traveled at 250 ', ...


'kilometers per hour alongside the automobile ', ...
'travelling at 120 km/h.'];
regexp(text, pattern, 'match')

ans =

1×2 cell array

{'kilometers per hour'} {'km/h'}

There are four MATLAB functions that support searching and replacing characters using regular
expressions. The first three are similar in the input values they accept and the output values they
return. For details, click the links to the function reference pages.

Function Description
regexp Match regular expression.
regexpi Match regular expression, ignoring case.
regexprep Replace part of text using regular expression.
regexptranslate Translate text into regular expression.

When calling any of the first three functions, pass the text to be parsed and the regular expression in
the first two input arguments. When calling regexprep, pass an additional input that is an
expression that specifies a pattern for the replacement.

Steps for Building Expressions


There are three steps involved in using regular expressions to search text for a particular term:

1 Identify unique patterns in the string on page 2-40

This entails breaking up the text you want to search for into groups of like character types. These
character types could be a series of lowercase letters, a dollar sign followed by three numbers
and then a decimal point, etc.
2 Express each pattern as a regular expression on page 2-41

2-39
2 Program Components

Use the metacharacters and operators described in this documentation to express each segment
of your search pattern as a regular expression. Then combine these expression segments into the
single expression to use in the search.
3 Call the appropriate search function on page 2-41

Pass the text you want to parse to one of the search functions, such as regexp or regexpi, or to
the text replacement function, regexprep.

The example shown in this section searches a record containing contact information belonging to a
group of five friends. This information includes each person's name, telephone number, place of
residence, and email address. The goal is to extract specific information from the text..

contacts = { ...
'Harry 287-625-7315 Columbus, OH [email protected]'; ...
'Janice 529-882-1759 Fresno, CA [email protected]'; ...
'Mike 793-136-0975 Richmond, VA [email protected]'; ...
'Nadine 648-427-9947 Tampa, FL [email protected]'; ...
'Jason 697-336-7728 Montrose, CO [email protected]'};

The first part of the example builds a regular expression that represents the format of a standard
email address. Using that expression, the example then searches the information for the email
address of one of the group of friends. Contact information for Janice is in row 2 of the contacts cell
array:

contacts{2}

ans =

'Janice 529-882-1759 Fresno, CA [email protected]'

Step 1 — Identify Unique Patterns in the Text

A typical email address is made up of standard components: the user's account name, followed by an
@ sign, the name of the user's internet service provider (ISP), a dot (period), and the domain to which
the ISP belongs. The table below lists these components in the left column, and generalizes the
format of each component in the right column.

Unique patterns of an email address General description of each pattern


Start with the account name One or more lowercase letters and underscores
jan_stephens . . .
Add '@' @ sign
jan_stephens@ . . .
Add the ISP One or more lowercase letters, no underscores
jan_stephens@horizon . . .
Add a dot (period) Dot (period) character
jan_stephens@horizon. . . .
Finish with the domain com or net
[email protected]

2-40
Regular Expressions

Step 2 — Express Each Pattern as a Regular Expression

In this step, you translate the general formats derived in Step 1 into segments of a regular
expression. You then add these segments together to form the entire expression.

The table below shows the generalized format descriptions of each character pattern in the left-most
column. (This was carried forward from the right column of the table in Step 1.) The second column
shows the operators or metacharacters that represent the character pattern.

Description of each segment Pattern


One or more lowercase letters and underscores [a-z_]+
@ sign @
One or more lowercase letters, no underscores [a-z]+
Dot (period) character \.
com or net (com|net)

Assembling these patterns into one character vector gives you the complete expression:
email = '[a-z_]+@[a-z]+\.(com|net)';

Step 3 — Call the Appropriate Search Function

In this step, you use the regular expression derived in Step 2 to match an email address for one of the
friends in the group. Use the regexp function to perform the search.

Here is the list of contact information shown earlier in this section. Each person's record occupies a
row of the contacts cell array:
contacts = { ...
'Harry 287-625-7315 Columbus, OH [email protected]'; ...
'Janice 529-882-1759 Fresno, CA [email protected]'; ...
'Mike 793-136-0975 Richmond, VA [email protected]'; ...
'Nadine 648-427-9947 Tampa, FL [email protected]'; ...
'Jason 697-336-7728 Montrose, CO [email protected]'};

This is the regular expression that represents an email address, as derived in Step 2:
email = '[a-z_]+@[a-z]+\.(com|net)';

Call the regexp function, passing row 2 of the contacts cell array and the email regular
expression. This returns the email address for Janice.
regexp(contacts{2}, email, 'match')

ans =

1×1 cell array

{'[email protected]'}

MATLAB parses a character vector from left to right, “consuming” the vector as it goes. If matching
characters are found, regexp records the location and resumes parsing the character vector, starting
just after the end of the most recent match.

Make the same call, but this time for the fifth person in the list:

2-41
2 Program Components

regexp(contacts{5}, email, 'match')

ans =

1×1 cell array

{'[email protected]'}

You can also search for the email address of everyone in the list by using the entire cell array for the
input argument:

regexp(contacts, email, 'match');

Operators and Characters


Regular expressions can contain characters, metacharacters, operators, tokens, and flags that specify
patterns to match, as described in these sections:

• “Metacharacters” on page 2-42


• “Character Representation” on page 2-43
• “Quantifiers” on page 2-43
• “Grouping Operators” on page 2-44
• “Anchors” on page 2-45
• “Lookaround Assertions” on page 2-45
• “Logical and Conditional Operators” on page 2-46
• “Token Operators” on page 2-47
• “Dynamic Expressions” on page 2-47
• “Comments” on page 2-48
• “Search Flags” on page 2-48

Metacharacters

Metacharacters represent letters, letter ranges, digits, and space characters. Use them to construct a
generalized pattern of characters.

Metacharacter Description Example


. Any single character, including white '..ain' matches sequences of five
space consecutive characters that end with 'ain'.
[c1c2c3] Any character contained within the '[rp.]ain' matches 'rain' or 'pain' or
square brackets. The following '.ain'.
characters are treated literally: $ | . *
+ ? and - when not used to indicate a
range.
[^c1c2c3] Any character not contained within the '[^*rp]ain' matches all four-letter
square brackets. The following sequences that end in 'ain', except 'rain'
characters are treated literally: $ | . * and 'pain' and '*ain'. For example, it
+ ? and - when not used to indicate a matches 'gain', 'lain', or 'vain'.
range.

2-42
Regular Expressions

Metacharacter Description Example


[c1-c2] Any character in the range of c1 through '[A-G]' matches a single character in the
c2 range of A through G.
\w Any alphabetic, numeric, or underscore '\w*' identifies a word comprised of any
character. For English character sets, \w grouping of alphabetic, numeric, or underscore
is equivalent to [a-zA-Z_0-9] characters.
\W Any character that is not alphabetic, '\W*' identifies a term that is not a word
numeric, or underscore. For English comprised of any grouping of alphabetic,
character sets, \W is equivalent to [^a- numeric, or underscore characters.
zA-Z_0-9]
\s Any white-space character; equivalent to '\w*n\s' matches words that end with the
[ \f\n\r\t\v] letter n, followed by a white-space character.
\S Any non-white-space character; '\d\S' matches a numeric digit followed by
equivalent to [^ \f\n\r\t\v] any non-white-space character.
\d Any numeric digit; equivalent to [0-9] '\d*' matches any number of consecutive
digits.
\D Any nondigit character; equivalent to '\w*\D\>' matches words that do not end
[^0-9] with a numeric digit.
\oN or \o{N} Character of octal value N '\o{40}' matches the space character,
defined by octal 40.
\xN or \x{N} Character of hexadecimal value N '\x2C' matches the comma character, defined
by hex 2C.

Character Representation

Operator Description
\a Alarm (beep)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\char Any character with special meaning in regular expressions that you want to match literally
(for example, use \\ to match a single backslash)

Quantifiers

Quantifiers specify the number of times a pattern must occur in the matching text.

Quantifier Number of Times Expression Occurs Example


expr* 0 or more times consecutively. '\w*' matches a word of any length.

2-43
2 Program Components

Quantifier Number of Times Expression Occurs Example


expr? 0 times or 1 time. '\w*(\.m)?' matches words that optionally
end with the extension .m.
expr+ 1 or more times consecutively. '<img src="\w+\.gif">' matches an
<img> HTML tag when the file name contains
one or more characters.
expr{m,n} At least m times, but no more than n times '\S{4,8}' matches between four and eight
consecutively. non-white-space characters.

{0,1} is equivalent to ?.
expr{m,} At least m times consecutively. '<a href="\w{1,}\.html">' matches an
<a> HTML tag when the file name contains one
{0,} and {1,} are equivalent to * and +, or more characters.
respectively.
expr{n} Exactly n times consecutively. '\d{4}' matches four consecutive digits.

Equivalent to {n,n}.

Quantifiers can appear in three modes, described in the following table. q represents any of the
quantifiers in the previous table.

Mode Description Example


exprq Greedy expression: match as many characters Given the text '<tr><td><p>text</p></
as possible. td>', the expression '</?t.*>' matches all
characters between <tr and /td>:

'<tr><td><p>text</p></td>'
exprq? Lazy expression: match as few characters as Given the text'<tr><td><p>text</p></
necessary. td>', the expression '</?t.*?>' ends each
match at the first occurrence of the closing
angle bracket (>):

'<tr>' '<td>' '</td>'


exprq+ Possessive expression: match as much as Given the text'<tr><td><p>text</p></
possible, but do not rescan any portions of the td>', the expression '</?t.*+>' does not
text. return any matches, because the closing
angle bracket is captured using .*, and is not
rescanned.

Grouping Operators

Grouping operators allow you to capture tokens, apply one operator to multiple elements, or disable
backtracking in a specific group.

2-44
Regular Expressions

Grouping Description Example


Operator
(expr) Group elements of the expression and capture 'Joh?n\s(\w*)' captures a token that
tokens. contains the last name of any person with the
first name John or Jon.
(?:expr) Group, but do not capture tokens. '(?:[aeiou][^aeiou]){2}' matches two
consecutive patterns of a vowel followed by a
nonvowel, such as 'anon'.

Without grouping, '[aeiou][^aeiou]


{2}'matches a vowel followed by two
nonvowels.
(?>expr) Group atomically. Do not backtrack within the 'A(?>.*)Z' does not match 'AtoZ',
group to complete the match, and do not although 'A(?:.*)Z' does. Using the atomic
capture tokens. group, Z is captured using .* and is not
rescanned.
(expr1|expr2) Match expression expr1 or expression '(let|tel)\w+' matches words that
expr2. contain, but do not end, with let or tel.

If there is a match with expr1, then expr2 is


ignored.

You can include ?: or ?> after the opening


parenthesis to suppress tokens or group
atomically.

Anchors

Anchors in the expression match the beginning or end of a character vector or word.

Anchor Matches the... Example


^expr Beginning of the input text. '^M\w*' matches a word starting with M at
the beginning of the text.
expr$ End of the input text. '\w*m$' matches words ending with m at the
end of the text.
\<expr Beginning of a word. '\<n\w*' matches any words starting with
n.
expr\> End of a word. '\w*e\>' matches any words ending with e.

Lookaround Assertions

Lookaround assertions look for patterns that immediately precede or follow the intended match, but
are not part of the match.

The pointer remains at the current location, and characters that correspond to the test expression
are not captured or discarded. Therefore, lookahead assertions can match overlapping character
groups.

2-45
2 Program Components

Lookaround Description Example


Assertion
expr(?=test) Look ahead for characters that match test. '\w*(?=ing)' matches terms that are
followed by ing, such as 'Fly' and 'fall'
in the input text 'Flying, not falling.'
expr(?!test) Look ahead for characters that do not 'i(?!ng)' matches instances of the letter i
match test. that are not followed by ng.
(?<=test)expr Look behind for characters that match '(?<=re)\w*' matches terms that follow
test. 're', such as 'new', 'use', and 'cycle'
in the input text 'renew, reuse,
recycle'
(?<!test)expr Look behind for characters that do not '(?<!\d)(\d)(?!\d)' matches single-
match test. digit numbers (digits that do not precede or
follow other digits).

If you specify a lookahead assertion before an expression, the operation is equivalent to a logical AND.

Operation Description Example


(?=test)expr Match both test and expr. '(?=[a-z])[^aeiou]' matches
consonants.
(?!test)expr Match expr and do not match test. '(?![aeiou])[a-z]' matches consonants.

For more information, see “Lookahead Assertions in Regular Expressions” on page 2-50.

Logical and Conditional Operators

Logical and conditional operators allow you to test the state of a given condition, and then use the
outcome to determine which pattern, if any, to match next. These operators support logical OR and if
or if/else conditions. (For AND conditions, see “Lookaround Assertions” on page 2-45.)

Conditions can be tokens on page 2-47, lookaround assertions on page 2-45, or dynamic expressions
on page 2-47 of the form (?@cmd). Dynamic expressions must return a logical or numeric value.

Conditional Operator Description Example


expr1|expr2 Match expression expr1 or expression '(let|tel)\w+' matches words that
expr2. start with let or tel.

If there is a match with expr1, then


expr2 is ignored.
(?(cond)expr) If condition cond is true, then match '(?(?@ispc)[A-Z]:\\)' matches a
expr. drive name, such as C:\, when run on a
Windows system.
(?(cond)expr1|expr2) If condition cond is true, then match 'Mr(s?)\..*?(?(1)her|his) \w*'
expr1. Otherwise, match expr2. matches text that includes her when
the text begins with Mrs, or that
includes his when the text begins with
Mr.

2-46
Regular Expressions

Token Operators

Tokens are portions of the matched text that you define by enclosing part of the regular expression in
parentheses. You can refer to a token by its sequence in the text (an ordinal token), or assign names
to tokens for easier code maintenance and readable output.

Ordinal Token Operator Description Example


(expr) Capture in a token the characters that 'Joh?n\s(\w*)' captures a token that
match the enclosed expression. contains the last name of any person
with the first name John or Jon.
\N Match the Nth token. '<(\w+).*>.*</\1>' captures tokens
for HTML tags, such as 'title' from
the text '<title>Some text</
title>'.
(?(N)expr1|expr2) If the Nth token is found, then match 'Mr(s?)\..*?(?(1)her|his) \w*'
expr1. Otherwise, match expr2. matches text that includes her when
the text begins with Mrs, or that
includes his when the text begins with
Mr.

Named Token Operator Description Example


(?<name>expr) Capture in a named token the '(?<month>\d+)-(?<day>\d+)-(?
characters that match the enclosed <yr>\d+)' creates named tokens for
expression. the month, day, and year in an input
date of the form mm-dd-yy.
\k<name> Match the token referred to by name. '<(?<tag>\w+).*>.*</\k<tag>>'
captures tokens for HTML tags, such as
'title' from the text '<title>Some
text</title>'.
(?(name)expr1|expr2) If the named token is found, then 'Mr(?<sex>s?)\..*?(?(sex)her|
match expr1. Otherwise, match his) \w*' matches text that includes
expr2. her when the text begins with Mrs, or
that includes his when the text begins
with Mr.

Note If an expression has nested parentheses, MATLAB captures tokens that correspond to the
outermost set of parentheses. For example, given the search pattern '(and(y|rew))', MATLAB
creates a token for 'andrew' but not for 'y' or 'rew'.

For more information, see “Tokens in Regular Expressions” on page 2-53.

Dynamic Expressions

Dynamic expressions allow you to execute a MATLAB command or a regular expression to determine
the text to match.

The parentheses that enclose dynamic expressions do not create a capturing group.

2-47
2 Program Components

Operator Description Example


(??expr) Parse expr and include the resulting term '^(\d+)((??\\w{$1}))' determines
in the match expression. how many characters to match by reading
a digit at the beginning of the match. The
When parsed, expr must correspond to a dynamic expression is enclosed in a
complete, valid regular expression. second set of parentheses so that the
Dynamic expressions that use the backslash resulting match is captured in a token. For
escape character (\) require two instance, matching '5XXXXX' captures
backslashes: one for the initial parsing of tokens for '5' and 'XXXXX'.
expr, and one for the complete match.
(??@cmd) Execute the MATLAB command '(.{2,}).?(??@fliplr($1))' finds
represented by cmd, and include the output palindromes that are at least four
returned by the command in the match characters long, such as 'abba'.
expression.
(?@cmd) Execute the MATLAB command '\w*?(\w)(?@disp($1))\1\w*'
represented by cmd, but discard any output matches words that include double letters
the command returns. (Helpful for (such as pp), and displays intermediate
diagnosing regular expressions.) results.

Within dynamic expressions, use the following operators to define replacement terms.

Replacement Operator Description


$& or $0 Portion of the input text that is currently a match
$` Portion of the input text that precedes the current match
$' Portion of the input text that follows the current match (use $'' to represent $')
$N Nth token
$<name> Named token
${cmd} Output returned when MATLAB executes the command, cmd

For more information, see “Dynamic Regular Expressions” on page 2-59.

Comments

The comment operator enables you to insert comments into your code to make it more maintainable.
The text of the comment is ignored by MATLAB when matching against the input text.

Characters Description Example


(?#comment) Insert a comment in the regular expression. '(?# Initial digit)\<\d\w+'
The comment text is ignored when includes a comment, and matches words
matching the input. that begin with a number.

Search Flags

Search flags modify the behavior for matching expressions.

Flag Description
(?-i) Match letter case (default for regexp and regexprep).
(?i) Do not match letter case (default for regexpi).

2-48
Regular Expressions

Flag Description
(?s) Match dot (.) in the pattern with any character (default).
(?-s) Match dot in the pattern with any character that is not a newline character.
(?-m) Match the ^ and $ metacharacters at the beginning and end of text (default).
(?m) Match the ^ and $ metacharacters at the beginning and end of a line.
(?-x) Include space characters and comments when matching (default).
(?x) Ignore space characters and comments when matching. Use '\ ' and '\#' to
match space and # characters.

The expression that the flag modifies can appear either after the parentheses, such as

(?i)\w*

or inside the parentheses and separated from the flag with a colon (:), such as

(?i:\w*)

The latter syntax allows you to change the behavior for part of a larger expression.

See Also
regexp | regexpi | regexprep | regexptranslate | pattern

More About
• “Lookahead Assertions in Regular Expressions” on page 2-50
• “Tokens in Regular Expressions” on page 2-53
• “Dynamic Regular Expressions” on page 2-59
• “Search and Replace Text” on page 6-37

2-49
2 Program Components

Lookahead Assertions in Regular Expressions


In this section...
“Lookahead Assertions” on page 2-50
“Overlapping Matches” on page 2-50
“Logical AND Conditions” on page 2-51

Lookahead Assertions
There are two types of lookaround assertions for regular expressions: lookahead and lookbehind. In
both cases, the assertion is a condition that must be satisfied to return a match to the expression.

A lookahead assertion has the form (?=test) and can appear anywhere in a regular expression.
MATLAB looks ahead of the current location in the text for the test condition. If MATLAB matches the
test condition, it continues processing the rest of the expression to find a match.

For example, look ahead in a character vector specifying a path to find the name of the folder that
contains a program file (in this case, fileread.m).

chr = which('fileread')

chr =

'matlabroot\toolbox\matlab\iofun\fileread.m'

regexp(chr,'\w+(?=\\\w+\.[mp])','match')

ans =

1×1 cell array

{'iofun'}

The match expression, \w+, searches for one or more alphanumeric or underscore characters. Each
time regexp finds a term that matches this condition, it looks ahead for a backslash (specified with
two backslashes, \\), followed by a file name (\w+) with an .m or .p extension (\.[mp]). The
regexp function returns the match that satisfies the lookahead condition, which is the folder name
iofun.

Overlapping Matches
Lookahead assertions do not consume any characters in the text. As a result, you can use them to find
overlapping character sequences.

For example, use lookahead to find every sequence of six nonwhitespace characters in a character
vector by matching initial characters that precede five additional characters:

chr = 'Locate several 6-char. phrases';


startIndex = regexpi(chr,'\S(?=\S{5})')

startIndex =

1 8 9 16 17 24 25

2-50
Lookahead Assertions in Regular Expressions

The starting indices correspond to these phrases:


Locate severa everal 6-char -char. phrase hrases

Without the lookahead operator, MATLAB parses a character vector from left to right, consuming the
vector as it goes. If matching characters are found, regexp records the location and resumes parsing
the character vector from the location of the most recent match. There is no overlapping of
characters in this process.
chr = 'Locate several 6-char. phrases';
startIndex = regexpi(chr,'\S{6}')

startIndex =

1 8 16 24

The starting indices correspond to these phrases:


Locate severa 6-char phrase

Logical AND Conditions


Another way to use a lookahead operation is to perform a logical AND between two conditions. This
example initially attempts to locate all lowercase consonants in a character array consisting of the
first 50 characters of the help for the normest function:
helptext = help('normest');
chr = helptext(1:50)

chr =

' NORMEST Estimate the matrix 2-norm.


NORMEST(S'

Merely searching for non-vowels ([^aeiou]) does not return the expected answer, as the output
includes capital letters, space characters, and punctuation:
c = regexp(chr,'[^aeiou]','match')

c =

1×43 cell array

Columns 1 through 14

{' '} {'N'} {'O'} {'R'} {'M'} {'E'} {'S'} {'T'} {' '} {'E'} {'s

Columns 15 through 28

{' '} {'t'} {'h'} {' '} {'m'} {'t'} {'r'} {'x'} {' '} {'2'} {'-

Columns 29 through 42

{'.'} {'↵'} {' '} {' '} {' '} {' '} {'N'} {'O'} {'R'} {'M'} {'E

Column 43

{'S'}

2-51
2 Program Components

Try this again, using a lookahead operator to create the following AND condition:

(lowercase letter) AND (not a vowel)

This time, the result is correct:

c = regexp(chr,'(?=[a-z])[^aeiou]','match')

c =

1×13 cell array

{'s'} {'t'} {'m'} {'t'} {'t'} {'h'} {'m'} {'t'} {'r'} {'x'} {'n

Note that when using a lookahead operator to perform an AND, you need to place the match
expression expr after the test expression test:

(?=test)expr or (?!test)expr

See Also
regexp | regexpi | regexprep

More About
• “Regular Expressions” on page 2-38

2-52
Tokens in Regular Expressions

Tokens in Regular Expressions

In this section...
“Introduction” on page 2-53
“Multiple Tokens” on page 2-55
“Unmatched Tokens” on page 2-56
“Tokens in Replacement Text” on page 2-56
“Named Capture” on page 2-57

Introduction
Parentheses used in a regular expression not only group elements of that expression together, but
also designate any matches found for that group as tokens. You can use tokens to match other parts
of the same text. One advantage of using tokens is that they remember what they matched, so you
can recall and reuse matched text in the process of searching or replacing.

Each token in the expression is assigned a number, starting from 1, going from left to right. To make
a reference to a token later in the expression, refer to it using a backslash followed by the token
number. For example, when referencing a token generated by the third set of parentheses in the
expression, use \3.

As a simple example, if you wanted to search for identical sequential letters in a character array, you
could capture the first letter as a token and then search for a matching character immediately
afterwards. In the expression shown below, the (\S) phrase creates a token whenever regexp
matches any nonwhitespace character in the character array. The second part of the expression,
'\1', looks for a second instance of the same character immediately following the first.
poe = ['While I nodded, nearly napping, ' ...
'suddenly there came a tapping,'];

[mat,tok,ext] = regexp(poe, '(\S)\1', 'match', ...


'tokens', 'tokenExtents');
mat

mat =

1×4 cell array

{'dd'} {'pp'} {'dd'} {'pp'}

The cell array tok contains cell arrays that each contain a token.
tok{:}

ans =

1×1 cell array

{'d'}

ans =

2-53
2 Program Components

1×1 cell array

{'p'}

ans =

1×1 cell array

{'d'}

ans =

1×1 cell array

{'p'}

The cell array ext contains numeric arrays that each contain starting and ending indices for a token.
ext{:}

ans =

11 11

ans =

26 26

ans =

35 35

ans =

57 57

For another example, capture pairs of matching HTML tags (e.g., <a> and </a>) and the text
between them. The expression used for this example is
expr = '<(\w+).*?>.*?</\1>';

The first part of the expression, '<(\w+)', matches an opening angle bracket (<) followed by one or
more alphabetic, numeric, or underscore characters. The enclosing parentheses capture token
characters following the opening angle bracket.

The second part of the expression, '.*?>.*?', matches the remainder of this HTML tag (characters
up to the >), and any characters that may precede the next opening angle bracket.

The last part, '</\1>', matches all characters in the ending HTML tag. This tag is composed of the
sequence </tag>, where tag is whatever characters were captured as a token.
hstr = '<!comment><a name="752507"></a><b>Default</b><br>';
expr = '<(\w+).*?>.*?</\1>';

2-54
Tokens in Regular Expressions

[mat,tok] = regexp(hstr, expr, 'match', 'tokens');


mat{:}

ans =

'<a name="752507"></a>'

ans =

'<b>Default</b>'

tok{:}

ans =

1×1 cell array

{'a'}

ans =

1×1 cell array

{'b'}

Multiple Tokens
Here is an example of how tokens are assigned values. Suppose that you are going to search the
following text:

andy ted bob jim andrew andy ted mark

You choose to search the above text with the following search pattern:

and(y|rew)|(t)e(d)

This pattern has three parenthetical expressions that generate tokens. When you finally perform the
search, the following tokens are generated for each match.

Match Token 1 Token 2


andy y
ted t d
andrew rew
andy y
ted t d

Only the highest level parentheses are used. For example, if the search pattern and(y|rew) finds the
text andrew, token 1 is assigned the value rew. However, if the search pattern (and(y|rew)) is
used, token 1 is assigned the value andrew.

2-55
2 Program Components

Unmatched Tokens
For those tokens specified in the regular expression that have no match in the text being evaluated,
regexp and regexpi return an empty character vector ('') as the token output, and an extent that
marks the position in the string where the token was expected.

The example shown here executes regexp on a character vector specifying the path returned from
the MATLAB tempdir function. The regular expression expr includes six token specifiers, one for
each piece of the path. The third specifier [a-z]+ has no match in the character vector because this
part of the path, Profiles, begins with an uppercase letter:

chr = tempdir

chr =

'C:\WINNT\Profiles\bpascal\LOCALS~1\Temp\'

expr = ['([A-Z]:)\\(WINNT)\\([a-z]+)?.*\\' ...


'([a-z]+)\\([A-Z]+~\d)\\(Temp)\\'];

[tok, ext] = regexp(chr, expr, 'tokens', 'tokenExtents');

When a token is not found in the text, regexp returns an empty character vector ('') as the token
and a numeric array with the token extent. The first number of the extent is the string index that
marks where the token was expected, and the second number of the extent is equal to one less than
the first.

In the case of this example, the empty token is the third specified in the expression, so the third token
returned is empty:

tok{:}

ans =

1×6 cell array

{'C:'} {'WINNT'} {0×0 char} {'bpascal'} {'LOCALS~1'} {'Temp'}

The third token extent returned in the variable ext has the starting index set to 10, which is where
the nonmatching term, Profiles, begins in the path. The ending extent index is set to one less than
the starting index, or 9:

ext{:}

ans =

1 2
4 8
10 9
19 25
27 34
36 39

Tokens in Replacement Text

2-56
Tokens in Regular Expressions

When using tokens in replacement text, reference them using $1, $2, etc. instead of \1, \2, etc. This
example captures two tokens and reverses their order. The first, $1, is 'Norma Jean' and the
second, $2, is 'Baker'. Note that regexprep returns the modified text, not a vector of starting
indices.

regexprep('Norma Jean Baker', '(\w+\s\w+)\s(\w+)', '$2, $1')

ans =

'Baker, Norma Jean'

Named Capture

If you use a lot of tokens in your expressions, it may be helpful to assign them names rather than
having to keep track of which token number is assigned to which token.

When referencing a named token within the expression, use the syntax \k<name> instead of the
numeric \1, \2, etc.:

poe = ['While I nodded, nearly napping, ' ...


'suddenly there came a tapping,'];

regexp(poe, '(?<anychar>.)\k<anychar>', 'match')

ans =

1×4 cell array

{'dd'} {'pp'} {'dd'} {'pp'}

Named tokens can also be useful in labeling the output from the MATLAB regular expression
functions. This is especially true when you are processing many pieces of text.

For example, parse different parts of street addresses from several character vectors. A short name is
assigned to each token in the expression:

chr1 = '134 Main Street, Boulder, CO, 14923';


chr2 = '26 Walnut Road, Topeka, KA, 25384';
chr3 = '847 Industrial Drive, Elizabeth, NJ, 73548';

p1 = '(?<adrs>\d+\s\S+\s(Road|Street|Avenue|Drive))';
p2 = '(?<city>[A-Z][a-z]+)';
p3 = '(?<state>[A-Z]{2})';
p4 = '(?<zip>\d{5})';

expr = [p1 ', ' p2 ', ' p3 ', ' p4];

As the following results demonstrate, you can make your output easier to work with by using named
tokens:

loc1 = regexp(chr1, expr, 'names')

loc1 =

struct with fields:

2-57
2 Program Components

adrs: '134 Main Street'


city: 'Boulder'
state: 'CO'
zip: '14923'

loc2 = regexp(chr2, expr, 'names')

loc2 =

struct with fields:

adrs: '26 Walnut Road'


city: 'Topeka'
state: 'KA'
zip: '25384'

loc3 = regexp(chr3, expr, 'names')

loc3 =

struct with fields:

adrs: '847 Industrial Drive'


city: 'Elizabeth'
state: 'NJ'
zip: '73548'

See Also
regexp | regexpi | regexprep

More About
• “Regular Expressions” on page 2-38

2-58
Dynamic Regular Expressions

Dynamic Regular Expressions

In this section...
“Introduction” on page 2-59
“Dynamic Match Expressions — (??expr)” on page 2-60
“Commands That Modify the Match Expression — (??@cmd)” on page 2-60
“Commands That Serve a Functional Purpose — (?@cmd)” on page 2-61
“Commands in Replacement Expressions — ${cmd}” on page 2-63

Introduction
In a dynamic expression, you can make the pattern that you want regexp to match dependent on the
content of the input text. In this way, you can more closely match varying input patterns in the text
being parsed. You can also use dynamic expressions in replacement terms for use with the
regexprep function. This gives you the ability to adapt the replacement text to the parsed input.

You can include any number of dynamic expressions in the match_expr or replace_expr
arguments of these commands:

regexp(text, match_expr)
regexpi(text, match_expr)
regexprep(text, match_expr, replace_expr)

As an example of a dynamic expression, the following regexprep command correctly replaces the
term internationalization with its abbreviated form, i18n. However, to use it on a different
term such as globalization, you have to use a different replacement expression:

match_expr = '(^\w)(\w*)(\w$)';

replace_expr1 = '$118$3';
regexprep('internationalization', match_expr, replace_expr1)

ans =

'i18n'

replace_expr2 = '$111$3';
regexprep('globalization', match_expr, replace_expr2)

ans =

'g11n'

Using a dynamic expression ${num2str(length($2))} enables you to base the replacement


expression on the input text so that you do not have to change the expression each time. This
example uses the dynamic replacement syntax ${cmd}.

match_expr = '(^\w)(\w*)(\w$)';
replace_expr = '$1${num2str(length($2))}$3';

regexprep('internationalization', match_expr, replace_expr)

2-59
2 Program Components

ans =

'i18n'

regexprep('globalization', match_expr, replace_expr)

ans =

'g11n'

When parsed, a dynamic expression must correspond to a complete, valid regular expression. In
addition, dynamic match expressions that use the backslash escape character (\) require two
backslashes: one for the initial parsing of the expression, and one for the complete match. The
parentheses that enclose dynamic expressions do not create a capturing group.

There are three forms of dynamic expressions that you can use in match expressions, and one form
for replacement expressions, as described in the following sections

Dynamic Match Expressions — (??expr)


The (??expr) operator parses expression expr, and inserts the results back into the match
expression. MATLAB then evaluates the modified match expression.

Here is an example of the type of expression that you can use with this operator:
chr = {'5XXXXX', '8XXXXXXXX', '1X'};
regexp(chr, '^(\d+)(??X{$1})$', 'match', 'once');

The purpose of this particular command is to locate a series of X characters in each of the character
vectors stored in the input cell array. Note however that the number of Xs varies in each character
vector. If the count did not vary, you could use the expression X{n} to indicate that you want to match
n of these characters. But, a constant value of n does not work in this case.

The solution used here is to capture the leading count number (e.g., the 5 in the first character vector
of the cell array) in a token, and then to use that count in a dynamic expression. The dynamic
expression in this example is (??X{$1}), where $1 is the value captured by the token \d+. The
operator {$1} makes a quantifier of that token value. Because the expression is dynamic, the same
pattern works on all three of the input vectors in the cell array. With the first input character vector,
regexp looks for five X characters; with the second, it looks for eight, and with the third, it looks for
just one:
regexp(chr, '^(\d+)(??X{$1})$', 'match', 'once')

ans =

1×3 cell array

{'5XXXXX'} {'8XXXXXXXX'} {'1X'}

Commands That Modify the Match Expression — (??@cmd)


MATLAB uses the (??@cmd) operator to include the results of a MATLAB command in the match
expression. This command must return a term that can be used within the match expression.

For example, use the dynamic expression (??@fliplr($1)) to locate a palindrome, “Never Odd or
Even”, that has been embedded into a larger character vector.

2-60
Dynamic Regular Expressions

First, create the input string. Make sure that all letters are lowercase, and remove all nonword
characters.

chr = lower(...
'Find the palindrome Never Odd or Even in this string');

chr = regexprep(chr, '\W*', '')

chr =

'findthepalindromeneveroddoreveninthisstring'

Locate the palindrome within the character vector using the dynamic expression:

palindrome = regexp(chr, '(.{3,}).?(??@fliplr($1))', 'match')

palindrome =

1×1 cell array

{'neveroddoreven'}

The dynamic expression reverses the order of the letters that make up the character vector, and then
attempts to match as much of the reversed-order vector as possible. This requires a dynamic
expression because the value for $1 relies on the value of the token (.{3,}).

Dynamic expressions in MATLAB have access to the currently active workspace. This means that you
can change any of the functions or variables used in a dynamic expression just by changing variables
in the workspace. Repeat the last command of the example above, but this time define the function to
be called within the expression using a function handle stored in the base workspace:

fun = @fliplr;

palindrome = regexp(chr, '(.{3,}).?(??@fun($1))', 'match')

palindrome =

1×1 cell array

{'neveroddoreven'}

Commands That Serve a Functional Purpose — (?@cmd)


The (?@cmd) operator specifies a MATLAB command that regexp or regexprep is to run while
parsing the overall match expression. Unlike the other dynamic expressions in MATLAB, this operator
does not alter the contents of the expression it is used in. Instead, you can use this functionality to
get MATLAB to report just what steps it is taking as it parses the contents of one of your regular
expressions. This functionality can be useful in diagnosing your regular expressions.

The following example parses a word for zero or more characters followed by two identical
characters followed again by zero or more characters:

regexp('mississippi', '\w*(\w)\1\w*', 'match')

ans =

1×1 cell array

2-61
2 Program Components

{'mississippi'}

To track the exact steps that MATLAB takes in determining the match, the example inserts a short
script (?@disp($1)) in the expression to display the characters that finally constitute the match.
Because the example uses greedy quantifiers, MATLAB attempts to match as much of the character
vector as possible. So, even though MATLAB finds a match toward the beginning of the string, it
continues to look for more matches until it arrives at the very end of the string. From there, it backs
up through the letters i then p and the next p, stopping at that point because the match is finally
satisfied:
regexp('mississippi', '\w*(\w)(?@disp($1))\1\w*', 'match')

i
p
p

ans =

1×1 cell array

{'mississippi'}

Now try the same example again, this time making the first quantifier lazy (*?). Again, MATLAB
makes the same match:
regexp('mississippi', '\w*?(\w)\1\w*', 'match')

ans =

1×1 cell array

{'mississippi'}

But by inserting a dynamic script, you can see that this time, MATLAB has matched the text quite
differently. In this case, MATLAB uses the very first match it can find, and does not even consider the
rest of the text:
regexp('mississippi', '\w*?(\w)(?@disp($1))\1\w*', 'match')

m
i
s

ans =

1×1 cell array

{'mississippi'}

To demonstrate how versatile this type of dynamic expression can be, consider the next example that
progressively assembles a cell array as MATLAB iteratively parses the input text. The (?!) operator
found at the end of the expression is actually an empty lookahead operator, and forces a failure at
each iteration. This forced failure is necessary if you want to trace the steps that MATLAB is taking to
resolve the expression.

MATLAB makes a number of passes through the input text, each time trying another combination of
letters to see if a fit better than last match can be found. On any passes in which no matches are

2-62
Dynamic Regular Expressions

found, the test results in an empty character vector. The dynamic script (?@if(~isempty($&)))
serves to omit the empty character vectors from the matches cell array:

matches = {};
expr = ['(Euler\s)?(Cauchy\s)?(Boole)?(?@if(~isempty($&)),' ...
'matches{end+1}=$&;end)(?!)'];

regexp('Euler Cauchy Boole', expr);

matches

matches =

1×6 cell array

{'Euler Cauchy Bo…'} {'Euler Cauchy '} {'Euler '} {'Cauchy Boole'} {'Cauchy '}

The operators $& (or the equivalent $0), $`, and $' refer to that part of the input text that is
currently a match, all characters that precede the current match, and all characters to follow the
current match, respectively. These operators are sometimes useful when working with dynamic
expressions, particularly those that employ the (?@cmd) operator.

This example parses the input text looking for the letter g. At each iteration through the text, regexp
compares the current character with g, and not finding it, advances to the next character. The
example tracks the progress of scan through the text by marking the current location being parsed
with a ^ character.

(The $` and $´ operators capture that part of the text that precedes and follows the current parsing
location. You need two single-quotation marks ($'') to express the sequence $´ when it appears
within text.)

chr = 'abcdefghij';
expr = '(?@disp(sprintf(''starting match: [%s^%s]'',$`,$'')))g';

regexp(chr, expr, 'once');

starting match: [^abcdefghij]


starting match: [a^bcdefghij]
starting match: [ab^cdefghij]
starting match: [abc^defghij]
starting match: [abcd^efghij]
starting match: [abcde^fghij]
starting match: [abcdef^ghij]

Commands in Replacement Expressions — ${cmd}


The ${cmd} operator modifies the contents of a regular expression replacement pattern, making this
pattern adaptable to parameters in the input text that might vary from one use to the next. As with
the other dynamic expressions used in MATLAB, you can include any number of these expressions
within the overall replacement expression.

Commands in replacement expressions only check the local workspace for variables. Caller and
global workspaces are not available to commands in replacement expressions

In the regexprep call shown here, the replacement pattern is '${convertMe($1,$2)}'. In this
case, the entire replacement pattern is a dynamic expression:

2-63
2 Program Components

regexprep('This highway is 125 miles long', ...


'(\d+\.?\d*)\W(\w+)', '${convertMe($1,$2)}');

The dynamic expression tells MATLAB to execute a function named convertMe using the two tokens
(\d+\.?\d*) and (\w+), derived from the text being matched, as input arguments in the call to
convertMe. The replacement pattern requires a dynamic expression because the values of $1 and $2
are generated at runtime.

The following example defines the file named convertMe that converts measurements from imperial
units to metric.

function valout = convertMe(valin, units)


switch(units)
case 'inches'
fun = @(in)in .* 2.54;
uout = 'centimeters';
case 'miles'
fun = @(mi)mi .* 1.6093;
uout = 'kilometers';
case 'pounds'
fun = @(lb)lb .* 0.4536;
uout = 'kilograms';
case 'pints'
fun = @(pt)pt .* 0.4731;
uout = 'litres';
case 'ounces'
fun = @(oz)oz .* 28.35;
uout = 'grams';
end
val = fun(str2num(valin));
valout = [num2str(val) ' ' uout];
end

At the command line, call the convertMe function from regexprep, passing in values for the
quantity to be converted and name of the imperial unit:

regexprep('This highway is 125 miles long', ...


'(\d+\.?\d*)\W(\w+)', '${convertMe($1,$2)}')

ans =

'This highway is 201.1625 kilometers long'

regexprep('This pitcher holds 2.5 pints of water', ...


'(\d+\.?\d*)\W(\w+)', '${convertMe($1,$2)}')

ans =

'This pitcher holds 1.1828 litres of water'

regexprep('This stone weighs about 10 pounds', ...


'(\d+\.?\d*)\W(\w+)', '${convertMe($1,$2)}')

ans =

'This stone weighs about 4.536 kilograms'

2-64
Dynamic Regular Expressions

As with the (??@ ) operator discussed in an earlier section, the ${ } operator has access to
variables in the currently active workspace. The following regexprep command uses the array A
defined in the base workspace:

A = magic(3)

A =

8 1 6
3 5 7
4 9 2

regexprep('The columns of matrix _nam are _val', ...


{'_nam', '_val'}, ...
{'A', '${sprintf(''%d%d%d '', A)}'})

ans =

'The columns of matrix A are 834 159 672'

See Also
regexp | regexpi | regexprep

More About
• “Regular Expressions” on page 2-38

2-65
2 Program Components

Comma-Separated Lists

In this section...
“What Is a Comma-Separated List?” on page 2-66
“Generating a Comma-Separated List” on page 2-66
“Assigning Output from a Comma-Separated List” on page 2-68
“Assigning to a Comma-Separated List” on page 2-68
“How to Use Comma-Separated Lists” on page 2-69
“Fast Fourier Transform Example” on page 2-72
“Troubleshooting Operations with Comma-Separated Lists” on page 2-72

What Is a Comma-Separated List?


When you type in a series of numbers separated by commas, MATLAB creates a comma-separated list
and returns each value individually.

1,2,3

ans =

ans =

ans =

When used with large and more complex data structures like MATLAB structures and cell arrays,
comma-separated lists can help simplify your code.

Generating a Comma-Separated List


You can generate a comma-separated list from either a cell array or a MATLAB structure.

Generating a List from a Cell Array

When you extract multiple elements from a cell array, the result is a comma-separated list. Define a 4-
by-6 cell array.

C = cell(4,6);
for k = 1:24
C{k} = k*2;
end
C

2-66
Comma-Separated Lists

C =

4×6 cell array

{[2]} {[10]} {[18]} {[26]} {[34]} {[42]}


{[4]} {[12]} {[20]} {[28]} {[36]} {[44]}
{[6]} {[14]} {[22]} {[30]} {[38]} {[46]}
{[8]} {[16]} {[24]} {[32]} {[40]} {[48]}

Extract the fifth column to generate a comma-separated list.

C{:,5}

ans =

34

ans =

36

ans =

38

ans =

40

This is the same as explicitly typing the list.

C{1,5},C{2,5},C{3,5},C{4,5}

Generating a List from a Structure

When you extract a field of a structure array across one of its dimensions, the result is a comma-
separated list.

Start by converting the cell array used above into a 4-by-1 MATLAB structure with six fields: f1
through f6. Read field f5 for all rows, and MATLAB returns a comma-separated list.

S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
S.f5

ans =

34

ans =

36

ans =

2-67
2 Program Components

38

ans =

40

This is the same as explicitly typing the list.

S(1).f5,S(2).f5,S(3).f5,S(4).f5

Assigning Output from a Comma-Separated List


You can assign any or all consecutive elements of a comma-separated list to variables with a simple
assignment statement. Define the cell array Cand assign the first row to variables c1 through c6.

C = cell(4,6);
for k = 1:24
C{k} = k*2;
end
[c1,c2,c3,c4,c5,c6] = C{1,1:6};
c5

c5 =

34

When you specify fewer output variables than the number of outputs returned by the expression,
MATLAB assigns the first N outputs to those N variables and ignores any remaining outputs. In this
example, MATLAB assigns C{1,1:3} to the variables c1, c2, and c3 and ignores C{1,4:6}.

[c1,c2,c3] = C{1,1:6};

You can assign structure outputs in the same manner.

S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
[sf1,sf2,sf3] = S.f5;
sf3

sf3 =

38

You also can use the deal function for this purpose.

Assigning to a Comma-Separated List


The simplest way to assign multiple values to a comma-separated list is to use the deal function. This
function distributes its input arguments to the elements of a comma-separated list.

This example uses deal to overwrite each element in a comma-separated list. First initialize a two-
element list. This step is necessary because you cannot use comma-separated list assignment with an
undefined variable when using : as an index. See “Comma-Separated List Assignment to an
Undefined Variable” on page 2-75 for more information.

2-68
Comma-Separated Lists

c{1} = [];
c{2} = [];
c{:}

ans =

[]

ans =

[]

Use deal to overwrite each element in the list.


[c{:}] = deal([10 20],[14 12]);
c{:}

ans =

10 20

ans =

14 12

This example works in the same way, but with a comma-separated list of vectors in a structure field.
s(1).field1 = [[]];
s(2).field1 = [[]];
s.field1

ans =

[]

ans =

[]

Use deal to overwrite the structure fields.


[s.field1] = deal([10 20],[14 12]);
s.field1

ans =

10 20

ans =

14 12

How to Use Comma-Separated Lists


Common uses for comma-separated lists are:

2-69
2 Program Components

• “Constructing Arrays” on page 2-70


• “Displaying Arrays” on page 2-70
• “Concatenation” on page 2-71
• “Function Call Arguments” on page 2-71
• “Function Return Values” on page 2-71

These sections provide examples of using comma-separated lists with cell arrays. Each of these
examples applies to structures as well.

Constructing Arrays

You can use a comma-separated list to enter a series of elements when constructing a matrix or array.
When you specify a list of elements with C{:,5}, MATLAB inserts the four individual elements.
C = cell(4,6);
for k = 1:24
C{k} = k*2;
end
A = {'Hello',C{:,5},magic(4)}

A =

1×6 cell array

{'Hello'} {[34]} {[36]} {[38]} {[40]} {4×4 double}

When you specify the C cell itself, MATLAB inserts the entire cell array.
A = {'Hello',C,magic(4)}

A =

1×3 cell array

{'Hello'} {4×6 cell} {4×4 double}

Displaying Arrays

Use a list to display all or part of a structure or cell array.


A{:}

ans =

'Hello'

ans =

4×6 cell array

{[2]} {[10]} {[18]} {[26]} {[34]} {[42]}


{[4]} {[12]} {[20]} {[28]} {[36]} {[44]}
{[6]} {[14]} {[22]} {[30]} {[38]} {[46]}
{[8]} {[16]} {[24]} {[32]} {[40]} {[48]}

2-70
Comma-Separated Lists

ans =

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

Concatenation

Putting a comma-separated list inside square brackets extracts the specified elements from the list
and concatenates them.

A = [C{:,5:6}]

A =

34 36 38 40 42 44 46 48

Function Call Arguments

When writing the code for a function call, you enter the input arguments as a list with each argument
separated by a comma. If you have these arguments stored in a structure or cell array, then you can
generate all or part of the argument list from the structure or cell array instead. This can be
especially useful when passing in variable numbers of arguments.

This example passes several name-value arguments to the plot function.

X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));
C = cell(2,3);
C{1,1} = 'LineWidth';
C{2,1} = 2;
C{1,2} = 'MarkerEdgeColor';
C{2,2} = 'k';
C{1,3} = 'MarkerFaceColor';
C{2,3} = 'g';
figure
plot(X,Y,'--rs',C{:})

Function Return Values

MATLAB functions can also return more than one value to the caller. These values are returned in a
list with each value separated by a comma. Instead of listing each return value, you can use a comma-
separated list with a structure or cell array. This becomes more useful for functions that have variable
numbers of return values.

This example returns three values to a cell array.

C = cell(1,3);
[C{:}] = fileparts('work/mytests/strArrays.mat')

C =

1×3 cell array

{'work/mytests'} {'strArrays'} {'.mat'}

2-71
2 Program Components

Fast Fourier Transform Example


The fftshift function swaps the left and right halves of each dimension of an array. For the vector
[0 2 4 6 8 10], the output is [6 8 10 0 2 4]. For a multidimensional array, fftshift
performs this swap along each dimension.

fftshift uses vectors of indices to perform the swap. For the vector shown above, the index [1 2
3 4 5 6] is rearranged to form a new index [4 5 6 1 2 3]. The function then uses this index
vector to reposition the elements. For a multidimensional array, fftshift constructs an index vector
for each dimension. A comma-separated list makes this task much simpler.

Here is the fftshift function.

function y = fftshift(x)
numDims = ndims(x);
idx = cell(1,numDims);
for k = 1:numDims
m = size(x,k);
p = ceil(m/2);
idx{k} = [p+1:m 1:p];
end
y = x(idx{:});
end

The function stores the index vectors in cell array idx. Building this cell array is relatively simple.
For each of the N dimensions, determine the size of that dimension and find the integer index nearest
the midpoint. Then, construct a vector that swaps the two halves of that dimension.

By using a cell array to store the index vectors and a comma-separated list for the indexing operation,
fftshift shifts arrays of any dimension using just a single operation: y = x(idx{:}). If you use
explicit indexing, you need to write one if statement for each dimension you want the function to
handle.

if ndims(x) == 1
y = x(index1);
else if ndims(x) == 2
y = x(index1,index2);
end
end

Another way to handle this without a comma-separated list is to loop over each dimension, converting
one dimension at a time and moving data each time. With a comma-separated list, you move the data
just once. A comma-separated list makes it easy to generalize the swapping operation to any number
of dimensions.

Troubleshooting Operations with Comma-Separated Lists


Some common MATLAB operations and indexing techniques do not work directly on comma-
separated lists. This section details several errors you can encounter when working with comma-
separated lists and explains how to resolve the underlying issues.

Intermediate Indexing Produced a Comma-Separated List

2-72
Comma-Separated Lists

Compound indexing expressions with braces or dots can produce comma-separated lists. You must
index into the individual elements of the list to access them.

For example, create a 1-by-2 cell array that contains two 3-by-3 matrices of doubles.

A = {magic(3),rand(3)}

A =

1×2 cell array

{3×3 double} {3×3 double}

Use brace indexing to display both elements.

A{1,:}

ans =

8 1 6
3 5 7
4 9 2

ans =

0.7922 0.0357 0.6787


0.9595 0.8491 0.7577
0.6557 0.9340 0.7431

Indexing into A this way produces a comma-separated list that includes both matrices contained by
the cell array. You cannot use parentheses indexing to retrieve the entries at (1,2) in both matrices
in the list.

A{1,:}(1,2)

Intermediate brace '{}' indexing produced a comma-separated list with


2 values, but it must produce a single value when followed by
subsequent indexing operations.

To retrieve the entries at (1,2) in both of the matrices in the cell array, index into the cells
individually.

A{1,1}(1,2)
A{1,2}(1,2)

ans =

ans =

0.0357

Expression Produced a Comma-Separated List Instead of a Single Value

2-73
2 Program Components

Arguments for conditional statements, logical operators, loops, and switch statements cannot be
comma-separated lists. For example, you cannot directly loop through the contents of a comma-
separated list using a for loop.

Create a cell array of the first three prime numbers.

A = cell(1,3);
A{1} = 2;
A{2} = 3;
A{3} = 5;

A{:} produces a comma-separated list of the three values.

A{:}

ans =

ans =

ans =

Using for to loop through the comma-separated list generated by A{:} errors.

for c = A{:}
disp(c)
end

A brace '{}' indexing expression produced a comma-separated list with


3 values where only a single value is allowed.

To loop over the contents of A, enclose A{:} in square brackets to concatenate the values into a
vector.

for c = [A{:}]
disp(c)
end

Assigning Multiple Elements Using Simple Assignment

Unlike with arrays, using simple assignment to assign values to multiple elements of a comma-
separated list errors. For example, define a 2-by-3 cell array.

B = cell(2,3);

2-74
Comma-Separated Lists

Assigning a value of 5 to all cells of the array using : as an index for B errors.

B{:} = 5

Assigning to 6 elements using a simple assignment statement is not


supported. Consider using comma-separated list assignment.

One way to accomplish this assignment is to enclose B{:} in square brackets and use the deal
function.

[B{:}] = deal(5)

B =

2×3 cell array

{[5]} {[5]} {[5]}


{[5]} {[5]} {[5]}

Comma-Separated List Assignment to an Undefined Variable

You cannot assign a comma-separated list to an undefined variable using : as an index. In the
example in “Assigning to a Comma-Separated List” on page 2-68, the variable x is defined as a
comma-separated list with explicit indices before assigning new values to it using : as an index.

x{1} = [];
x{2} = [];
[x{:}] = deal([10 20],[14 12]);
x{:}

ans =

10 20

ans =

14 12

Performing the same assignment with a variable that has not been initialized errors.

[y{:}] = deal([10 20],[14 12]);

Comma-separated list assignment to a nonexistent variable is not


supported when any index is a colon (:). Use explicit indices or
define the variable before performing the assignment.

To solve this problem, initialize y in the same way as x, or create y using enough explicit indices to
accommodate the number of values produced by the deal function.

[y{1:2}] = deal([10 20],[14 12])

y =

1×2 cell array

{[10 20]} {[14 12]}

2-75
2 Program Components

See Also
cell | deal | struct

2-76
Alternatives to the eval Function

Alternatives to the eval Function


In this section...
“Why Avoid the eval Function?” on page 2-77
“Variables with Sequential Names” on page 2-77
“Files with Sequential Names” on page 2-78
“Function Names in Variables” on page 2-78
“Field Names in Variables” on page 2-79
“Error Handling” on page 2-79

Why Avoid the eval Function?


Although the eval function is very powerful and flexible, it is not always the best solution to a
programming problem. Code that calls eval is often less efficient and more difficult to read and
debug than code that uses other functions or language constructs. For example:

• MATLAB compiles code the first time you run it to enhance performance for future runs. However,
because code in an eval statement can change at run time, it is not compiled.
• Code within an eval statement can unexpectedly create or assign to a variable already in the
current workspace, overwriting existing data.
• Concatenated character vectors within an eval statement are often difficult to read. Other
language constructs can simplify the syntax in your code.

For many common uses of eval, there are preferred alternate approaches, as shown in the following
examples.

Variables with Sequential Names


A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this
approach does not use the array processing power of MATLAB and is not recommended. The
preferred method is to store related data in a single array. If the data sets are of different types or
sizes, use a structure or cell array.

For example, create a cell array that contains 10 elements, where each element is a numeric array:

numArrays = 10;
A = cell(numArrays,1);
for n = 1:numArrays
A{n} = magic(n);
end

Access the data in the cell array by indexing with curly braces. For example, display the fifth element
of A:

A{5}

ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22

2-77
2 Program Components

10 12 19 21 3
11 18 25 2 9

The assignment statement A{n} = magic(n) is more elegant and efficient than this call to eval:
eval(['A', int2str(n),' = magic(n)']) % Not recommended

For more information, see:

• “Create Cell Array” on page 12-2


• “Structure Arrays” on page 11-2

Files with Sequential Names


Related data files often have a common root name with an integer index, such as myfile1.mat
through myfileN.mat. A common (but not recommended) use of the eval function is to construct
and pass each file name to a function using command syntax, such as
eval(['save myfile',int2str(n),'.mat']) % Not recommended

The best practice is to use function syntax, which allows you to pass variables as inputs. For example:
currentFile = 'myfile1.mat';
save(currentFile)

You can construct file names within a loop using the sprintf function (which is usually more
efficient than int2str), and then call the save function without eval. This code creates 10 files in
the current folder:
numFiles = 10;
for n = 1:numFiles
randomData = rand(n);
currentFile = sprintf('myfile%d.mat',n);
save(currentFile,'randomData')
end

For more information, see:

• “Choose Command Syntax or Function Syntax” on page 1-8


• “Import or Export a Sequence of Files”

Function Names in Variables


A common use of eval is to execute a function when the name of the function is in a variable
character vector. There are two ways to evaluate functions from variables that are more efficient than
using eval:

• Create function handles with the @ symbol or with the str2func function. For example, run a
function from a list stored in a cell array:
examples = {@odedemo,@sunspots,@fitdemo};
n = input('Select an example (1, 2, or 3): ');
examples{n}()
• Use the feval function. For example, call a plot function (such as plot, bar, or pie) with data
that you specify at run time:

2-78
Alternatives to the eval Function

plotFunction = input('Specify a plotting function: ','s');


data = input('Enter data to plot: ');
feval(plotFunction,data)

Field Names in Variables


Access data in a structure with a variable field name by enclosing the expression for the field in
parentheses. For example:

myData.height = [67, 72, 58];


myData.weight = [140, 205, 90];

fieldName = input('Select data (height or weight): ','s');


dataToUse = myData.(fieldName);

If you enter weight at the input prompt, then you can find the minimum weight value with the
following command.

min(dataToUse)

ans =
90

For an additional example, see “Generate Field Names from Variables” on page 11-9.

Error Handling
The preferred method for error handling in MATLAB is to use a try, catch statement. For example:

try
B = A;
catch exception
disp('A is undefined')
end

If your workspace does not contain variable A, then this code returns:

A is undefined

Previous versions of the documentation for the eval function include the syntax
eval(expression,catch_expr). If evaluating the expression input returns an error, then eval
evaluates catch_expr. However, an explicit try/catch is significantly clearer than an implicit
catch in an eval statement. Using the implicit catch is not recommended.

2-79
Classes (Data Types)
3

Overview of MATLAB Classes

• “Fundamental MATLAB Classes” on page 3-2


• “Use is* Functions to Detect State” on page 3-7
3 Overview of MATLAB Classes

Fundamental MATLAB Classes

There are many different data types, or classes, that you can work with in MATLAB. You can build
matrices and arrays of floating-point and integer data, characters and strings, logical true and
false values, and so on. Function handles connect your code with any MATLAB function regardless
of the current scope. Tables, timetables, structures, and cell arrays provide a way to store dissimilar
types of data in the same container.

There are 17 fundamental classes in MATLAB. Each of these classes is in the form of a matrix or
array. With the exceptions of function handles and tables, this matrix or array is a minimum of 0-by-0
in size and can grow to an n-dimensional array of any size. A function handle is always scalar (1-by-1).
A table always has m rows and n variables, where m >= 0 and n >= 0.

The fundamental MATLAB classes are shown in the diagram.

3-2
Fundamental MATLAB Classes

Matrix or Array
(full or sparse) Scalar

function
logical string char numeric table cell struct handle (@)

str c h

int8, uint8, single double


int16,uint16,
int32,uint32,
int64,uint64

3-3
3 Overview of MATLAB Classes

Numeric classes in the MATLAB software include signed and unsigned integers, and single- and
double-precision floating-point numbers. By default, MATLAB stores all numeric values as double-
precision floating point. (You cannot change the default type and precision.) You can choose to store
any number, or array of numbers, as integers or as single-precision. Integer and single-precision
arrays offer more memory-efficient storage than double-precision.

All numeric types support basic array operations, such as subscripting, reshaping, and mathematical
operations.

You can create two-dimensional double and logical matrices using one of two storage formats:
full or sparse. For matrices with mostly zero-valued elements, a sparse matrix requires a fraction
of the storage space required for an equivalent full matrix. Sparse matrices invoke methods
especially tailored to solve sparse problems.

These classes require different amounts of storage, the smallest being a logical value or 8-bit
integer which requires only 1 byte. It is important to keep this minimum size in mind if you work on
data in files that were written using a precision smaller than 8 bits.

Tables and timetables also support mathematical operations as long as all their variables have
numeric data types. For more information, see “Direct Calculations on Tables and Timetables” on
page 9-54.

The following table describes the fundamental classes in more detail.

Class Name Documentation Intended Use


double, single Floating-Point • Required for fractional numeric data.
Numbers on page 4- • Double- and single-precision floating-point numbers on page 4-
7 7.
• Use realmin and realmax to show range of values on page 4-
8.
• Two-dimensional arrays can be sparse.
• double is the default numeric type in MATLAB.
int8, uint8, Integers on page 4- • Use for signed and unsigned whole numbers.
int16, uint16, 2 • More efficient use of memory. on page 31-2
int32, uint32,
int64, uint64 • Use intmin and intmax to show range of values on page 4-
4.
• Choose from 4 sizes (8, 16, 32, and 64 bits).
string, char “Characters and • Data types for text.
Strings” • Both data types store characters as Unicode® characters.
• Support conversions to and from numeric representations.
• Use either data type with regular expressions on page 2-38.
• To search for and match text in strings, use pattern objects.
• To store multiple strings, use string arrays. You can also store
multiple character vectors in cell arrays. However, the
recommended way to store text is to use string arrays.

3-4
Fundamental MATLAB Classes

Class Name Documentation Intended Use


logical “Logical (Boolean) • Use in relational conditions or to test state.
Operations” • Can have one of two values: true or false.
• Also useful in array indexing.
• Two-dimensional arrays can be sparse.
function_handle “Function Handles” • Pointer to a function.
• Enables passing a function to another function.
• Can also call functions outside usual scope.
• Use to specify graphics callback functions.
• Save to MAT-file and restore later.
table, timetable “Tables”, • Tables and timetables are rectangular containers for mixed-
“Timetables” type, column-oriented data.
• Tables have row and variable names that identify contents.
• Timetables also provide storage for time series data in a table
with rows labeled by timestamps. Timetable functions can
synchronize, resample, or aggregate timestamped data.
• Use the properties of a table or timetable to store metadata
such as variable units.
• Manipulation of elements similar to numeric or logical arrays.
• Access data by numeric or named index.
• Can select a subset of data and preserve the table container or
can extract the data from a table.
struct “Structures” • Fields store arrays of varying classes and sizes.
• Access one or all fields/indices in single operation.
• Field names identify contents.
• Method of passing function arguments.
• Use in comma-separated lists on page 2-66.
• More memory required for overhead
cell “Cell Arrays” • Cells store arrays of varying classes and sizes.
• Allows freedom to package data as you want.
• Manipulation of elements is similar to numeric or logical arrays.
• Method of passing function arguments.
• Use in comma-separated lists.
• More memory required for overhead

See Also

More About
• “Valid Combinations of Unlike Classes” on page 15-2

3-5
3 Overview of MATLAB Classes

External Websites
• Programming: Organizing Data (MathWorks Teaching Resources)

3-6
Use is* Functions to Detect State

Use is* Functions to Detect State


MATLAB has many functions that detect if an array or object has a specified state. These functions
are sometimes referred to as the is* functions because their names start with is. The functions
return logical 1 (true) if the inputs have the specified states, and logical 0 (false) otherwise.

For example, you can use these functions to detect if:

• An array has a specified data type (such as numeric, double, categorical, datetime, or
string)
• A numeric matrix has certain properties (such as being symmetric)
• The elements of a numeric array are finite, real, or complex
• The elements of a categorical or datetime array meet certain conditions
• An array has any elements that are outliers, missing values, or local maxima or minima
• An object is a graphics handle, or a Java or COM object

This table contains an alphabetical list of the most notable is* functions with descriptions of the
states that they detect.

Function Description
isa Determine if input has specified data type
isappdata Determine if application data exists
isapprox Determine approximate equality
isbanded Determine if matrix is within specific bandwidth
isbetween Determine which elements are within specified range
iscalendarduration Determine if input is calendarDuration array
iscategorical Determine if input is categorical array
iscategory Determine if inputs are names of categories
iscell Determine if input is cell array
iscellstr Determine if input is cell array of character vectors
ischange Find abrupt changes in input
ischar Determine if input is character array
iscolumn Determine if input is column vector
iscom Determine if input is Component Object Model (COM) object
isConfigured Determine if dictionary is configured
isdatetime Determine if input is datetime array
isdiag Determine if matrix is diagonal
isdst Find elements of datetime array that occur during daylight saving
time
isduration Determine if input is duration array
isempty Determine if input is empty array
isenum Determine if input is enumeration

3-7
3 Overview of MATLAB Classes

Function Description
isequal Determine if arrays are numerically equal
isequaln Determine if arrays are numerically equal, treating NaNs as equal
isevent Determine if input is Component Object Model (COM) object event
isfield Determine if input is MATLAB structure array field
isfile Determine if input is a file
isfinite Find array elements that are finite
isfloat Determine if input is floating-point array
isfolder Determine if input is folder
isgraphics Determine if input is valid graphics object handle
ishandle Determine if input is valid graphics or Java object handle
ishermitian Determine if matrix is Hermitian or skew-Hermitian
ishold Determine if graphics hold state is on
isinf Find array elements that are infinite
isinteger Determine if input is integer array
isinterface Determine if input is Component Object Model (COM) interface
isjava Determine if input is Java object
isKey Determine if dictionary contains key
iskeyword Determine if input is MATLAB keyword
isletter Find characters that are letters
islocalmax Find local maxima in input
islocalmax2 Find local maxima in 2-D data
islocalmin Find local minima in input
islocalmin2 Find local minima in 2-D data
islogical Determine if input is logical array
ismac Determine if version is for macOS platform
ismatrix Determine if input is matrix
ismember Find array elements that are members of set array
ismembertol Find array elements, within tolerance, that are members of set array
ismethod Determine if object has specified method
ismissing Find missing values in input
isnan Find numeric array elements that are NaN (Not-a-Number)
isnat Find datetime array elements that are NaT (Not-a-Time)
isnumeric Determine if input is numeric array
isobject Determine if input is MATLAB object
isordinal Determine if input is ordinal categorical array
isoutlier Find outliers in input

3-8
Use is* Functions to Detect State

Function Description
ispc Determine if version is for Windows (PC) platform
isprime Find array elements that are prime
isprop Determine if input is object property
isprotected Determine if categories of categorical array are protected
isreal Determine if all numeric array elements are real numbers
isregular Determine if input times are regular with respect to time or
calendar unit
isrow Determine if input is row vector
isscalar Determine if input is scalar
issorted Determine if array is sorted
issortedrows Determine if matrix or table rows are sorted
isspace Find characters that are space characters
issparse Determine if input is sparse
isstring Determine if input is string array
isStringScalar Determine if input is string array with one element
isstrprop Find characters in input strings that are of specified category
isstruct Determine if input is structure array
isstudent Determine if version is Student Version
issymmetric Determine if matrix is symmetric or skew-symmetric
istable Determine if input is table
istabular Determine if input is table or timetable
istall Determine if input is tall array
istimetable Determine if input is timetable
istril Determine if matrix is lower triangular
istriu Determine if matrix is upper triangular
isundefined Find undefined elements in categorical array
isUnderlyingType Determine if input has specified underlying data type
isuniform Determine if vector is uniformly spaced
isunix Determine if version is for Linux® or macOS platforms
isvalid Determine if input is valid handle
isvarname Determine if input is valid variable name
isvector Determine if input is vector
isweekend Find elements of datetime array that occur during weekends

See Also
Functions
exist | openvar | what | which | who | whos

3-9
3 Overview of MATLAB Classes

Tools
Workspace Panel

Related Examples
• “Fundamental MATLAB Classes” on page 3-2

3-10
4

Numeric Classes

• “Integers” on page 4-2


• “Floating-Point Numbers” on page 4-7
• “Create Complex Numbers” on page 4-14
• “Infinity and NaN” on page 4-15
• “Identifying Numeric Classes” on page 4-17
• “Display Format for Numeric Values” on page 4-18
• “Integer Arithmetic” on page 4-20
• “Single Precision Math” on page 4-26
4 Numeric Classes

Integers

In this section...
“Integer Classes” on page 4-2
“Creating Integer Data” on page 4-2
“Arithmetic Operations on Integer Classes” on page 4-4
“Largest and Smallest Values for Integer Classes” on page 4-4
“Loss of Precision Due to Conversion” on page 4-5

Integer Classes
MATLAB has four signed and four unsigned integer classes. Signed types enable you to work with
negative integers as well as positive, but cannot represent as wide a range of numbers as the
unsigned types because one bit is used to designate a positive or negative sign for the number.
Unsigned types give you a wider range of numbers, but these numbers can only be zero or positive.

MATLAB supports 1-, 2-, 4-, and 8-byte storage for integer data. You can save memory and execution
time for your programs if you use the smallest integer type that accommodates your data. For
example, you do not need a 32-bit integer to store the value 100.

Here are the eight integer classes, the range of values you can store with each type, and the MATLAB
conversion function required to create that type.

Class Range of Values Conversion Function


7 7
Signed 8-bit integer –2 to 2 –1 int8
15 15
Signed 16-bit integer –2 to 2 –1 int16
31 31
Signed 32-bit integer –2 to 2 –1 int32
63 63
Signed 64-bit integer –2 to 2 –1 int64
8
Unsigned 8-bit integer 0 to 2 –1 uint8
Unsigned 16-bit integer 0 to 216–1 uint16
Unsigned 32-bit integer 0 to 232–1 uint32
Unsigned 64-bit integer 0 to 264–1 uint64

Creating Integer Data


MATLAB stores numeric data as double-precision floating point (double) by default. To store data as
an integer, you need to convert from double to the desired integer type. Use one of the conversion
functions shown in the table above.

For example, to store 325 as a 16-bit signed integer assigned to variable x, type

x = int16(325);

4-2
Integers

If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest
integer. If the fractional part is exactly 0.5, then MATLAB chooses the nearest integer whose
absolute value is larger in magnitude:
x = 325.499;
int16(x)

ans =

int16

325

x = x + .001;
int16(x)

ans =

int16

326

If you need to round a number using a rounding scheme other than the default, MATLAB provides
four rounding functions: round, fix, floor, and ceil. The fix function enables you to override the
default and round towards zero when there is a nonzero fractional part:
x = 325.9;
int16(fix(x))

ans =

int16

325

Arithmetic operations that involve both integers and floating-point numbers always result in an
integer data type. MATLAB rounds the result, when necessary, according to the default rounding
algorithm. The example below yields an exact answer of 1426.75 which MATLAB then rounds to the
next highest integer:
int16(325)*4.39

ans =

int16

1427

The integer conversion functions are also useful when converting other classes, such as character
vectors, to integers:
chr = 'Hello World';
int8(chr)

ans =

1×11 int8 row vector

72 101 108 108 111 32 87 111 114 108 100

4-3
4 Numeric Classes

If you convert a NaN value to an integer class, the result is a value of 0 in that integer class. For
example:

int32(NaN)

ans =

int32

Arithmetic Operations on Integer Classes

MATLAB can perform integer arithmetic on the following types of data:

• Integers or integer arrays of the same integer data type. Arithmetic operations yield a result that
has the same data type as the operands:

x = uint32([132 347 528]) .* uint32(75);


class(x)

ans =
'uint32'
• Integers or integer arrays and scalar double-precision floating-point numbers. Arithmetic
operations yield a result that has the same data type as the integer operands:

x = uint32([132 347 528]) .* 75.49;


class(x)

ans =
'uint32'

For all binary operations in which one operand is an array of integer data type (except 64-bit
integers) and the other is a scalar double, MATLAB computes the operation using element-wise
double-precision arithmetic, and then converts the result back to the original integer data type. For
binary operations involving a 64-bit integer array and a scalar double, MATLAB computes the
operation as if 80-bit extended-precision arithmetic were used, to prevent loss of precision.

Operations involving complex numbers with integer types are not supported.

Largest and Smallest Values for Integer Classes

For each integer data type, there is a largest and smallest number that you can represent with that
type. The table shown under “Integer Classes” on page 4-2 lists the largest and smallest values for
each integer data type in the “Range of Values” column.

You can also obtain these values with the intmax and intmin functions:

intmax("int8")

ans =

int8

4-4
Integers

127

intmin("int8")

ans =

int8

-128

If you convert a number that is larger than the maximum value of an integer data type to that type,
MATLAB sets it to the maximum value. Similarly, if you convert a number that is smaller than the
minimum value of the integer data type, MATLAB sets it to the minimum value. For example:

x = int8(300)

x =

int8

127

x = int8(-300)

x =

int8

-128

Also, when the result of an arithmetic operation involving integers exceeds the maximum (or
minimum) value of the data type, MATLAB sets it to the maximum (or minimum) value:

x = int8(100)*3

x =

int8

127

x = int8(-100)*3

x =

int8

-128

Loss of Precision Due to Conversion


When you create a numeric array of large integers (larger than flintmax), MATLAB initially
represents the input as double precision by default. Precision can be lost when you convert this input
to the int64 or uint64 data type. To maintain precision, call int64 or uint64 with each scalar
element of the array instead.

4-5
4 Numeric Classes

For example, convert a numeric array of large integers to a 64-bit signed integer array by using
int64. The output array loses precision.

Y_inaccurate = int64([-72057594035891654 81997179153022975])

Y_inaccurate = 1×2 int64 row vector

-72057594035891656 81997179153022976

Instead, call int64 with each scalar element to return an accurate array.

Y_accurate = [int64(-72057594035891654) int64(81997179153022975)]

Y_accurate = 1×2 int64 row vector

-72057594035891654 81997179153022975

You can also create the integer array without loss of precision by using the hexadecimal or binary
values on page 6-54 of the integers.

Y_accurate = [0xFF000000001F123As64 0x1234FFFFFFFFFFFs64]

Y_accurate = 1×2 int64 row vector

-72057594035891654 81997179153022975

4-6
Floating-Point Numbers

Floating-Point Numbers
“Floating point” refers to a set of data types that encode real numbers, including fractions and
decimals. Floating-point data types allow for a varying number of digits after the decimal point, while
fixed-point data types have a specific number of digits reserved before and after the decimal point.
So, floating-point data types can represent a wider range of numbers than fixed-point data types.

Due to limited memory for number representation and storage, computers can represent a finite set
of floating-point numbers that have finite precision. This finite precision can limit accuracy for
floating-point computations that require exact values or high precision, as some numbers are not
represented exactly. Despite their limitations, floating-point numbers are widely used due to their fast
calculations and sufficient precision and range for solving real-world problems.

Floating-Point Numbers in MATLAB


MATLAB has data types for double-precision (double) and single-precision (single) floating-point
numbers following IEEE® Standard 754. By default, MATLAB represents floating-point numbers in
double precision. Double precision allows you to represent numbers to greater precision but requires
more memory than single precision. To conserve memory, you can convert a number to single
precision by using the single function.

You can store numbers between approximately –3.4 × 1038 and 3.4 × 1038 using either double or
single precision. If you have numbers outside of that range, store them using double precision.

Create Double-Precision Data

Because the default numeric type for MATLAB is type double, you can create a double-precision
floating-point number with a simple assignment statement.
x = 10;
c = class(x)

c =
'double'

You can convert numeric data, characters or strings, and logical data to double precision by using the
double function. For example, convert a signed integer to a double-precision floating-point number.
x = int8(-113);
y = double(x)

y =
-113

Create Single-Precision Data

To create a single-precision number, use the single function.


x = single(25.783);

You can also convert numeric data, characters or strings, and logical data to single precision by using
the single function. For example, convert a signed integer to a single-precision floating-point
number.
x = int8(-113);
y = single(x)

4-7
4 Numeric Classes

y =
single
-113

How MATLAB Stores Floating-Point Numbers

MATLAB constructs its double and single floating-point data types according to IEEE format and
follows the round to nearest, ties to even rounding mode by default.

A floating-point number x has the form:


s e
x = − 1 ⋅ (1 + f ) ⋅ 2

where:

• s determines the sign.


• f is the fraction, or mantissa, which satisfies 0 ≤ f < 1.
• e is the exponent.

s, f, and e are each determined by a finite number of bits in memory, with f and e depending on the
precision of the data type.

Storage of a double number requires 64 bits, as shown in this table.

Bits Width Usage


63 1 Stores the sign, where 0 is positive and 1 is
negative
62 to 52 11 Stores the exponent, biased by 1023
51 to 0 52 Stores the mantissa

Storage of a single number requires 32 bits, as shown in this table.

Bits Width Usage


31 1 Stores the sign, where 0 is positive and 1 is
negative
30 to 23 8 Stores the exponent, biased by 127
22 to 0 23 Stores the mantissa

Largest and Smallest Values for Floating-Point Data Types


The double- and single-precision data types have a largest and smallest value that you can represent.
Numbers outside of the representable range are assigned positive or negative infinity. However, some
numbers within the representable range cannot be stored exactly due to the gaps between
consecutive floating-point numbers, and these numbers can have round-off errors.

Largest and Smallest Double-Precision Values

Find the largest and smallest positive values that can be represented with the double data type by
using the realmax and realmin functions, respectively.

m = realmax

4-8
Floating-Point Numbers

m =
1.7977e+308

n = realmin

n =
2.2251e-308

realmax and realmin return normalized IEEE values. You can find the largest and smallest negative
values by multiplying realmax and realmin by -1. Numbers greater than realmax or less than –
realmax are assigned the values of positive or negative infinity, respectively.

Largest and Smallest Single-Precision Values

Find the largest and smallest positive values that can be represented with the single data type by
calling the realmax and realmin functions with the argument "single".
m = realmax("single")

m =
single
3.4028e+38

n = realmin("single")

n =
single
1.1755e-38

You can find the largest and smallest negative values by multiplying realmax("single") and
realmin("single") by –1. Numbers greater than realmax("single") or less than –
realmax("single") are assigned the values of positive or negative infinity, respectively.

Largest Consecutive Floating-Point Integers

Not all integers are representable using floating-point data types. The largest consecutive integer, x,
is the greatest integer for which all integers less than or equal to x can be exactly represented, but x
+ 1 cannot be represented in floating-point format. The flintmax function returns the largest
consecutive integer. For example, find the largest consecutive integer in double-precision floating-
point format, which is 253, by using the flintmax function.
x = flintmax

x =
9.0072e+15

Find the largest consecutive integer in single-precision floating-point format, which is 224.
y = flintmax("single")

y =
single
16777216

When you convert an integer data type to a floating-point data type, integers that are not exactly
representable in floating-point format lose accuracy. flintmax, which is a floating-point number, is
less than the greatest integer representable by integer data types using the same number of bits. For
example, flintmax for double precision is 253, while the maximum value for type int64 is 264 – 1.
Therefore, converting an integer greater than 253 to double precision results in a loss of accuracy.

4-9
4 Numeric Classes

Accuracy of Floating-Point Data


The accuracy of floating-point data can be affected by several factors:

• Limitations of your computer hardware — For example, hardware with insufficient memory
truncates the results of floating-point calculations.
• Gaps between each floating-point number and the next larger floating-point number — These gaps
are present on any computer and limit precision.

Gaps Between Floating-Point Numbers

You can determine the size of a gap between consecutive floating-point numbers by using the eps
function. For example, find the distance between 5 and the next larger double-precision number.

e = eps(5)

e =
8.8818e-16

You cannot represent numbers between 5 and 5 + eps(5) in double-precision format. If a double-
precision computation returns the answer 5, the result is accurate within eps(5). This radius of
accuracy is often called machine epsilon.

The gaps between floating-point numbers are not equal. For example, the gap between 1e10 and the
next larger double-precision number is larger than the gap between 5 and the next larger double-
precision number.

e = eps(1e10)

e =
1.9073e-06

Similarly, find the distance between 5 and the next larger single-precision number.

x = single(5);
e = eps(x)

e =
single
4.7684e-07

Gaps between single-precision numbers are larger than the gaps between double-precision numbers
because there are fewer single-precision numbers. So, results of single-precision calculations are less
precise than results of double-precision calculations.

When you convert a double-precision number to a single-precision number, you can determine the
upper bound for the amount the number is rounded by using the eps function. For example, when
you convert the double-precision number 3.14 to single precision, the number is rounded by at most
eps(single(3.14)).

Gaps Between Consecutive Floating-Point Integers

The flintmax function returns the largest consecutive integer in floating-point format. Above this
value, consecutive floating-point integers have a gap greater than 1.

Find the gap between flintmax and the next floating-point number by using eps:

4-10
Floating-Point Numbers

format long
x = flintmax

x =
9.007199254740992e+15

e = eps(x)

e =
2

Because eps(x) is 2, the next larger floating-point number that can be represented exactly is x + 2.

y = x + e

y =
9.007199254740994e+15

If you add 1 to x, the result is rounded to x.

z = x + 1

z =
9.007199254740992e+15

Arithmetic Operations on Floating-Point Numbers


You can use a range of data types in arithmetic operations with floating-point numbers, and the data
type of the result depends on the input types. However, when you perform operations with different
data types, some calculations may not be exact due to approximations or intermediate conversions.

Double-Precision Operands

You can perform basic arithmetic operations with double and any of the following data types. If one
or more operands are an integer scalar or array, the double operand must be a scalar. The result is
of type double, except where noted otherwise.

• single — The result is of type single.


• double
• int8, int16, int32, int64 — The result is of the same data type as the integer operand.
• uint8, uint16, uint32, uint64 — The result is of the same data type as the integer operand.
• char
• logical

Single-Precision Operands

You can perform basic arithmetic operations with single and any of the following data types. The
result is of type single.

• single
• double
• char
• logical

4-11
4 Numeric Classes

Unexpected Results with Floating-Point Arithmetic


Almost all operations in MATLAB are performed in double-precision arithmetic conforming to IEEE
Standard 754. Because computers represent numbers to a finite precision, some computations can
yield mathematically nonintuitive results. Some common issues that can arise while computing with
floating-point numbers are round-off error, cancellation, swamping, and intermediate conversions.
The unexpected results are not bugs in MATLAB and occur in any software that uses floating-point
numbers. For exact rational representations of numbers, consider using the Symbolic Math Toolbox™.

Round-Off Error

Round-off error can occur due to the finite-precision representation of floating-point numbers. For
example, the number 4/3 cannot be represented exactly as a binary fraction. As such, this calculation
returns the quantity eps(1), rather than 0.
e = 1 - 3*(4/3 - 1)

e =
2.2204e-16

Similarly, because pi is not an exact representation of π, sin(pi) is not exactly zero.


x = sin(pi)

x =
1.2246e-16

Round-off error is most noticeable when many operations are performed on floating-point numbers,
allowing errors to accumulate and compound. A best practice is to minimize the number of operations
whenever possible.

Cancellation

Cancellation can occur when you subtract a number from another number of roughly the same
magnitude, as measured by eps. For example, eps(2^53) is 2, so the numbers 2^53 + 1 and 2^53
have the same floating-point representation.
x = (2^53 + 1) - 2^53

x =
0

When possible, try rewriting computations in an equivalent form that avoids cancellations.

Swamping

Swamping can occur when you perform operations on floating-point numbers that differ by many
orders of magnitude. For example, this calculation shows a loss of precision that makes the addition
insignificant.
x = 1 + 1e-16

x =
1

Intermediate Conversions

When you perform arithmetic with different data types, intermediate calculations and conversions
can yield unexpected results. For example, although x and y are both 0.2, subtracting them yields a

4-12
Floating-Point Numbers

nonzero result. The reason is that y is first converted to double before the subtraction is performed.
This subtraction result is then converted to single, z.

format long
x = 0.2

x =
0.200000000000000

y = single(0.2)

y =
single
0.2000000

z = x - y

z =
single
-2.9802323e-09

Linear Algebra

Common issues in floating-point arithmetic, such as the ones described above, can compound when
applied to linear algebra problems because the related calculations typically consist of multiple steps.
For example, when solving the system of linear equations Ax = b, MATLAB warns that the results
may be inaccurate because operand matrix A is ill conditioned.

A = diag([2 eps]);
b = [2; eps];
x = A\b;

Warning: Matrix is close to singular or badly scaled.


Results may be inaccurate. RCOND = 1.110223e-16.

References
[1] Moler, Cleve. Numerical Computing with MATLAB. Natick, MA: The MathWorks, Inc., 2004.

See Also
Functions
double | single | flintmax | realmax | realmin | eps | isfloat

4-13
4 Numeric Classes

Create Complex Numbers


Complex numbers consist of two separate parts: a real part and an imaginary part. The basic
imaginary unit is equal to the square root of -1. This is represented in MATLAB by either of two
letters: i or j.

The following statement shows one way of creating a complex value in MATLAB. The variable x is
assigned a complex number with a real part of 2 and an imaginary part of 3:

x = 2 + 3i;

Another way to create a complex number is using the complex function. This function combines two
numeric inputs into a complex output, making the first input real and the second imaginary:

x = rand(3) * 5;
y = rand(3) * -8;

z = complex(x, y)
z =
4.7842 -1.0921i 0.8648 -1.5931i 1.2616 -2.2753i
2.6130 -0.0941i 4.8987 -2.3898i 4.3787 -3.7538i
4.4007 -7.1512i 1.3572 -5.2915i 3.6865 -0.5182i

You can separate a complex number into its real and imaginary parts using the real and imag
functions:

zr = real(z)
zr =
4.7842 0.8648 1.2616
2.6130 4.8987 4.3787
4.4007 1.3572 3.6865

zi = imag(z)
zi =
-1.0921 -1.5931 -2.2753
-0.0941 -2.3898 -3.7538
-7.1512 -5.2915 -0.5182

4-14
Infinity and NaN

Infinity and NaN


In this section...
“Infinity” on page 4-15
“NaN” on page 4-15

Infinity
MATLAB represents infinity by the special value Inf. Infinity results from operations like division by
zero and overflow, which lead to results too large to represent as conventional floating-point values.
MATLAB also provides a function called Inf that returns the IEEE arithmetic representation for
positive infinity as a double scalar value.

Several examples of statements that return positive or negative infinity in MATLAB are shown here.

x = 1/0 x = 1.e1000
x = x =
Inf Inf
x = exp(1000) x = log(0)
x = x =
Inf -Inf

Use the isinf function to verify that x is positive or negative infinity:

x = log(0);

isinf(x)
ans =
1

NaN
MATLAB represents values that are not real or complex numbers with a special value called NaN,
which stands for “Not a Number”. Expressions like 0/0 and inf/inf result in NaN, as do any
arithmetic operations involving a NaN:

x = 0/0
x =

NaN

You can also create NaNs by:

x = NaN;

whos x
Name Size Bytes Class

x 1x1 8 double

The NaN function returns one of the IEEE arithmetic representations for NaN as a double scalar
value. The exact bit-wise hexadecimal representation of this NaN value is,

4-15
4 Numeric Classes

format hex
x = NaN

x =

fff8000000000000

Always use the isnan function to verify that the elements in an array are NaN:

isnan(x)
ans =

MATLAB preserves the “Not a Number” status of alternate NaN representations and treats all of the
different representations of NaN equivalently. However, in some special cases (perhaps due to
hardware limitations), MATLAB does not preserve the exact bit pattern of alternate NaN
representations throughout an entire calculation, and instead uses the canonical NaN bit pattern
defined above.

Logical Operations on NaN

Because two NaNs are not equal to each other, logical operations involving NaN always return false,
except for a test for inequality, (NaN ~= NaN):

NaN > NaN


ans =
0

NaN ~= NaN
ans =
1

4-16
Identifying Numeric Classes

Identifying Numeric Classes


You can check the data type of a variable x using any of these commands.

Command Operation
whos x Display the data type of x.
xType = class(x); Assign the data type of x to a variable.
isnumeric(x) Determine if x is a numeric type.
isa(x, 'integer') Determine if x is the specified numeric type. (Examples for any
isa(x, 'uint64') integer, unsigned 64-bit integer, any floating point, double precision,
isa(x, 'float') and single precision are shown here).
isa(x, 'double')
isa(x, 'single')
isreal(x) Determine if x is real or complex.
isnan(x) Determine if x is Not a Number (NaN).
isinf(x) Determine if x is infinite.
isfinite(x) Determine if x is finite.

4-17
4 Numeric Classes

Display Format for Numeric Values


By default, MATLAB uses a 5-digit short format to display numbers. For example,

x = 4/3

x =

1.3333

You can change the display in the Command Window or Editor using the format function.

format long
x

x =

1.333333333333333

Using the format function only sets the format for the current MATLAB session. To set the format for
subsequent sessions, click Settings on the Home tab in the Environment section. Select
MATLAB > Command Window, and then choose a Numeric format option.

The following table summarizes the numeric output format options.

Style Result Example


short Short, fixed-decimal format with 4 digits after the 3.1416
(default) decimal point.
long Long, fixed-decimal format with 15 digits after the 3.141592653589793
decimal point for double values, and 7 digits after
the decimal point for single values.
shortE Short scientific notation with 4 digits after the 3.1416e+00
decimal point.
longE Long scientific notation with 15 digits after the 3.141592653589793e+00
decimal point for double values, and 7 digits after
the decimal point for single values.
shortG Short, fixed-decimal format or scientific notation, 3.1416
whichever is more compact, with a total of 5
digits.
longG Long, fixed-decimal format or scientific notation, 3.14159265358979
whichever is more compact, with a total of 15
digits for double values, and 7 digits for single
values.
shortEng Short engineering notation (exponent is a multiple 3.1416e+000
of 3) with 4 digits after the decimal point.
longEng Long engineering notation (exponent is a multiple 3.14159265358979e+000
of 3) with 15 significant digits.
+ Positive/Negative format with +, -, and blank +
characters displayed for positive, negative, and
zero elements.

4-18
Display Format for Numeric Values

Style Result Example


bank Currency format with 2 digits after the decimal 3.14
point.
hex Hexadecimal representation of a binary double- 400921fb54442d18
precision number.
rat Ratio of small integers. 355/113

The display format only affects how numbers are displayed, not how they are stored in MATLAB.

See Also
format

Related Examples
• “Format Output”

4-19
4 Numeric Classes

Integer Arithmetic

This example shows how to perform arithmetic on integer data representing signals and images.

Load Integer Signal Data

Load measurement datasets comprising signals from four instruments using 8 and 16-bit A-to-D's
resulting in data saved as int8, int16 and uint16. Time is stored as uint16.
load integersignal

% Look at variables
whos Signal1 Signal2 Signal3 Signal4 Time1

Name Size Bytes Class Attributes

Signal1 7550x1 7550 int8


Signal2 7550x1 7550 int8
Signal3 7550x1 15100 int16
Signal4 7550x1 15100 uint16
Time1 7550x1 15100 uint16

Plot Data

First we will plot two of the signals to see the signal ranges.
plot(Time1, Signal1, Time1, Signal2);
grid;
legend('Signal1','Signal2');

It is likely that these values would need to be scaled to calculate the actual physical value that the
signal represents, for example, Volts.

4-20
Integer Arithmetic

Process Data

We can perform standard arithmetic on integers such as +, -, *, and /. Let's say we wished to find the
sum of Signal1 and Signal2.

SumSig = Signal1 + Signal2; % Here we sum the integer signals.

Now let's plot the sum signal and see where it saturates.

cla;
plot(Time1, SumSig);
hold on
Saturated = (SumSig == intmin('int8')) | (SumSig == intmax('int8')); % Find where it has saturate
plot(Time1(Saturated),SumSig(Saturated),'rd')
grid
hold off

The markers show where the signal has saturated.

Load Integer Image Data

Next we will look at arithmetic on some image data.

street1 = imread('street1.jpg'); % Load image data


street2 = imread('street2.jpg');
whos street1 street2

Name Size Bytes Class Attributes

street1 480x640x3 921600 uint8


street2 480x640x3 921600 uint8

Here we see the images are 24-bit color, stored as three planes of uint8 data.

4-21
4 Numeric Classes

Display Images

Display first image.

cla;
image(street1); % Display image
axis equal
axis off

Display second image.

image(street2); % Display image


axis equal
axis off

4-22
Integer Arithmetic

Scale an Image

We can scale the image by a double precision constant but keep the image stored as integers. For
example,

duller = 0.5 * street2; % Scale image with a double constant but create an integer
whos duller

Name Size Bytes Class Attributes

duller 480x640x3 921600 uint8

subplot(1,2,1);
image(street2);
axis off equal tight
title('Original'); % Display image

subplot(1,2,2);
image(duller);
axis off equal tight
title('Duller'); % Display image

4-23
4 Numeric Classes

Add the Images

We can add the two street images together and plot the ghostly result.

combined = street1 + duller; % Add |uint8| images


subplot(1,1,1)
cla;
image(combined); % Display image
title('Combined');
axis equal
axis off

4-24
Integer Arithmetic

4-25
4 Numeric Classes

Single Precision Math

This example shows how to perform arithmetic and linear algebra with single precision data. It also
shows how the results are computed appropriately in single-precision or double-precision, depending
on the input.

Create Double Precision Data

Let's first create some data, which is double precision by default.

Ad = [1 2 0; 2 5 -1; 4 10 -1]

Ad = 3×3

1 2 0
2 5 -1
4 10 -1

Convert to Single Precision

We can convert data to single precision with the single function.

A = single(Ad); % or A = cast(Ad,'single');

Create Single Precision Zeros and Ones

We can also create single precision zeros and ones with their respective functions.

n = 1000;
Z = zeros(n,1,'single');
O = ones(n,1,'single');

Let's look at the variables in the workspace.

whos A Ad O Z n

Name Size Bytes Class Attributes

A 3x3 36 single
Ad 3x3 72 double
O 1000x1 4000 single
Z 1000x1 4000 single
n 1x1 8 double

We can see that some of the variables are of type single and that the variable A (the single precision
version of Ad) takes half the number of bytes of memory to store because singles require just four
bytes (32-bits), whereas doubles require 8 bytes (64-bits).

Arithmetic and Linear Algebra

We can perform standard arithmetic and linear algebra on singles.

B = A' % Matrix Transpose

B = 3×3 single matrix

4-26
Single Precision Math

1 2 4
2 5 10
0 -1 -1

whos B

Name Size Bytes Class Attributes

B 3x3 36 single

We see the result of this operation, B, is a single.

C = A * B % Matrix multiplication

C = 3×3 single matrix

5 12 24
12 30 59
24 59 117

C = A .* B % Elementwise arithmetic

C = 3×3 single matrix

1 4 0
4 25 -10
0 -10 1

X = inv(A) % Matrix inverse

X = 3×3 single matrix

5 2 -2
-2 -1 1
0 -2 1

I = inv(A) * A % Confirm result is identity matrix

I = 3×3 single matrix

1 0 0
0 1 0
0 0 1

I = A \ A % Better way to do matrix division than inv

I = 3×3 single matrix

1 0 0
0 1 0
0 0 1

E = eig(A) % Eigenvalues

4-27
4 Numeric Classes

E = 3×1 single column vector

3.7321
0.2679
1.0000

F = fft(A(:,1)) % FFT

F = 3×1 single column vector

7.0000 + 0.0000i
-2.0000 + 1.7321i
-2.0000 - 1.7321i

S = svd(A) % Singular value decomposition

S = 3×1 single column vector

12.3171
0.5149
0.1577

P = round(poly(A)) % The characteristic polynomial of a matrix

P = 1×4 single row vector

1 -5 5 -1

R = roots(P) % Roots of a polynomial

R = 3×1 single column vector

3.7321
1.0000
0.2679

Q = conv(P,P) % Convolve two vectors

Q = 1×7 single row vector

1 -10 35 -52 35 -10 1

R = conv(P,Q)

R = 1×10 single row vector

1 -15 90 -278 480 -480 278 -90 15 -1

stem(R); % Plot the result

4-28
Single Precision Math

A Program That Works for Either Single or Double Precision

Now let's look at a function to compute enough terms in the Fibonacci sequence so the ratio is less
than the correct machine epsilon (eps) for datatype single or double.

% How many terms needed to get single precision results?


fibodemo('single')

ans =
19

% How many terms needed to get double precision results?


fibodemo('double')

ans =
41

% Now let's look at the working code.


type fibodemo

function nterms = fibodemo(dtype)


%FIBODEMO Used by SINGLEMATH demo.
% Calculate number of terms in Fibonacci sequence.

% Copyright 1984-2014 The MathWorks, Inc.

fcurrent = ones(dtype);
fnext = fcurrent;
goldenMean = (ones(dtype)+sqrt(5))/2;
tol = eps(goldenMean);
nterms = 2;
while abs(fnext/fcurrent - goldenMean) >= tol

4-29
4 Numeric Classes

nterms = nterms + 1;
temp = fnext;
fnext = fnext + fcurrent;
fcurrent = temp;
end

Notice that we initialize several of our variables, fcurrent, fnext, and goldenMean, with values
that are dependent on the input datatype, and the tolerance tol depends on that type as well. Single
precision requires that we calculate fewer terms than the equivalent double precision calculation.

4-30
5

The Logical Class

• “Find Array Elements That Meet Conditions” on page 5-2


• “Reduce Logical Arrays to Single Value” on page 5-6
5 The Logical Class

Find Array Elements That Meet Conditions

This example shows how to filter the elements of an array by applying conditions to the array. For
instance, you can examine the even elements in a matrix, find the location of all 0s in a
multidimensional array, or replace NaN values in data. You can perform these tasks using a
combination of the relational and logical operators. The relational operators (>, <, >=, <=, ==, ~=)
impose conditions on the array, and you can apply multiple conditions by connecting them with the
logical operators and, or, and not, respectively denoted by the symbols &, |, and ~.

Apply Single Condition

To apply a single condition, start by creating a 5-by-5 matrix that contains random integers between 1
and 15. Reset the random number generator to the default state for reproducibility.
rng("default")
A = randi(15,5)

A = 5×5

13 2 3 3 10
14 5 15 7 1
2 9 15 14 13
14 15 8 12 15
10 15 13 15 11

Use the relational less than operator, <, to determine which elements of A are less than 9. Store the
result in B.
B = A < 9

B = 5×5 logical array

0 1 1 1 0
0 1 0 1 1
1 0 0 0 0
0 0 1 0 0
0 0 0 0 0

The result is a logical matrix. Each value in B is a logical 1 (true) or logical 0 (false) that indicates
whether the corresponding element of A fulfills the condition A < 9. For example, A(1,1) is 13, so
B(1,1) is logical 0 (false). However, A(1,2) is 2, so B(1,2) is logical 1 (true).

Although B contains information about which elements in A are less than 9, B does not tell you what
their values are. Rather than comparing the two matrices element by element, you can use B to index
into A.
A(B)

ans = 8×1

2
2
5
3

5-2
Find Array Elements That Meet Conditions

8
3
7
1

The result is a column vector of the elements in A that are less than 9. Since B is a logical matrix, this
operation is called logical indexing. In this case, the logical array being used as an index is the same
size as the array it is indexing, but this is not a requirement. For more information, see “Array
Indexing”.

Some problems require information about the locations of the array elements that meet a condition
rather than their actual values. In this example, you can use the find function to locate all of the
elements in A less than 9.
I = find(A < 9)

I = 8×1

3
6
7
11
14
16
17
22

The result is a column vector of linear indices. Each index describes the location of an element in A
that is less than 9, so in practice A(I) returns the same result as A(B). The difference is that A(B)
uses logical indexing, whereas A(I) uses linear indexing.

Apply Multiple Conditions

You can use the logical and, or, and not operators to apply any number of conditions to an array; the
number of conditions is not limited to one or two.

First, use the logical and operator, denoted &, to specify two conditions: the elements must be less
than 9 and greater than 2. Specify the conditions as a logical index to view the elements that
satisfy both conditions.
A(A<9 & A>2)

ans = 5×1

5
3
8
3
7

The result is a list of the elements in A that satisfy both conditions. Be sure to specify each condition
with a separate statement connected by a logical operator. For example, you cannot specify the
conditions above using A(2<A<9) because it evaluates to A(2<A | A<9).

Next, find the elements in A that are less than 9 and even.

5-3
5 The Logical Class

A(A<9 & ~mod(A,2))

ans = 3×1

2
2
8

The result is a list of all even elements in A that are less than 9. The use of the logical not operator,
~, converts the matrix mod(A,2) into a logical matrix, with a value of logical 1 (true) located where
an element is divisible by 2 or even.

Finally, find the elements in A that are less than 9 and even and not equal to 2.
A(A<9 & ~mod(A,2) & A~=2)

ans =
8

The result, 8, is less than 9, even, and not equal to 2. It is the only element in A that satisfies all three
conditions.

Use the find function to get the index of the element equal to 8 that satisfies the conditions.
find(A<9 & ~mod(A,2) & A~=2)

ans =
14

The result indicates that A(14) = 8.

Replace Values That Meet Condition

Sometimes it is useful to simultaneously change the values of several existing array elements. Use
logical indexing with a simple assignment statement to replace the values in an array that meet a
condition.

For example, replace all values in A that are greater than 10 with the number 10.
A(A>10) = 10

A = 5×5

10 2 3 3 10
10 5 10 7 1
2 9 10 10 10
10 10 8 10 10
10 10 10 10 10

Next, replace all values in A that are not equal to 10 with a NaN value.
A(A~=10) = NaN

A = 5×5

10 NaN NaN NaN 10


10 NaN 10 NaN NaN

5-4
Find Array Elements That Meet Conditions

NaN NaN 10 10 10
10 10 NaN 10 10
10 10 10 10 10

Lastly, replace all of the NaN values in A with zeros and apply the logical not operator on A, ~A.

A(isnan(A)) = 0;
C = ~A

C = 5×5 logical array

0 1 1 1 0
0 1 0 1 1
1 1 0 0 0
0 0 1 0 0
0 0 0 0 0

The resulting matrix has values of logical 1 (true) in place of the NaN values, and logical 0 (false)
in place of the 10s. The logical not operation, ~A, converts the numeric array A into a logical array C
such that A&C returns a matrix of logical 0 (false) values and A|C returns a matrix of logical 1
(true) values.

See Also
nan | Short-Circuit AND | Short-Circuit OR | isnan | find | and | or | xor | not

5-5
5 The Logical Class

Reduce Logical Arrays to Single Value

This example shows how to use the any and all functions to reduce an entire array to a single
logical value.

The any and all functions are natural extensions of the logical | (OR) and & (AND) operators,
respectively. However, rather than comparing just two elements, the any and all functions compare
all of the elements in a particular dimension of an array. It is as if all of those elements are connected
by & or | operators and the any or all functions evaluate the resulting long logical expressions.
Therefore, unlike the core logical operators, the any and all functions reduce the size of the array
dimension that they operate on so that it has size 1. This enables the reduction of many logical values
into a single logical condition.

First, create a matrix A that contains random integers between 1 and 25. Reset the random number
generator to the default state for reproducibility.

rng default
A = randi(25,5)

A = 5×5

21 3 4 4 17
23 7 25 11 1
4 14 24 23 22
23 24 13 20 24
16 25 21 24 17

Next, use the mod function along with the logical NOT operator, ~, to determine which elements in A
are even.

A = ~mod(A,2)

A = 5×5 logical array

0 0 1 1 0
0 0 0 0 0
1 1 1 0 1
0 1 0 1 1
1 0 0 1 0

The resulting matrices have values of logical 1 (true) where an element is even, and logical 0
(false) where an element is odd.

Since the any and all functions reduce the dimension that they operate on to size 1, it normally
takes two applications of one of the functions to reduce a 2–D matrix into a single logical condition,
such as any(any(A)). However, if you use the notation A(:) to regard all of the elements of A as a
single column vector, you can use any(A(:)) to get the same logical information without nesting the
function calls.

Determine if any elements in A are even.

any(A(:))

5-6
Reduce Logical Arrays to Single Value

ans = logical
1

You can perform logical and relational comparisons within the function call to any or all. This makes
it easy to quickly test an array for a variety of properties.

Determine if all elements in A are odd.

all(~A(:))

ans = logical
0

Determine whether any main or super diagonal elements in A are even. Since the vectors returned by
diag(A) and diag(A,1) are not the same size, you first need to reduce each diagonal to a single
scalar logical condition before comparing them. You can use the short-circuit OR operator || to
perform the comparison, since if any elements in the first diagonal are even then the entire
expression evaluates to true regardless of what appears on the right-hand side of the operator.

any(diag(A)) || any(diag(A,1))

ans = logical
1

See Also
any | all | and | or | xor | Short-Circuit AND | Short-Circuit OR

5-7
6

Characters and Strings

• “Text in String and Character Arrays” on page 6-2


• “Create String Arrays” on page 6-5
• “Cell Arrays of Character Vectors” on page 6-12
• “Analyze Text Data with String Arrays” on page 6-15
• “Test for Empty Strings and Missing Values” on page 6-20
• “Formatting Text” on page 6-24
• “Compare Text” on page 6-32
• “Search and Replace Text” on page 6-37
• “Build Pattern Expressions” on page 6-40
• “Convert Numeric Values to Text” on page 6-45
• “Convert Text to Numeric Values” on page 6-48
• “Unicode and ASCII Values” on page 6-52
• “Hexadecimal and Binary Values” on page 6-54
• “Frequently Asked Questions About String Arrays” on page 6-58
• “Update Your Code to Accept Strings” on page 6-63
6 Characters and Strings

Text in String and Character Arrays

There are two ways to represent text in MATLAB®. You can store text in string arrays and in
character vectors. MATLAB displays strings with double quotes and character vectors with single
quotes.

Represent Text with String Arrays

You can store any 1-by-n sequence of characters as a string, using the string data type. Enclose text
in double quotes to create a string.

str = "Hello, world"

str =
"Hello, world"

Though the text "Hello, world" is 12 characters long, str itself is a 1-by-1 string, or string scalar.
You can use a string scalar to specify a file name, plot label, or any other piece of textual information.

To find the number of characters in a string, use the strlength function.

n = strlength(str)

n =
12

If the text includes double quotes, use two double quotes within the definition.

str = "They said, ""Welcome!"" and waved."

str =
"They said, "Welcome!" and waved."

To add text to the end of a string, use the plus operator, +. If a variable can be converted to a string,
then plus converts it and appends it.

fahrenheit = 71;
celsius = (fahrenheit-32)/1.8;
tempText = "temperature is " + celsius + "C"

tempText =
"temperature is 21.6667C"

You can also concatenate text using the append function.

tempText2 = append("Today's ",tempText)

tempText2 =
"Today's temperature is 21.6667C"

The string function can convert different types of inputs, such as numeric, datetime, duration, and
categorical values. For example, convert the output of pi to a string.

ps = string(pi)

ps =
"3.1416"

6-2
Text in String and Character Arrays

You can store multiple pieces of text in a string array. Each element of the array can contain a string
having a different number of characters, without padding.

str = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","ISS"]

str = 2×3 string


"Mercury" "Gemini" "Apollo"
"Skylab" "Skylab B" "ISS"

str is a 2-by-3 string array. You can find the lengths of the strings with the strlength function.

N = strlength(str)

N = 2×3

7 6 6
6 8 3

String arrays are supported throughout MATLAB and MathWorks® products. Functions that accept
character arrays (and cell arrays of character vectors) as inputs also accept string arrays.

Represent Text with Character Vectors

To store a 1-by-n sequence of characters as a character vector, using the char data type, enclose it in
single quotes.

chr = 'Hello, world'

chr =
'Hello, world'

The text 'Hello, world' is 12 characters long, and chr stores it as a 1-by-12 character vector.

whos chr

Name Size Bytes Class Attributes

chr 1x12 24 char

If the text includes single quotes, use two single quotes within the definition.

chr = 'They said, ''Welcome!'' and waved.'

chr =
'They said, 'Welcome!' and waved.'

Character vectors have two principal uses:

• To specify single pieces of text, such as file names and plot labels.
• To represent data that is encoded using characters. In such cases, you might need easy access to
individual characters.

For example, you can store a DNA sequence as a character vector.

seq = 'GCTAGAATCC';

6-3
6 Characters and Strings

You can access individual characters or subsets of characters by indexing, just as you would index
into a numeric array.

seq(4:6)

ans =
'AGA'

Concatenate character vector with square brackets, just as you concatenate other types of arrays.

seq2 = [seq 'ATTAGAAACC']

seq2 =
'GCTAGAATCCATTAGAAACC'

You can also concatenate text using append. The append function is recommended because it treats
string arrays, character vectors, and cell arrays of character vectors consistently.

seq2 = append(seq,'ATTAGAAACC')

seq2 =
'GCTAGAATCCATTAGAAACC'

MATLAB functions that accept string arrays as inputs also accept character vectors and cell arrays of
character vectors.

See Also
string | char | cellstr | strlength | plus | horzcat | append

Related Examples
• “Create String Arrays” on page 6-5
• “Analyze Text Data with String Arrays” on page 6-15
• “Frequently Asked Questions About String Arrays” on page 6-58
• “Update Your Code to Accept Strings” on page 6-63
• “Cell Arrays of Character Vectors” on page 6-12

6-4
Create String Arrays

Create String Arrays

String arrays store pieces of text and provide a set of functions for working with text as data. You can
index into, reshape, and concatenate strings arrays just as you can with arrays of any other type. You
also can access the characters in a string and append text to strings using the plus operator. To
rearrange strings within a string array, use functions such as split, join, and sort.

Create String Arrays from Variables

MATLAB® provides string arrays to store pieces of text. Each element of a string array contains a 1-
by-n sequence of characters.

You can create a string using double quotes.


str = "Hello, world"

str =
"Hello, world"

As an alternative, you can convert a character vector to a string using the string function. chr is a
1-by-17 character vector. str is a 1-by-1 string that has the same text as the character vector.
chr = 'Greetings, friend'

chr =
'Greetings, friend'

str = string(chr)

str =
"Greetings, friend"

Create a string array containing multiple strings using the [] operator. str is a 2-by-3 string array
that contains six strings.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"]

str = 2×3 string


"Mercury" "Gemini" "Apollo"
"Skylab" "Skylab B" "ISS"

Find the length of each string in str with the strlength function. Use strlength, not length, to
determine the number of characters in strings.
L = strlength(str)

L = 2×3

7 6 6
6 8 3

As an alternative, you can convert a cell array of character vectors to a string array using the string
function. MATLAB displays strings in string arrays with double quotes, and displays characters
vectors in cell arrays with single quotes.

6-5
6 Characters and Strings

C = {'Mercury','Venus','Earth'}

C = 1×3 cell
{'Mercury'} {'Venus'} {'Earth'}

str = string(C)

str = 1×3 string


"Mercury" "Venus" "Earth"

In addition to character vectors, you can convert numeric, datetime, duration, and categorical values
to strings using the string function.

Convert a numeric array to a string array.


X = [5 10 20 3.1416];
string(X)

ans = 1×4 string


"5" "10" "20" "3.1416"

Convert a datetime value to a string.


d = datetime('now');
string(d)

ans =
"01-Feb-2025 09:13:31"

Also, you can read text from files into string arrays using the readtable, textscan, and fscanf
functions.

Create Empty and Missing Strings

String arrays can contain both empty and missing values. An empty string contains zero characters.
When you display an empty string, the result is a pair of double quotes with nothing between them
(""). The missing string is the string equivalent to NaN for numeric arrays. It indicates where a string
array has missing values. When you display a missing string, the result is <missing>, with no
quotation marks.

Create an empty string array using the strings function. When you call strings with no
arguments, it returns an empty string. Note that the size of str is 1-by-1, not 0-by-0. However, str
contains zero characters.
str = strings

str =
""

Create an empty character vector using single quotes. Note that the size of chr is 0-by-0.
chr = ''

chr =

0×0 empty char array

6-6
Create String Arrays

Create a string array where every element is an empty string. You can preallocate a string array with
the strings function.

str = strings(2,3)

str = 2×3 string


"" "" ""
"" "" ""

To create a missing string, convert a missing value using the string function. The missing string
displays as <missing>.

str = string(missing)

str =
<missing>

You can create a string array with both empty and missing strings. Use the ismissing function to
determine which elements are strings with missing values. Note that the empty string is not a
missing string.

str(1) = "";
str(2) = "Gemini";
str(3) = string(missing)

str = 1×3 string


"" "Gemini" <missing>

ismissing(str)

ans = 1×3 logical array

0 0 1

Compare a missing string to another string. The result is always 0 (false), even when you compare a
missing string to another missing string.

str = string(missing);
str == "Gemini"

ans = logical
0

str == string(missing)

ans = logical
0

Access Elements of String Array

String arrays support array operations such as indexing and reshaping. Use array indexing to access
the first row of str and all the columns.

6-7
6 Characters and Strings

str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
str(1,:)

ans = 1×3 string


"Mercury" "Gemini" "Apollo"

Access the second element in the second row of str.

str(2,2)

ans =
"Skylab B"

Assign a new string outside the bounds of str. MATLAB expands the array and fills unallocated
elements with missing values.

str(3,4) = "Mir"

str = 3×4 string


"Mercury" "Gemini" "Apollo" <missing>
"Skylab" "Skylab B" "ISS" <missing>
<missing> <missing> <missing> "Mir"

Access Characters Within Strings

You can index into a string array using curly braces, {}, to access characters directly. Use curly
braces when you need to access and modify characters within a string element. Indexing with curly
braces provides compatibility for code that could work with either string arrays or cell arrays of
character vectors. But whenever possible, use string functions to work with the characters in strings.

Access the second element in the second row with curly braces. chr is a character vector, not a
string.

str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
chr = str{2,2}

chr =
'Skylab B'

Access the character vector and return the first three characters.

str{2,2}(1:3)

ans =
'Sky'

Find the space characters in a string and replace them with dashes. Use the isspace function to
inspect individual characters within the string. isspace returns a logical vector that contains a true
value wherever there is a space character. Finally, display the modified string element, str(2,2).

TF = isspace(str{2,2})

TF = 1×8 logical array

6-8
Create String Arrays

0 0 0 0 0 0 1 0

str{2,2}(TF) = "-";
str(2,2)

ans =
"Skylab-B"

Note that in this case, you can also replace spaces using the replace function, without resorting to
curly brace indexing.
replace(str(2,2)," ","-")

ans =
"Skylab-B"

Concatenate Strings into String Array

Concatenate strings into a string array just as you would concatenate arrays of any other kind.

Concatenate two string arrays using square brackets, [].


str1 = ["Mercury","Gemini","Apollo"];
str2 = ["Skylab","Skylab B","ISS"];
str = [str1 str2]

str = 1×6 string


"Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"

Transpose str1 and str2. Concatenate them and then vertically concatenate column headings onto
the string array. When you concatenate character vectors into a string array, the character vectors
are automatically converted to strings.
str1 = str1';
str2 = str2';
str = [str1 str2];
str = [["Mission:","Station:"] ; str]

str = 4×2 string


"Mission:" "Station:"
"Mercury" "Skylab"
"Gemini" "Skylab B"
"Apollo" "ISS"

Append Text to Strings

To append text to strings, use the plus operator, +. The plus operator appends text to strings but
does not change the size of a string array.

Append a last name to an array of names. If you append a character vector to strings, then the
character vector is automatically converted to a string.
names = ["Mary";"John";"Elizabeth";"Paul";"Ann"];
names = names + ' Smith'

names = 5×1 string


"Mary Smith"

6-9
6 Characters and Strings

"John Smith"
"Elizabeth Smith"
"Paul Smith"
"Ann Smith"

Append different last names. You can append text to a string array from a string array or from a cell
array of character vectors. When you add nonscalar arrays, they must be the same size.
names = ["Mary";"John";"Elizabeth";"Paul";"Ann"];
lastnames = ["Jones";"Adams";"Young";"Burns";"Spencer"];
names = names + " " + lastnames

names = 5×1 string


"Mary Jones"
"John Adams"
"Elizabeth Young"
"Paul Burns"
"Ann Spencer"

Append a missing string. When you append a missing string with the plus operator, the output is a
missing string.
str1 = "Jones";
str2 = string(missing);
str1 + str2

ans =
<missing>

Split, Join, and Sort String Array

MATLAB provides a rich set of functions to work with string arrays. For example, you can use the
split, join, and sort functions to rearrange the string array names so that the names are in
alphabetical order by last name.

Split names on the space characters. Splitting changes names from a 5-by-1 string array to a 5-by-2
array.
names = ["Mary Jones";"John Adams";"Elizabeth Young";"Paul Burns";"Ann Spencer"];
names = split(names)

names = 5×2 string


"Mary" "Jones"
"John" "Adams"
"Elizabeth" "Young"
"Paul" "Burns"
"Ann" "Spencer"

Switch the columns of names so that the last names are in the first column. Add a comma after each
last name.
names = [names(:,2) names(:,1)];
names(:,1) = names(:,1) + ','

names = 5×2 string


"Jones," "Mary"

6-10
Create String Arrays

"Adams," "John"
"Young," "Elizabeth"
"Burns," "Paul"
"Spencer," "Ann"

Join the last and first names. The join function places a space character between the strings it joins.
After the join, names is a 5-by-1 string array.

names = join(names)

names = 5×1 string


"Jones, Mary"
"Adams, John"
"Young, Elizabeth"
"Burns, Paul"
"Spencer, Ann"

Sort the elements of names so that they are in alphabetical order.

names = sort(names)

names = 5×1 string


"Adams, John"
"Burns, Paul"
"Jones, Mary"
"Spencer, Ann"
"Young, Elizabeth"

See Also
string | strings | strlength | ismissing | isspace | plus | split | join | sort

Related Examples
• “Analyze Text Data with String Arrays” on page 6-15
• “Search and Replace Text” on page 6-37
• “Compare Text” on page 6-32
• “Test for Empty Strings and Missing Values” on page 6-20
• “Frequently Asked Questions About String Arrays” on page 6-58
• “Update Your Code to Accept Strings” on page 6-63

6-11
6 Characters and Strings

Cell Arrays of Character Vectors

To store text as a character vector, enclose it single quotes. Typically, a character vector has text that
you consider to be a single piece of information, such as a file name or a label for a plot. If you have
many pieces of text, such as a list of file names, then you can store them in a cell array. A cell array
whose elements are all character vectors is a cell array of character vectors.

Note

• The recommended way to store text is to use string arrays. If you create variables that have the
string data type, store them in string arrays, not cell arrays. For more information, see “Text in
String and Character Arrays” on page 6-2 and “Update Your Code to Accept Strings” on page 6-
63.
• While the phrase cell array of strings frequently has been used to describe such cell arrays, the
phrase is no longer accurate because such a cell array holds character vectors, not strings.

Create Cell Array of Character Vectors

To create a cell array of character vectors, use curly braces, {}, just as you would to create any cell
array. For example, use a cell array of character vectors to store a list of names.

C = {'Li','Sanchez','Jones','Yang','Larson'}

C = 1×5 cell
{'Li'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}

The character vectors in C can have different lengths because a cell array does not require that its
contents have the same size. To determine the lengths of the character vectors in C, use the
strlength function.

L = strlength(C)

L = 1×5

2 7 5 4 6

Access Character Vectors in Cell Array

To access character vectors in a cell array, index into it using curly braces, {}. Extract the contents of
the first cell and store it as a character vector.

C = {'Li','Sanchez','Jones','Yang','Larson'};
chr = C{1}

chr =
'Li'

6-12
Cell Arrays of Character Vectors

Assign a different character vector to the first cell.

C{1} = 'Yang'

C = 1×5 cell
{'Yang'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}

To refer to a subset of cells, instead of their contents, index using smooth parentheses.

C(1:3)

ans = 1×3 cell


{'Yang'} {'Sanchez'} {'Jones'}

While you can access the contents of cells by indexing, most functions that accept cell arrays as
inputs operate on the entire cell array. For example, you can use the strcmp function to compare the
contents of C to a character vector. strcmp returns 1 where there is a match and 0 otherwise.

TF = strcmp(C,'Yang')

TF = 1×5 logical array

1 0 0 1 0

You can sum over TF to find the number of matches.

num = sum(TF)

num =
2

Use TF as logical indices to return the matches in C. If you index using smooth parentheses, then the
output is a cell array containing only the matches.

M = C(TF)

M = 1×2 cell
{'Yang'} {'Yang'}

Convert Cell Arrays to String Arrays

String arrays are supported throughout MATLAB® and MathWorks® products. Therefore it is
recommended that you use string arrays instead of cell arrays of character vectors. (However,
MATLAB functions that accept string arrays as inputs do accept character vectors and cell arrays of
character vectors as well.)

You can convert cell arrays of character vectors to string arrays. To convert a cell array of character
vectors, use the string function.

C = {'Li','Sanchez','Jones','Yang','Larson'}

6-13
6 Characters and Strings

C = 1×5 cell
{'Li'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}

str = string(C)

str = 1×5 string


"Li" "Sanchez" "Jones" "Yang" "Larson"

In fact, the string function converts any cell array, so long as all of the contents can be converted to
strings.

C2 = {5, 10, 'some text', datetime('today')}

C2=1×4 cell array


{[5]} {[10]} {'some text'} {[02-Feb-2025]}

str2 = string(C2)

str2 = 1×4 string


"5" "10" "some text" "02-Feb-2025"

See Also
cellstr | char | iscellstr | strcmp | string

More About
• “Text in String and Character Arrays” on page 6-2
• “Access Data in Cell Array” on page 12-4
• “Create String Arrays” on page 6-5
• “Update Your Code to Accept Strings” on page 6-63
• “Frequently Asked Questions About String Arrays” on page 6-58

6-14
Analyze Text Data with String Arrays

Analyze Text Data with String Arrays

This example shows how to store text from a file as a string array, sort the words by their frequency,
plot the result, and collect basic statistics for the words found in the file.

Import Text File to String Array

Read text from Shakespeare's Sonnets with the fileread function. fileread returns the text as a
1-by-100266 character vector.

sonnets = fileread('sonnets.txt');
sonnets(1:35)

ans =
'THE SONNETS

by William Shakespeare'

Convert the text to a string using the string function. Then, split it on newline characters using the
splitlines function. sonnets becomes a 2625-by-1 string array, where each string contains one
line from the poems. Display the first five lines of sonnets.

sonnets = string(sonnets);
sonnets = splitlines(sonnets);
sonnets(1:5)

ans = 5×1 string


"THE SONNETS"
""
"by William Shakespeare"
""
""

Clean String Array

To calculate the frequency of the words in sonnets, first clean it by removing empty strings and
punctuation marks. Then reshape it into a string array that contains individual words as elements.

Remove the strings with zero characters ("") from the string array. Compare each element of
sonnets to "", the empty string. You can create strings, including an empty string, using double
quotes. TF is a logical vector that contains a true value wherever sonnets contains a string with zero
characters. Index into sonnets with TF and delete all strings with zero characters.

TF = (sonnets == "");
sonnets(TF) = [];
sonnets(1:10)

ans = 10×1 string


"THE SONNETS"
"by William Shakespeare"
" I"
" From fairest creatures we desire increase,"
" That thereby beauty's rose might never die,"
" But as the riper should by time decease,"

6-15
6 Characters and Strings

" His tender heir might bear his memory:"


" But thou, contracted to thine own bright eyes,"
" Feed'st thy light's flame with self-substantial fuel,"
" Making a famine where abundance lies,"

Replace some punctuation marks with space characters. For example, replace periods, commas, and
semi-colons. Keep apostrophes because they can be part of some words in the Sonnets, such as
light's.
p = [".","?","!",",",";",":"];
sonnets = replace(sonnets,p," ");
sonnets(1:10)

ans = 10×1 string


"THE SONNETS"
"by William Shakespeare"
" I"
" From fairest creatures we desire increase "
" That thereby beauty's rose might never die "
" But as the riper should by time decease "
" His tender heir might bear his memory "
" But thou contracted to thine own bright eyes "
" Feed'st thy light's flame with self-substantial fuel "
" Making a famine where abundance lies "

Strip leading and trailing space characters from each element of sonnets.
sonnets = strip(sonnets);
sonnets(1:10)

ans = 10×1 string


"THE SONNETS"
"by William Shakespeare"
"I"
"From fairest creatures we desire increase"
"That thereby beauty's rose might never die"
"But as the riper should by time decease"
"His tender heir might bear his memory"
"But thou contracted to thine own bright eyes"
"Feed'st thy light's flame with self-substantial fuel"
"Making a famine where abundance lies"

Split sonnets into a string array whose elements are individual words. You can use the split
function to split elements of a string array on whitespace characters, or on delimiters that you
specify. However, split requires that every element of a string array must be divisible into an equal
number of new strings. The elements of sonnets have different numbers of spaces, and therefore are
not divisible into equal numbers of strings. To use the split function on sonnets, write a for-loop
that calls split on one element at a time.

Create the empty string array sonnetWords using the strings function. Write a for-loop that splits
each element of sonnets using the split function. Concatenate the output from split onto
sonnetWords. Each element of sonnetWords is an individual word from sonnets.
sonnetWords = strings(0);
for i = 1:length(sonnets)

6-16
Analyze Text Data with String Arrays

sonnetWords = [sonnetWords ; split(sonnets(i))];


end
sonnetWords(1:10)

ans = 10×1 string


"THE"
"SONNETS"
"by"
"William"
"Shakespeare"
"I"
"From"
"fairest"
"creatures"
"we"

Sort Words Based on Frequency

Find the unique words in sonnetWords. Count them and sort them based on their frequency.

To count words that differ only by case as the same word, convert sonnetWords to lowercase. For
example, The and the count as the same word. Find the unique words using the unique function.
Then, count the number of times each unique word occurs using the histcounts function.

sonnetWords = lower(sonnetWords);
[words,~,idx] = unique(sonnetWords);
numOccurrences = histcounts(idx,numel(words));

Sort the words in sonnetWords by number of occurrences, from most to least common.

[rankOfOccurrences,rankIndex] = sort(numOccurrences,'descend');
wordsByFrequency = words(rankIndex);

Plot Word Frequency

Plot the occurrences of words in the Sonnets from the most to least common words. Zipf's Law states
that the distribution of occurrences of words in a large body text follows a power-law distribution.

loglog(rankOfOccurrences);
xlabel('Rank of word (most to least common)');
ylabel('Number of Occurrences');

6-17
6 Characters and Strings

Display the ten most common words in the Sonnets.

wordsByFrequency(1:10)

ans = 10×1 string


"and"
"the"
"to"
"my"
"of"
"i"
"in"
"that"
"thy"
"thou"

Collect Basic Statistics in Table

Calculate the total number of occurrences of each word in sonnetWords. Calculate the number of
occurrences as a percentage of the total number of words, and calculate the cumulative percentage
from most to least common. Write the words and the basic statistics for them to a table.

numOccurrences = numOccurrences(rankIndex);
numOccurrences = numOccurrences';
numWords = length(sonnetWords);
T = table;
T.Words = wordsByFrequency;
T.NumOccurrences = numOccurrences;
T.PercentOfText = numOccurrences / numWords * 100.0;
T.CumulativePercentOfText = cumsum(numOccurrences) / numWords * 100.0;

Display the statistics for the ten most common words.

6-18
Analyze Text Data with String Arrays

T(1:10,:)

ans=10×4 table
Words NumOccurrences PercentOfText CumulativePercentOfText
______ ______________ _____________ _______________________

"and" 490 2.7666 2.7666


"the" 436 2.4617 5.2284
"to" 409 2.3093 7.5377
"my" 371 2.0947 9.6324
"of" 370 2.0891 11.722
"i" 341 1.9254 13.647
"in" 321 1.8124 15.459
"that" 320 1.8068 17.266
"thy" 280 1.5809 18.847
"thou" 233 1.3156 20.163

The most common word in the Sonnets, and, occurs 490 times. Together, the ten most common words
account for 20.163% of the text.

See Also
string | split | join | unique | replace | lower | splitlines | histcounts | strip | sort |
table

Related Examples
• “Create String Arrays” on page 6-5
• “Search and Replace Text” on page 6-37
• “Compare Text” on page 6-32
• “Test for Empty Strings and Missing Values” on page 6-20

6-19
6 Characters and Strings

Test for Empty Strings and Missing Values

String arrays can contain both empty strings and missing values. Empty strings contain zero
characters and display as double quotes with nothing between them (""). You can determine if a
string is an empty string using the == operator. The empty string is a substring of every other string.
Therefore, functions such as contains always find the empty string within other strings. String
arrays also can contain missing values. Missing values in string arrays display as <missing>. To find
missing values in a string array, use the ismissing function instead of the == operator.

Test for Empty Strings

You can test a string array for empty strings using the == operator.

You can create an empty string using double quotes with nothing between them (""). Note that the
size of str is 1-by-1, not 0-by-0. However, str contains zero characters.

str = ""

str =
""

Create an empty character vector using single quotes. Note that the size of chr is 0-by-0. The
character array chr actually is an empty array, and not just an array with zero characters.

chr = ''

chr =

0×0 empty char array

Create an array of empty strings using the strings function. Each element of the array is a string
with no characters.

str2 = strings(1,3)

str2 = 1×3 string


"" "" ""

Test if str is an empty string by comparing it to an empty string.

if (str == "")
disp 'str has zero characters'
end

str has zero characters

Do not use the isempty function to test for empty strings. A string with zero characters still has a
size of 1-by-1. However, you can test if a string array has at least one dimension with a size of zero
using the isempty function.

Create an empty string array using the strings function. To be an empty array, at least one
dimension must have a size of zero.

str = strings(0,3)

6-20
Test for Empty Strings and Missing Values

str =

0×3 empty string array

Test str using the isempty function.

isempty(str)

ans = logical
1

Test a string array for empty strings. The == operator returns a logical array that is the same size as
the string array.

str = ["Mercury","","Apollo"]

str = 1×3 string


"Mercury" "" "Apollo"

str == ''

ans = 1×3 logical array

0 1 0

Find Empty Strings Within Other Strings

Strings always contain the empty string as a substring. In fact, the empty string is always at both the
start and the end of every string. Also, the empty string is always found between any two consecutive
characters in a string.

Create a string. Then test if it contains the empty string.

str = "Hello, world";


TF = contains(str,"")

TF = logical
1

Test if str starts with the empty string.

TF = startsWith(str,"")

TF = logical
1

Count the number of characters in str. Then count the number of empty strings in str. The count
function counts empty strings at the beginning and end of str, and between each pair of characters.
Therefore if str has N characters, it also has N+1 empty strings.

str

str =
"Hello, world"

6-21
6 Characters and Strings

strlength(str)

ans =
12

count(str,"")

ans =
13

Replace a substring with the empty string. When you call replace with an empty string, it removes
the substring and replaces it with a string that has zero characters.

replace(str,"world","")

ans =
"Hello, "

Insert a substring after empty strings using the insertAfter function. Because there are empty
strings between each pair of characters, insertAfter inserts substrings between each pair.

insertAfter(str,"","-")

ans =
"-H-e-l-l-o-,- -w-o-r-l-d-"

In general, string functions that replace, erase, extract, or insert substrings allow you to specify
empty strings as the starts and ends of the substrings to modify. When you do so, these functions
operate on the start and end of the string, and between every pair of characters.

Test for Missing Values

You can test a string array for missing values using the ismissing function. The missing string is the
string equivalent to NaN for numeric arrays. It indicates where a string array has missing values. The
missing string displays as <missing>.

To create a missing string, convert a missing value using the string function.

str = string(missing)

str =
<missing>

You can create a string array with both empty and missing strings. Use the ismissing function to
determine which elements are strings with missing values. Note that the empty string is not a
missing string.

str(1) = "";
str(2) = "Gemini";
str(3) = string(missing)

str = 1×3 string


"" "Gemini" <missing>

ismissing(str)

ans = 1×3 logical array

6-22
Test for Empty Strings and Missing Values

0 0 1

Compare str to a missing string. The comparison is always 0 (false), even when you compare a
missing string to another missing string.

str == string(missing)

ans = 1×3 logical array

0 0 0

To find missing strings, use the ismissing function. Do not use the == operator.

See Also
string | strings | strlength | ismissing | contains | startsWith | endsWith | erase |
extractBetween | extractBefore | extractAfter | insertAfter | insertBefore | replace |
replaceBetween | eraseBetween | eq | all | any

Related Examples
• “Create String Arrays” on page 6-5
• “Analyze Text Data with String Arrays” on page 6-15
• “Search and Replace Text” on page 6-37
• “Compare Text” on page 6-32

6-23
6 Characters and Strings

Formatting Text

To convert data to text and control its format, you can use formatting operators with common
conversion functions, such as num2str and sprintf. These operators control notation, alignment,
significant digits, and so on. They are similar to those used by the printf function in the C
programming language. Typical uses for formatted text include text for display and output files.

For example, %f converts floating-point values to text using fixed-point notation. Adjust the format by
adding information to the operator, such as %.2f to represent two digits after the decimal mark, or
%12f to represent 12 characters in the output, padding with spaces as needed.

A = pi*ones(1,3);
txt = sprintf('%f | %.2f | %12f', A)

txt =
'3.141593 | 3.14 | 3.141593'

You can combine operators with ordinary text and special characters in a format specifier. For
instance, \n inserts a newline character.

txt = sprintf('Displaying pi: \n %f \n %.2f \n %12f', A)

txt =
'Displaying pi:
3.141593
3.14
3.141593'

Functions that support formatting operators are compose, num2str, sprintf, fprintf, and the
error handling functions assert, error, warning, and MException.

Fields of the Formatting Operator


A formatting operator can have six fields, as shown in the figure. From right to left, the fields are the
conversion character, subtype, precision, field width, flags, and numeric identifier. (Space characters
are not allowed in the operator. They are shown here only to improve readability of the figure.) The
conversion character is the only required field, along with the leading % character.

Conversion Character

The conversion character specifies the notation of the output. It consists of a single character and
appears last in the format specifier.

Specifier Description
c Single character.

6-24
Formatting Text

Specifier Description
d Decimal notation (signed).
e Exponential notation (using a lowercase e, as in 3.1415e+00).
E Exponential notation (using an uppercase E, as in 3.1415E+00).
f Fixed-point notation.
g The more compact of %e or %f. (Insignificant zeros do not print.)
G Same as %g, but using an uppercase E.
o Octal notation (unsigned).
s Character vector or string array.
u Decimal notation (unsigned).
x Hexadecimal notation (unsigned, using lowercase letters a–f).
X Hexadecimal notation (unsigned, using uppercase letters A–F).

For example, format the number 46 using different conversion characters to display the number in
decimal, fixed-point, exponential, and hexadecimal formats.
A = 46*ones(1,4);
txt = sprintf('%d %f %e %X', A)

txt =
'46 46.000000 4.600000e+01 2E'

Subtype

The subtype field is a single alphabetic character that immediately precedes the conversion
character. Without the subtype field, the conversion characters %o, %x, %X, and %u treat input data as
integers. To treat input data as floating-point values instead and convert them to octal, decimal, or
hexadecimal representations, use one of following subtype specifiers.

b The input data are double-precision floating-point values rather than unsigned integers. For
example, to print a double-precision value in hexadecimal, use a format like %bx.
t The input data are single-precision floating-point values rather than unsigned integers.

Precision

The precision field in a formatting operator is a nonnegative integer that immediately follows a
period. For example, in the operator %7.3f, the precision is 3. For the %g operator, the precision
indicates the number of significant digits to display. For the %f, %e, and %E operators, the precision
indicates how many digits to display to the right of the decimal point.

Display numbers to different precisions using the precision field.


txt = sprintf('%g %.2g %f %.2f', pi*50*ones(1,4))

txt =

'157.08 1.6e+02 157.079633 157.08'

While you can specify the precision in a formatting operator for input text (for example, in the %s
operator), there is usually no reason to do so. If you specify the precision as p, and p is less than the
number of characters in the input text, then the output contains only the first p characters.

6-25
6 Characters and Strings

Field Width

The field width in a formatting operator is a nonnegative integer that specifies the number of digits or
characters in the output when formatting input values. For example, in the operator %7.3f, the field
width is 7.

Specify different field widths. To show the width for each output, use the | character. By default, the
output text is padded with space characters when the field width is greater than the number of
characters.

txt = sprintf('|%e|%15e|%f|%15f|', pi*50*ones(1,4))

txt =

'|1.570796e+02| 1.570796e+02|157.079633| 157.079633|'

When used on text input, the field width can determine whether to pad the output text with spaces. If
the field width is less than or equal to the number of characters in the input text, then it has no effect.

txt = sprintf('%30s', 'Pad left with spaces')

txt =

' Pad left with spaces'

Flags

Optional flags control additional formatting of the output text. The table describes the characters you
can use as flags.

Character Description Example


Minus sign (-) Left-justify the converted argument %-5.2d
in its field.
Plus sign (+) For numeric values, always print a %+5.2d
leading sign character (+ or -). %+5s
For text values, right-justify the
converted argument in its field.
Space Insert a space before the value. % 5.2f
Zero (0) Pad with zeros rather than spaces. %05.2f
Pound sign (#) Modify selected numeric %#5.0f
conversions:

• For %o, %x, or %X, print 0, 0x, or


0X prefix.
• For %f, %e, or %E, print decimal
point even when precision is 0.
• For %g or %G, do not remove
trailing zeros or decimal point.

Right- and left-justify the output. The default behavior is to right-justify the output text.

txt = sprintf('right-justify: %12.2f\nleft-justify: %-12.2f',...


12.3, 12.3)

6-26
Formatting Text

txt =

'right-justify: 12.30
left-justify: 12.30 '

Display a + sign for positive numbers. The default behavior is to omit the leading + sign for positive
numbers.

txt = sprintf('no sign: %12.2f\nsign: %+12.2f',...


12.3, 12.3)

txt =

'no sign: 12.30


sign: +12.30'

Pad to the left with spaces and zeros. The default behavior is to pad with spaces.

txt = sprintf('Pad with spaces: %12.2f\nPad with zeros: %012.2f',...


5.2, 5.2)

txt =

'Pad with spaces: 5.20


Pad with zeros: 000000005.20'

Note You can specify more than one flag in a formatting operator.

Value Identifiers

By default, functions such as sprintf insert values from input arguments into the output text in
sequential order. To process the input arguments in a nonsequential order, specify the order using
numeric identifiers in the format specifier. Specify nonsequential arguments with an integer
immediately following the % sign, followed by a $ sign.

Ordered Sequentially Ordered By Identifier


sprintf('%s %s %s',... sprintf('%3$s %2$s %1$s',...
'1st','2nd','3rd') '1st','2nd','3rd')

ans = ans =

'1st 2nd 3rd' '3rd 2nd 1st'

Special Characters

Special characters can be part of the output text. But because they cannot be entered as ordinary
text, they require specific character sequences to represent them. To insert special characters into
output text, use any of the character sequences in the table.

Special Character Representation in Format


Specifier
Single quotation mark ''
Percent character %%

6-27
6 Characters and Strings

Special Character Representation in Format


Specifier
Backslash \\
Alarm \a
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t
Vertical tab \v
Character whose Unicode numeric value can be represented by \xN
the hexadecimal number, N
Example: sprintf('\x5A')
returns 'Z'
Character whose Unicode numeric value can be represented by \N
the octal number, N
Example: sprintf('\132')
returns 'Z'

Setting Field Width and Precision


The formatting operator follows a set of rules for formatting output text to the specified field width
and precision. You also can specify values for the field width and precision outside the format
specifier, and use numbered identifiers with the field width and precision.

Rules for Formatting Precision and Field Width

The figure illustrates how the field width and precision settings affect the output of the formatting
functions. In this figure, the zero following the % sign in the formatting operator means to add leading
zeros to the output text rather than space characters.

• If the precision is not specified, then it defaults to six.


• If the precision p is less than the number of digits in the fractional part of the input, then only p
digits are shown after the decimal point. The fractional value is rounded in the output.
• If the precision p is greater than the number of digits f in the fractional part of the input, then p
digits are shown after the decimal point. The fractional part is extended to the right with p-f
zeros in the output.

6-28
Formatting Text

• If the field width is not specified, then it defaults to p+1+n, where n is the number of digits in the
whole part of the input value.
• If the field width w is greater than p+1+n, then the whole part of the output value is padded to the
left with w-(p+1+n) additional characters. The additional characters are space characters unless
the formatting operator includes the 0 flag. In that case, the additional characters are zeros.

Specify Field Width and Precision Outside Format Specifier

You can specify the field width and precision using values from a sequential argument list. Use an
asterisk (*) in place of the field width or precision fields of the formatting operator.

For example, format and display three numbers. In each case, use an asterisk to specify that the field
width or precision come from input arguments that follow the format specifier.

txt = sprintf('%*f %.*f %*.*f',...


15,123.45678,...
3,16.42837,...
6,4,pi)

txt =

' 123.456780 16.428 3.1416'

The table describes the effects of each formatting operator in the example.

Formatting Operator Description


%*f Specify width as the following input argument,
15.
%.*f Specify precision as the following input
argument, 3.
%*.*f Specify width and precision as the following input
arguments, 6, and 4.

You can mix the two styles. For example, get the field width from the following input argument and
the precision from the format specifier.

txt = sprintf('%*.2f', 5, 123.45678)

txt =

'123.46'

Specify Numbered Identifiers in Width and Precision Fields

You also can specify field width and precision as values from a nonsequential argument list, using an
alternate syntax shown in the figure. Within the formatting operator, specify the field width and
precision with asterisks that follow numbered identifiers and $ signs. Specify the values of the field
width and precision with input arguments that follow the format specifier.

6-29
6 Characters and Strings

For example, format and display three numbers. In each case, use a numbered identifier to specify
that the field width or precision come from input arguments that follow the format specifier.

txt = sprintf('%1$*4$f %2$.*5$f %3$*6$.*7$f',...


123.45678, 16.42837, pi, 15, 3, 6, 4)

txt =

' 123.456780 16.428 3.1416'

The table describes the effect of each formatting operator in the example.

Formatting Operator Description


%1$*4$f 1$ specifies the first input argument,
123.45678, as the value
*4$ specifies the fourth input argument, 15, as
the field width
%2$.*5$f 2$ specifies the second input argument,
16.42837, as the value
.*5$ specifies the fifth input argument, 3, as the
precision
%3$*6$.*7$f 3$ specifies the third input argument, pi, as the
value
*6$ specifies the sixth input argument, 6, as the
field width
.*7$ specifies the seventh input argument, 4, as
the precision

Restrictions on Using Identifiers


If any of the formatting operators include an identifier field, then all the operators in the format
specifier must include identifier fields. If you use both sequential and nonsequential ordering in the
same function call, then the output is truncated at the first switch between sequential and
nonsequential identifiers.

Valid Syntax Invalid Syntax


sprintf('%d %d %d %d',... sprintf('%d %3$d %d %d',...
1,2,3,4) 1,2,3,4)

ans = ans =

'1 2 3 4' '1 '

If your function call provides more input arguments than there are formatting operators in the format
specifier, then the operators are reused. However, only function calls that use sequential ordering
reuse formatting operators. You cannot reuse formatting operators when you use numbered
identifiers.

6-30
Formatting Text

Valid Syntax Invalid Syntax


sprintf('%d',1,2,3,4) sprintf('%1$d',1,2,3,4)

ans = ans =

'1234' '1'

If you use numbered identifiers when the input data is a vector or array, then the output does not
contain formatted data.

Valid Syntax Invalid Syntax


v = [1.4 2.7 3.1]; v = [1.4 2.7 3.1];
sprintf('%.4f %.4f %.4f',v) sprintf('%3$.4f %1$.4f %2$.4f',v)

ans = ans =

'1.4000 2.7000 3.1000' 1×0 empty char array

See Also
compose | sprintf | fprintf | num2str

Related Examples
• “Convert Text to Numeric Values” on page 6-48
• “Convert Numeric Values to Text” on page 6-45

External Websites
• Programming: Organizing Data (MathWorks Teaching Resources)

6-31
6 Characters and Strings

Compare Text

Compare text in character arrays and string arrays in different ways. You can compare string arrays
and character vectors with relational operators and with the strcmp function. You can sort string
arrays using the sort function, just as you would sort arrays of any other type. MATLAB® also
provides functions to inspect characters in pieces of text. For example, you can determine which
characters in a character vector or string array are letters or space characters.

Compare String Arrays for Equality

You can compare string arrays for equality with the relational operators == and ~=. When you
compare string arrays, the output is a logical array that has 1 where the relation is true, and 0 where
it is not true.

Create two string scalars. You can create strings using double quotes.

str1 = "Hello";
str2 = "World";
str1,str2

str1 =
"Hello"

str2 =
"World"

Compare str1 and str2 for equality.

str1 == str2

ans = logical
0

Compare a string array with multiple elements to a string scalar.

str1 = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","International Space Station"];
str2 = "Apollo";
str1 == str2

ans = 2×3 logical array

0 0 1
0 0 0

Compare a string array to a character vector. As long as one of the variables is a string array, you can
make the comparison.

chr = 'Gemini';
TF = (str1 == chr)

TF = 2×3 logical array

0 1 0

6-32
Compare Text

0 0 0

Index into str1 with TF to extract the string elements that matched Gemini. You can use logical
arrays to index into an array.

str1(TF)

ans =
"Gemini"

Compare for inequality using the ~= operator. Index into str1 to extract the elements that do not
match 'Gemini'.

TF = (str1 ~= chr)

TF = 2×3 logical array

1 0 1
1 1 1

str1(TF)

ans = 5×1 string


"Mercury"
"Skylab"
"Skylab B"
"Apollo"
"International Space Station"

Compare two nonscalar string arrays. When you compare two nonscalar arrays, they must be the
same size.

str2 = ["Mercury","Mars","Apollo";...
"Jupiter","Saturn","Neptune"];
TF = (str1 == str2)

TF = 2×3 logical array

1 0 1
0 0 0

Index into str1 to extract the matches.

str1(TF)

ans = 2×1 string


"Mercury"
"Apollo"

Compare String Arrays with Other Relational Operators

You can also compare strings with the relational operators >, >=, <, and <=. Strings that start with
uppercase letters come before strings that start with lowercase letters. For example, the string
"ABC" is less than "abc". Digits and some punctuation marks also come before letters.

6-33
6 Characters and Strings

"ABC" < "abc"

ans = logical
1

Compare a string array that contains names to another name with the > operator. The names
Sanchez, de Ponte, and Nash come after Matthews, because S, d, and N all are greater than M.

str = ["Sanchez","Jones","de Ponte","Crosby","Nash"];


TF = (str > "Matthews")

TF = 1×5 logical array

1 0 1 0 1

str(TF)

ans = 1×3 string


"Sanchez" "de Ponte" "Nash"

Sort String Arrays

You can sort string arrays. MATLAB® stores characters as Unicode® using the UTF-16 character
encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order.
For the characters that are also the ASCII characters, this order means that uppercase letters come
before lowercase letters. Digits and some punctuation also come before letters.

Sort the string array str.

sort(str)

ans = 1×5 string


"Crosby" "Jones" "Nash" "Sanchez" "de Ponte"

Sort a 2-by-3 string array. The sort function sorts the elements in each column separately.

sort(str2)

ans = 2×3 string


"Jupiter" "Mars" "Apollo"
"Mercury" "Saturn" "Neptune"

To sort the elements in each row, sort str2 along the second dimension.

sort(str2,2)

ans = 2×3 string


"Apollo" "Mars" "Mercury"
"Jupiter" "Neptune" "Saturn"

6-34
Compare Text

Compare Character Vectors

You can compare character vectors and cell arrays of character vectors to each other. Use the
strcmp function to compare two character vectors, or strncmp to compare the first N characters.
You also can use strcmpi and strncmpi for case-insensitive comparisons.

Compare two character vectors with the strcmp function. chr1 and chr2 are not equal.

chr1 = 'hello';
chr2 = 'help';
TF = strcmp(chr1,chr2)

TF = logical
0

Note that the MATLAB strcmp differs from the C version of strcmp. The C version of strcmp
returns 0 when two character arrays are the same, not when they are different.

Compare the first two characters with the strncmp function. TF is 1 because both character vectors
start with the characters he.

TF = strncmp(chr1,chr2,2)

TF = logical
1

Compare two cell arrays of character vectors. strcmp returns a logical array that is the same size as
the cell arrays.

C1 = {'pizza'; 'chips'; 'candy'};


C2 = {'pizza'; 'chocolate'; 'pretzels'};
strcmp(C1,C2)

ans = 3×1 logical array

1
0
0

Inspect Characters in String and Character Arrays

You can inspect the characters in string arrays or character arrays with the isstrprop, isletter,
and isspace functions.

• The isstrprop inspects characters in either string arrays or character arrays.


• The isletter and isspace functions inspect characters in character arrays only.

Determine which characters in a character vector are space characters. isspace returns a logical
vector that is the same size as chr.

chr = 'Four score and seven years ago';


TF = isspace(chr)

TF = 1×30 logical array

6-35
6 Characters and Strings

0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0

The isstrprop function can query characters for many different traits. isstrprop can determine
whether characters in a string or character vector are letters, alphanumeric characters, decimal or
hexadecimal digits, or punctuation characters.

Determine which characters in a string are punctuation marks. isstrprop returns a logical vector
whose length is equal to the number of characters in str.

str = "A horse! A horse! My kingdom for a horse!"

str =
"A horse! A horse! My kingdom for a horse!"

isstrprop(str,"punct")

ans = 1×41 logical array

0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

Determine which characters in the character vector chr are letters.

isstrprop(chr,"alpha")

ans = 1×30 logical array

1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1

See Also
strcmp | sort | isstrprop | isletter | isspace | eq | ne | gt | ge | le | lt

Related Examples
• “Text in String and Character Arrays” on page 6-2
• “Create String Arrays” on page 6-5
• “Analyze Text Data with String Arrays” on page 6-15
• “Search and Replace Text” on page 6-37
• “Test for Empty Strings and Missing Values” on page 6-20

6-36
Search and Replace Text

Search and Replace Text

Processing text data often involves finding and replacing substrings. There are several functions that
find text and return different information: some functions confirm that the text exists, while others
count occurrences, find starting indices, or extract substrings. These functions work on character
vectors and string scalars, such as "yes", as well as character and string arrays, such as
["yes","no";"abc","xyz"]. In addition, you can use patterns to define rules for searching, such as
one or more letter or digit characters.

Search for Text

To determine if text is present, use a function that returns logical values, like contains,
startsWith, or endsWith. Logical values of 1 correspond to true, and 0 corresponds to false.

txt = "she sells seashells by the seashore";


TF = contains(txt,"sea")

TF = logical
1

Calculate how many times the text occurs using the count function.

n = count(txt,"sea")

n =
2

To locate where the text occurs, use the strfind function, which returns starting indices.

idx = strfind(txt,"sea")

idx = 1×2

11 28

Find and extract text using extraction functions, such as extract, extractBetween,
extractBefore, or extractAfter.

mid = extractBetween(txt,"sea","shore")

mid =
"shells by the sea"

Optionally, include the boundary text.

mid = extractBetween(txt,"sea","shore","Boundaries","inclusive")

mid =
"seashells by the seashore"

Find Text in Arrays

The search and replacement functions can also find text in multi-element arrays. For example, look
for color names in several song titles.

6-37
6 Characters and Strings

songs = ["Yellow Submarine";


"Penny Lane";
"Blackbird"];

colors =["Red","Yellow","Blue","Black","White"];

TF = contains(songs,colors)

TF = 3×1 logical array

1
0
1

To list the songs that contain color names, use the logical TF array as indices into the original songs
array. This technique is called logical indexing.
colorful = songs(TF)

colorful = 2×1 string


"Yellow Submarine"
"Blackbird"

Use the function replace to replace text in songs that matches elements of colors with the string
"Orange".
replace(songs,colors,"Orange")

ans = 3×1 string


"Orange Submarine"
"Penny Lane"
"Orangebird"

Match Patterns

Since R2020b

In addition to searching for literal text, like “sea” or “yellow”, you can search for text that matches a
pattern. There are many predefined patterns, such as digitsPattern to find numeric digits.
address = "123a Sesame Street, New York, NY 10128";
nums = extract(address,digitsPattern)

nums = 2×1 string


"123"
"10128"

For additional precision in searches, you can combine patterns. For example, locate words that start
with the character “S”. Use a string to specify the “S” character, and lettersPattern to find
additional letters after that character.
pat = "S" + lettersPattern;
StartWithS = extract(address,pat)

StartWithS = 2×1 string


"Sesame"

6-38
Search and Replace Text

"Street"

For more information, see “Build Pattern Expressions” on page 6-40.

See Also
contains | extract | count | pattern | replace | strfind

Related Examples
• “Text in String and Character Arrays” on page 6-2
• “Build Pattern Expressions” on page 6-40
• “Test for Empty Strings and Missing Values” on page 6-20
• “Regular Expressions” on page 2-38

6-39
6 Characters and Strings

Build Pattern Expressions

Since R2020b

Patterns are a tool to aid in searching for and modifying text. Similar to regular expressions, a
pattern defines rules for matching text. Patterns can be used with text-searching functions like
contains, matches, and extract to specify which portions of text these functions act on. You can
build a pattern expression in a way similar to how you would build a mathematical expression, using
pattern functions, operators, and literal text. Because building pattern expressions is open ended,
patterns can become quite complicated. Building patterns in steps and using functions like
maskedPattern and namedPattern can help organize complicated patterns.

Building Simple Patterns

The simplest pattern is built from a single pattern function. For example, lettersPattern matches
any letter characters. There are many pattern functions for matching different types of characters
and other features of text. A list of these functions can be found on the pattern reference page.
txt = "abc123def";
pat = lettersPattern;
extract(txt,pat)

ans = 2×1 string


"abc"
"def"

Patterns combine with other patterns and literal text by using the plus(+) operator. This operator
appends patterns and text together in the order they are defined in the pattern expression. The
combined patterns only match text in the same order. In this example, "YYYY/MM/DD" is not a match
because a four-letter string must be at the end of the text.
txt = "Dates can be expressed as MM/DD/YYYY, DD/MM/YYYY, or YYYY/MM/DD";
pat = lettersPattern(2) + "/" + lettersPattern(2) + "/" + lettersPattern(4);
extract(txt,pat)

ans = 2×1 string


"MM/DD/YYYY"
"DD/MM/YYYY"

Patterns used with the or(|) operator specify that only one of the two specified patterns needs to
match a section of text. If neither pattern is able to match then the pattern expression fails to match.
txt = "123abc";
pat = lettersPattern|digitsPattern;
extract(txt,pat)

ans = 2×1 string


"123"
"abc"

Some pattern functions take patterns as their input and modify them in some way. For example,
optionalPattern makes a specified pattern match if possible, but the pattern is not required for a
successful match.

6-40
Build Pattern Expressions

txt = ["123abc" "abc"];


pat = optionalPattern(digitsPattern) + lettersPattern;
extract(txt,pat)

ans = 1×2 string


"123abc" "abc"

Boundary Patterns

Boundary patterns are a special type of pattern that do not match characters but rather match the
boundaries between a designated character type and other characters or the start or end of that
piece of text. For example, digitBoundary matches the boundaries between digit characters and
nondigit characters and between digit characters and the start or end of the text. It does not match
digit characters themselves. Boundary patterns are useful as delimiters for functions like split.

txt = "123abc";
pat = digitBoundary;
split(txt,pat)

ans = 3×1 string


""
"123"
"abc"

Boundary patterns are special amongst patterns because they can be negated using the not(~)
operator. When negated in this way, boundary patterns match before or after characters that did not
satisfy the requirements above. For example, ~digitBoundary matches the boundary between:

• characters that are both digits


• characters that are both nondigits
• a nondigit character and the start or end of a piece of text

Use replace to mark the locations matched by ~digitBoundary with a "|" character.

txt = "123abc";
pat = ~digitBoundary;
replace(txt,pat,"|")

ans =
"1|2|3a|b|c|"

Building Complicated Patterns in Steps

Sometimes a simple pattern is not sufficient to solve a problem and a more complicated pattern is
needed. As a pattern expression grows it can become difficult to understand what it is matching. One
way to simplify building a complicated pattern is building each part of the pattern separately and
then combining the parts together into a single pattern expression.

For instance, email addresses use the form [email protected]. Each of the three identifiers —
local_part, domain, and TLD — must be a combination of digits, letters and underscore characters. To
build the full pattern, start by defining a pattern for the identifiers. Build a pattern that matches one
letter or digit character or one underscore character.

identCharacters = alphanumericsPattern(1) | "_";

6-41
6 Characters and Strings

Now, use asManyOfPattern to match one or more consecutive instances of identCharacters.

identifier = asManyOfPattern(identCharacters,1);

Next, build a pattern that matches an email containing multiple identifiers.

emailPattern = identifier + "@" + identifier + "." + identifier;

Test the pattern by seeing how well it matches the following example emails.

exampleEmails = ["[email protected]"
"[email protected]"
"[email protected]"];
matches(exampleEmails,emailPattern)

ans = 3×1 logical array

1
0
0

The pattern fails to match several of the example emails even though all the emails are valid. Both the
local_part and domain can be made of a series of identifiers that are separated by periods. Use the
identifier pattern to build a pattern that is capable of matching a series of identifiers.
asManyOfPattern matches as many concurrent appearances of the specified pattern as possible,
but if there are none the rest of the pattern is still able to match successfully.

identifierSeries = asManyOfPattern(identifier + ".") + identifier;

Use this pattern to build a new emailPattern that can match all of the example emails.

emailPattern = identifierSeries + "@" + identifierSeries + "." + identifier;


matches(exampleEmails,emailPattern)

ans = 3×1 logical array

1
1
1

Organizing Pattern Display

Complex patterns can sometimes be difficult to read and interpret, especially by those you share
them with who are unfamiliar with the pattern's structure. For example, when displayed,
emailPattern is long and difficult to read.

emailPattern

emailPattern = pattern
Matching:

asManyOfPattern(asManyOfPattern(alphanumericsPattern(1) | "_",1) + ".") + asManyOfPattern(alp

Part of the issue with the display is that there are many repetitions of the identifier pattern. If the
exact details of this pattern are not important to users of the pattern, then the display of the

6-42
Build Pattern Expressions

identifier pattern can be concealed using maskedPattern. This function creates a new pattern
where the display of identifier is masked and the variable name, "identifier", is displayed
instead. Alternatively, you can specify a different name to be displayed. The details of patterns that
are masked in this way can be accessed by clicking "Show all details" in the displayed pattern.
identifier = maskedPattern(identifier);
identifierSeries = asManyOfPattern(identifier + ".") + identifier

identifierSeries = pattern
Matching:

asManyOfPattern(identifier + ".") + identifier

Show all details

Patterns can be further organized using the namedPattern function. namedPattern designates a
pattern as a named pattern that changes how the pattern is displayed when combined with other
patterns. Email addresses have several important portions, [email protected], which each have
their own matching rules. Create a named pattern for each section.
localPart = namedPattern(identifierSeries,"local_part");

Named patterns can be nested, to further delineate parts of a pattern. To nest a named pattern, build
a pattern using named patterns and then designate that pattern as a named pattern. For example,
Domain.TLD can be divided into the domain, subdomains, and the top level domain (TLD). Create
named patterns for each part of domain.TLD.
subdomain = namedPattern(identifierSeries,"subdomain");
domainName = namedPattern(identifier,"domainName");
tld = namedPattern(identifier,"TLD");

Nest the named patterns for the components of domain underneath a single named pattern domain.
domain = optionalPattern(subdomain + ".") + ...
domainName + "." + ...
tld;
domain = namedPattern(domain);

Combine the patterns together into a single named pattern, emailPattern. In the display of
emailPattern you can see each named pattern and what they match as well as the information on
any nested named patterns.
emailPattern = localPart + "@" + domain

emailPattern = pattern
Matching:

local_part + "@" + domain

Using named patterns:

local_part : asManyOfPattern(identifier + ".") + identifier


domain : optionalPattern(subdomain + ".") + domainName + "." + TLD
subdomain : asManyOfPattern(identifier + ".") + identifier
domainName: identifier
TLD : identifier

6-43
6 Characters and Strings

Show all details

You can access named patterns and nested named patterns by dot-indexing into a pattern. For
example, you can access the nested named pattern subdomain by dot-indexing from emailPattern
into domain and then dot-indexing again into subdomain.

emailPattern.domain.subdomain

ans = pattern
Matching:

asManyOfPattern(identifier + ".") + identifier

Show all details

Dot-assignment can be used to change named patterns without needing to rewrite the rest of the
pattern expression.

emailPattern.domain = "mathworks.com"

emailPattern = pattern
Matching:

local_part + "@" + domain

Using named patterns:

local_part: asManyOfPattern(identifier + ".") + identifier


domain : "mathworks.com"

Show all details

Copyright 2020 The MathWorks, Inc.

See Also
pattern | string, " " | regexp | contains | replace | extract

More About
• “Search and Replace Text” on page 6-37
• “Regular Expressions” on page 2-38

6-44
Convert Numeric Values to Text

Convert Numeric Values to Text

This example shows how to convert numeric values to text and append them to larger pieces of text.
For example, you might want to add a label or title to a plot, where the label includes a number that
describes a characteristic of the plot.

Convert to Strings

To convert a number to a string that represents it, use the string function.

str = string(pi)

str =
"3.1416"

The string function converts a numeric array to a string array having the same size.

A = [256 pi 8.9e-3];
str = string(A)

str = 1×3 string


"256" "3.141593" "0.0089"

You can specify the format of the output text using the compose function, which accepts format
specifiers for precision, field width, and exponential notation.

str = compose("%9.7f",pi)

str =
"3.1415927"

If the input is a numeric array, then compose returns a string array. Return a string array that
represents numbers using exponential notation.

A = [256 pi 8.9e-3];
str = compose("%5.2e",A)

str = 1×3 string


"2.56e+02" "3.14e+00" "8.90e-03"

Add Numbers to Strings

The simplest way to combine text and numbers is to use the plus operator (+). This operator
automatically converts numeric values to strings when the other operands are strings.

For example, plot a sine wave. Calculate the frequency of the wave and add a string representing that
value in the title of the plot.

X = linspace(0,2*pi);
Y = sin(X);
plot(X,Y)
freq = 1/(2*pi);
str = "Sine Wave, Frequency = " + freq + " Hz"

6-45
6 Characters and Strings

str =
"Sine Wave, Frequency = 0.15915 Hz"

title(str)

Sometimes existing text is stored in character vectors or cell arrays of character vectors. However,
the plus operator also automatically converts those types of data to strings when another operand is
a string. To combine numeric values with those types of data, first convert the numeric values to
strings, and then use plus to combine the text.

str = 'Sine Wave, Frequency = ' + string(freq) + {' Hz'}

str =
"Sine Wave, Frequency = 0.15915 Hz"

Character Codes

If your data contains integers that represent Unicode® values, use the char function to convert the
values to the corresponding characters. The output is a character vector or array.

u = [77 65 84 76 65 66];
c = char(u)

c =
'MATLAB'

Converting Unicode values also allows you to include special characters in text. For instance, the
Unicode value for the degree symbol is 176. To add char(176) to a string, use plus.

deg = char(176);
temp = 21;
str = "Temperature: " + temp + deg + "C"

6-46
Convert Numeric Values to Text

str =
"Temperature: 21°C"

Hexadecimal and Binary Values

You can represent hexadecimal and binary values in your code either using text or using literals. The
recommended way to represent them is to write them as literals. You can write hexadecimal and
binary literals using the 0x and 0b prefixes respectively. However, it can sometimes be useful to
represent such values as text, using the dec2hex or dec2bin functions.

For example, set a bit in a binary value. If you specify the binary value using a literal, then it is stored
as an integer. After setting one of the bits, display the new binary value as text using the dec2bin
function.

register = 0b10010110

register = uint8

150

register = bitset(register,5,0)

register = uint8

134

binStr = dec2bin(register)

binStr =
'10000110'

See Also
dec2bin | dec2hex | char | string | compose | plus

More About
• “Convert Text to Numeric Values” on page 6-48
• “Hexadecimal and Binary Values” on page 6-54
• “Convert Between Text and datetime or duration Values” on page 7-54
• “Formatting Text” on page 6-24
• “Unicode and ASCII Values” on page 6-52

6-47
6 Characters and Strings

Convert Text to Numeric Values

This example shows how to convert text to the numeric values that it represents. Typically, you need
to perform such conversions when you have text that represents numbers to be plotted or used in
calculations. For example, the text might come from a text file or spreadsheet. If you did not already
convert it to numeric values when importing it into MATLAB®, you can use the functions shown in
this example.

You can convert string arrays, character vectors, and cell arrays of character vectors to numeric
values. Text can represent hexadecimal or binary values, though when you convert them to numbers
they are stored as decimal values. You can also convert text representing dates and time to
datetime or duration values, which can be treated like numeric values.

Double-Precision Values

The recommended way to convert text to double-precision values is to use the str2double function.
It can convert character vectors, string arrays, and cell arrays of character vectors.

For example, create a character vector using single quotes and convert it to the number it represents.

X = str2double('3.1416')

X =
3.1416

If the input argument is a string array or cell array of character vectors, then str2double converts
it to a numeric array having the same size. You can create strings using double quotes. (Strings have
the string data type, while character vectors have the char data type.)

str = ["2.718","3.1416";
"137","0.015"]

str = 2×2 string


"2.718" "3.1416"
"137" "0.015"

X = str2double(str)

X = 2×2

2.7180 3.1416
137.0000 0.0150

The str2double function can convert text that includes commas (as thousands separators) and
decimal points. For example, you can use str2double to convert the Balance variable in the table
below. Balance represents numbers as strings, using a comma as the thousands separator.

load balances
balances

balances=3×2 table
Customer Balance
_________ ___________

6-48
Convert Text to Numeric Values

"Diaz" "13,790.00"
"Johnson" "2,456.10"
"Wu" "923.71"

T.Balance = str2double(T.Balance)

T=3×2 table
Customer Balance
_________ _______

"Diaz" 13790
"Johnson" 2456.1
"Wu" 923.71

If str2double cannot convert text to a number, then it returns a NaN value.

While the str2num function can also convert text to numbers, it is not recommended. str2num uses
the eval function, which can cause unintended side effects when the text input includes a function
name. To avoid these issues, use str2double.

As an alternative, you can convert strings to double-precision values using the double function. If the
input is a string array, then double returns a numeric array that has the same size, just as
str2double does. However, if the input is a character vector, then double converts the individual
characters to numbers representing their Unicode® values.

X = double("3.1416")

X =
3.1416

X = double('3.1416')

X = 1×6

51 46 49 52 49 54

This list summarizes the best practices for converting text to numeric values.

• To convert text to numeric values, use the str2double function. It treats string arrays, character
vectors, and cell arrays of character vectors consistently.
• You can also use the double function for string arrays. However, it treats character vectors
differently.
• Avoid str2num. It calls the eval function which can have unintended consequences.

Hexadecimal and Binary Values

You can represent hexadecimal and binary numbers as text or as literals. When you write them as
literals, you must use the 0x and 0b prefixes. When you represent them as text and then convert
them, you can use the prefixes, but they are not required.

For example, write a hexadecimal number as a literal. The prefix is required.

D = 0x3FF

6-49
6 Characters and Strings

D = uint16

1023

Then convert text representing the same value by using the hex2dec function. It recognizes the
prefix but does not require it.

D = hex2dec('3FF')

D =
1023

D = hex2dec('0x3FF')

D =
1023

Convert text representing binary values using the bin2dec function.

D = bin2dec('101010')

D =
42

D = bin2dec('0b101010')

D =
42

Dates and Times

MATLAB provides the datetime and duration data types to store dates and times, and to treat
them as numeric values. To convert text representing dates and times, use the datetime and
duration functions.

Convert text representing a date to a datetime value. The datetime function recognizes many
common formats for dates and times.

C = '2019-09-20'

C =
'2019-09-20'

D = datetime(C)

D = datetime
20-Sep-2019

You can convert arrays representing dates and times.

str = ["2019-01-31","2019-02-28","2019-03-31"]

str = 1×3 string


"2019-01-31" "2019-02-28" "2019-03-31"

D = datetime(str)

6-50
Convert Text to Numeric Values

D = 1×3 datetime
31-Jan-2019 28-Feb-2019 31-Mar-2019

If you convert text to duration values, then use the hh:mm:ss or dd:hh:mm:ss formats.

D = duration('12:34:56')

D = duration
12:34:56

See Also
bin2dec | hex2dec | str2double | datetime | duration | double | table

More About
• “Convert Numeric Values to Text” on page 6-45
• “Convert Between Text and datetime or duration Values” on page 7-54
• “Hexadecimal and Binary Values” on page 6-54
• “Formatting Text” on page 6-24
• “Unicode and ASCII Values” on page 6-52

6-51
6 Characters and Strings

Unicode and ASCII Values

MATLAB® stores all characters as Unicode® characters using the UTF-16 encoding, where every
character is represented by a numeric code value. (Unicode incorporates the ASCII character set as
the first 128 symbols, so ASCII characters have the same numeric codes in Unicode and ASCII.) Both
character arrays and string arrays use this encoding. You can convert characters to their numeric
code values by using various numeric conversion functions. You can convert numbers to characters
using the char function.

Convert Characters to Numeric Code Values

You can convert characters to integers that represent their Unicode code values. To convert a single
character or a character array, use any of these functions:

• double
• uint16, uint32, or uint64

The best practice is to use the double function. However, if you need to store the numeric values as
integers, use unsigned integers having at least 16 bits because MATLAB uses the UTF-16 encoding.

Convert a character vector to Unicode code values using the double function.

C = 'MATLAB'

C =
'MATLAB'

unicodeValues = double(C)

unicodeValues = 1×6

77 65 84 76 65 66

You cannot convert characters in a string array directly to Unicode code values. In particular, the
double function converts strings to the numbers they represent, just as the str2double function
does. If double cannot convert a string to a number, then it returns a NaN value.

str = "MATLAB";
double(str)

ans =
NaN

To convert characters in a string, first convert the string to a character vector, or use curly braces to
extract the characters. Then convert the characters using a function such as double.

C = char(str);
unicodeValues = double(C)

unicodeValues = 1×6

77 65 84 76 65 66

6-52
Unicode and ASCII Values

Convert Numeric Code Values to Characters

You can convert Unicode values to characters using the char function.

D = [77 65 84 76 65 66]

D = 1×6

77 65 84 76 65 66

C = char(D)

C =
'MATLAB'

A typical use for char is to create characters you cannot type and append them to strings. For
example, create the character for the degree symbol and append it to a string. The Unicode code
value for the degree symbol is 176.

deg = char(176)

deg =
'°'

myLabel = append("Current temperature is 21",deg,"C")

myLabel =
"Current temperature is 21°C"

For more information on Unicode, including mappings between characters and code values, see
Unicode.

See Also
char | double | single | string | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 |
uint64

More About
• “Convert Text to Numeric Values” on page 6-48
• “Convert Numeric Values to Text” on page 6-45

External Websites
• Unicode

6-53
6 Characters and Strings

Hexadecimal and Binary Values

You can represent numbers as hexadecimal or binary values. In some contexts, these representations
of numbers are more convenient. For example, you can represent the bits of a hardware register
using binary values. In MATLAB®, there are two ways to represent hexadecimal and binary values:

• As literals. Starting in R2019b, you can write hexadecimal and binary values as literals using an
appropriate prefix as notation. For example, 0x2A is a literal that specifies 42—and MATLAB
stores it as a number, not as text.
• As strings or character vectors. For example, the character vector '2A' represents the number 42
as a hexadecimal value. When you represent a hexadecimal or binary value using text, enclose it
in quotation marks. MATLAB stores this representation as text, not a number.

MATLAB provides several functions for converting numbers to and from their hexadecimal and binary
representations.

Write Integers Using Hexadecimal and Binary Notation

Hexadecimal literals start with a 0x or 0X prefix, while binary literals start with a 0b or 0B prefix.
MATLAB stores the number written with this notation as an integer. For example, these two literals
both represent the integer 42.

A = 0x2A

A = uint8

42

B = 0b101010

B = uint8

42

Do not use quotation marks when you write a number using this notation. Use 0-9, A-F, and a-f to
represent hexadecimal digits. Use 0 and 1 to represent binary digits.

By default, MATLAB stores the number as the smallest unsigned integer type that can accommodate
it. However, you can use an optional suffix to specify the type of integer that stores the value.

• To specify unsigned 8-, 16-, 32-, and 64-bit integer types, use the suffixes u8, u16, u32, and u64.
• To specify signed 8-, 16-, 32-, and 64-bit integer types, use the suffixes s8, s16, s32, and s64.

For example, write a hexadecimal literal to be stored as a signed 32-bit integer.

A = 0x2As32

A = int32

42

When you specify signed integer types, you can write literals that represent negative numbers.
Represent negative numbers in two's complement form. For example, specify a negative number with
a literal using the s8 suffix.

6-54
Hexadecimal and Binary Values

A = 0xFFs8

A = int8

-1

Because MATLAB stores these literals as numbers, you can use them in any context or function where
you use numeric arrays. For example, you can create a 64-bit signed integer array without a loss of
precision for large integers.
C = [0xFF000000001F123As64 0x1234FFFFFFFFFFFs64]

C = 1×2 int64 row vector

-72057594035891654 81997179153022975

For comparison, when you convert an array of large integers (larger than flintmax) using int64,
precision can be lost because MATLAB initially represents a numeric array input as double precision
by default.
C_inaccurate = int64([-72057594035891654 81997179153022975])

C_inaccurate = 1×2 int64 row vector

-72057594035891656 81997179153022976

Represent Hexadecimal and Binary Values as Text

You can also convert integers to character vectors that represent them as hexadecimal or binary
values using the dec2hex and dec2bin functions. Convert an integer to hexadecimal.
hexStr = dec2hex(255)

hexStr =
'FF'

Convert an integer to binary.


binStr = dec2bin(16)

binStr =
'10000'

Since these functions produce text, use them when you need text that represents numeric values. For
example, you can append these values to a title or a plot label, or write them to a file that stores
numbers as their hexadecimal or binary representations.

Represent Arrays of Hexadecimal Values as Text

The recommended way to convert an array of numbers to text is to use the compose function. This
function returns a string array having the same size as the input numeric array. To produce
hexadecimal format, use %X as the format specifier.
A = [255 16 12 1024 137]

A = 1×5

6-55
6 Characters and Strings

255 16 12 1024 137

hexStr = compose("%X",A)

hexStr = 1×5 string


"FF" "10" "C" "400" "89"

The dec2hex and dec2bin functions also convert arrays of numbers to text representing them as
hexadecimal or binary values. However, these functions return character arrays, where each row
represents a number from the input numeric array, padded with zeros as necessary.

Convert Binary Representations to Hexadecimal

To convert a binary value to hexadecimal, start with a binary literal, and convert it to text
representing its hexadecimal value. Since a literal is interpreted as a number, you can specify it
directly as the input argument to dec2hex.

D = 0b1111;
hexStr = dec2hex(D)

hexStr =
'F'

If you start with a hexadecimal literal, then you can convert it to text representing its binary value
using dec2bin.

D = 0x8F;
binStr = dec2bin(D)

binStr =
'10001111'

Bitwise Operations with Binary Values

One typical use of binary numbers is to represent bits. For example, many devices have registers that
provide access to a collection of bits representing data in memory or the status of the device. When
working with such hardware you can use numbers in MATLAB to represent the value in a register.
Use binary values and bitwise operations to represent and access particular bits.

Create a number that represents an 8-bit register. It is convenient to start with binary representation,
but the number is stored as an integer.

register = 0b10010110

register = uint8

150

To get or set the values of particular bits, use bitwise operations. For example, use the bitand and
bitshift functions to get the value of the fifth bit. (Shift that bit to the first position so that
MATLAB returns a 0 or 1. In this example, the fifth bit is a 1.)

b5 = bitand(register,0b10000);
b5 = bitshift(b5,-4)

6-56
Hexadecimal and Binary Values

b5 = uint8

To flip the fifth bit to 0, use the bitset function.

register = bitset(register,5,0)

register = uint8

134

Since register is an integer, use the dec2bin function to display all the bits in binary format.
binStr is a character vector, and represents the binary value without a leading 0b prefix.

binStr = dec2bin(register)

binStr =
'10000110'

See Also
bin2dec | bitand | bitshift | bitset | dec2bin | dec2hex | hex2dec | sprintf | sscanf

More About
• “Convert Text to Numeric Values” on page 6-48
• “Convert Numeric Values to Text” on page 6-45
• “Formatting Text” on page 6-24
• “Bit-Wise Operations” on page 2-25
• “Perform Cyclic Redundancy Check” on page 2-31

External Websites
• Two's Complement

6-57
6 Characters and Strings

Frequently Asked Questions About String Arrays


You can use string arrays to work with text throughout MathWorks products. String arrays store
pieces of text and provide a set of functions for working with text as data. You can index into,
reshape, and concatenate strings arrays just as you can with arrays of any other type. For more
information, see “Create String Arrays” on page 6-5.

In most respects, strings arrays behave like character vectors and cell arrays of character vectors.
However, there are a few key differences between string arrays and character arrays that can lead to
results you might not expect. For each of these differences, there is a recommended way to use
strings that leads to the expected result.

Why Does Using Command Form With Strings Return An Error?


When you use functions such as the cd, dir, copyfile, or load functions in command form, avoid
using double quotes. In command form, arguments enclosed in double quotes can result in errors. To
specify arguments as strings, use functional form.

With command syntax, you separate inputs with spaces rather than commas, and you do not enclose
input arguments in parentheses. For example, you can use the cd function with command syntax to
change folders.

cd C:\Temp

The text C:\Temp is a character vector. In command form, all arguments are always character
vectors. If you have an argument, such as a folder name, that contains spaces, then specify it as one
input argument by enclosing it in single quotes.

cd 'C:\Program Files'

But if you specify the argument using double quotes, then cd throws an error.

cd "C:\Program Files"

Error using cd
Too many input arguments.

The error message can vary depending on the function that you use and the arguments that you
specify. For example, if you use the load function with command syntax and specify the argument
using double quotes, then load throws a different error.

load "myVariables.mat"

Error using load


Unable to read file '"myVariables.mat"': Invalid argument.

In command form, double quotes are treated as part of the literal text rather than as the string
construction operator. If you wrote the equivalent of cd "C:\Program Files" in functional form,
then it would look like a call to cd with two arguments.

cd('"C:\Program','Files"')

When specifying arguments as strings, use function syntax. All functions that support command
syntax also support function syntax. For example, you can use cd with function syntax and input
arguments that are double quoted strings.

6-58
Frequently Asked Questions About String Arrays

cd("C:\Program Files")

Why Do Strings in Cell Arrays Return an Error?


When you have multiple strings, store them in a string array, not a cell array. Create a string array
using square brackets, not curly braces. String arrays are more efficient than cell arrays for storing
and manipulating text.
str = ["Venus","Earth","Mars"]

str = 1×3 string array


"Venus" "Earth" "Mars"

Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages
that come from using string arrays. And in fact, most functions do not accept cell arrays of strings as
input arguments, options, or values of name-value pairs. For example, if you specify a cell array of
strings as an input argument, then the contains function throws an error.
C = {"Venus","Earth","Mars"}

C = 1×3 cell array


{["Venus"]} {["Earth"]} {["Mars"]}

TF = contains(C,"Earth")

Error using contains


First argument must be a string array, character vector, or cell array of character vectors.

Instead, specify the argument as a string array.


str = ["Venus","Earth","Mars"];
TF = contains(str,"Earth");

Cell arrays can contain variables having any data types, including strings. It is still possible to create
a cell array whose elements all contain strings. And if you already have specified cell arrays of
character vectors in your code, then replacing single quotes with double quotes might seem like a
simple update. However, it is not recommended that you create or use cell arrays of strings.

Why Does length() of String Return 1?


It is common to use the length function to determine the number of characters in a character vector.
But to determine the number of characters in a string, use the strlength function, not length.

Create a character vector using single quotes. To determine its length, use the length function.
Because C is a vector, its length is equal to the number of characters. C is a 1-by-11 vector.
C = 'Hello world';
L = length(C)

L = 11

Create a string with the same characters, using double quotes. Though it stores 11 characters, str is
a 1-by-1 string array, or string scalar. If you call length on a string scalar, then the output argument is
1, no matter how many characters it stores.
str = "Hello World";
L = length(str)

6-59
6 Characters and Strings

L = 1

To determine the number of characters in a string, use the strlength function. For compatibility,
strlength operates on character vectors as well. In both cases strlength returns the number of
characters.
L = strlength(C)

L = 11

L = strlength(str)

L = 11

You also can use strlength on string arrays containing multiple strings and on cell arrays of
character vectors.

The length function returns the size of the longest dimension of an array. For a string array, length
returns the number of strings along the longest dimension of the array. It does not return the number
of characters within strings.

Why Does isempty("") Return 0?


A string can have no characters at all. Such a string is an empty string. You can specify an empty
string using an empty pair of double quotes.
L = strlength("")

L = 0

However, an empty string is not an empty array. An empty string is a string scalar that happens to
have no characters.
sz = size("")

sz = 1×2
1 1

If you call isempty on an empty string, then it returns 0 (false) because the string is not an empty
array.
tf = isempty("")

tf = logical
0

However, if you call isempty on an empty character array, then it returns 1 (true). A character
array specified as an empty pair of single quotes, '', is a 0-by-0 character array.
tf = isempty('')

tf = logical
1

To test whether a piece of text has no characters, the best practice is to use the strlength function.
You can use the same call whether the input is a string scalar or a character vector.
str = "";
if strlength(str) == 0

6-60
Frequently Asked Questions About String Arrays

disp('String has no text')


end

String has no text

chr = '';
if strlength(chr) == 0
disp('Character vector has no text')
end

Character vector has no text

Why Does Appending Strings Using Square Brackets Return Multiple


Strings?
You can append text to a character vector using square brackets. But if you add text to a string array
using square brackets, then the new text is concatenated as new elements of the string array. To
append text to strings, use the plus operator or the strcat function.

For example, if you concatenate two strings, then the result is a 1-by-2 string array.

str = ["Hello" "World"]

str = 1×2 string array


"Hello" "World"

However, if you concatenate two character vectors, then the result is a longer character vector.

str = ['Hello' 'World']

chr = 'HelloWorld'

To append text to a string (or to the elements of a string array), use the plus operator instead of
square brackets.

str = "Hello" + "World"

str = "HelloWorld"

As an alternative, you can use the strcat function. strcat appends text whether the input
arguments are strings or character vectors.

str = strcat("Hello","World")

str = "HelloWorld"

Whether you use square brackets, plus, or strcat, you can specify an arbitrary number of
arguments. Append a space character between Hello and World.

str = "Hello" + " " + "World"

str = "Hello World"

See Also
string | strlength | contains | plus | strcat | sprintf | dir | cd | copyfile | load | length
| size | isempty

6-61
6 Characters and Strings

Related Examples
• “Create String Arrays” on page 6-5
• “Test for Empty Strings and Missing Values” on page 6-20
• “Compare Text” on page 6-32
• “Update Your Code to Accept Strings” on page 6-63

6-62
Update Your Code to Accept Strings

Update Your Code to Accept Strings


If you write code for other MATLAB users, then it is to your advantage to update your API to accept
string arrays, while maintaining backward compatibility with other text data types. String adoption
makes your code consistent with MathWorks products.

If your code has few dependencies, or if you are developing new code, then consider using string
arrays as your primary text data type for better performance. In that case, best practice is to write or
update your API to accept input arguments that are character vectors, cell arrays of character
vectors, or string arrays.

For the definitions of string array and other terms, see “Terminology for Character and String Arrays”
on page 6-69.

What Are String Arrays?


In MATLAB, you can store text data in two ways. One way is to use a character array, which is a
sequence of characters, just as a numeric array is a sequence of numbers. The other way is to store a
sequence of characters in a string. You can store multiple strings in a string array. For more
information, see “Characters and Strings”.

Recommended Approaches for String Adoption in Old APIs


When your code has many dependencies, and you must maintain backward compatibility, follow these
approaches for updating functions and classes to present a compatible API.

Functions

• Accept string arrays as input arguments.

• If an input argument can be either a character vector or a cell array of character vectors, then
update your code so that the argument also can be a string array. For example, consider a
function that has an input argument you can specify as a character vector (using single
quotes). Best practice is to update the function so that the argument can be specified as either
a character vector or a string scalar (using double quotes).
• Accept strings as both names and values in name-value pair arguments.

• In name-value pair arguments, allow names to be specified as either character vectors or


strings—that is, with either single or double quotes around the name. If a value can be a
character vector or cell array of character vectors, then update your code so that it also can be
a string array.
• Do not accept cell arrays of string arrays for text input arguments.

• A cell array of string arrays has a string array in each cell. For example, {"hello","world"}
is a cell array of string arrays. While you can create such a cell array, it is not recommended
for storing text. The elements of a string array have the same data type and are stored
efficiently. If you store strings in a cell array, then you lose the advantages of using a string
array.

However, if your code accepts heterogeneous cell arrays as inputs, then consider accepting cell
arrays that contain strings. You can convert any strings in such a cell array to character
vectors.

6-63
6 Characters and Strings

• In general, do not change the output type.

• If your function returns a character vector or cell array of character vectors, then do not
change the output type, even if the function accepts string arrays as inputs. For example, the
fileread function accepts an input file name specified as either a character vector or a
string, but the function returns the file contents as a character vector. By keeping the output
type the same, you can maintain backward compatibility.
• Return the same data type when the function modifies input text.

• If your function modifies input text and returns the modified text as the output argument, then
the input and output arguments should have the same data type. For example, the lower
function accepts text as the input argument, converts it to all lowercase letters, and returns it.
If the input argument is a character vector, then lower returns a character vector. If the input
is a string array, then lower returns a string array.
• Consider adding a 'TextType' argument to import functions.

• If your function imports data from files, and at least some of that data can be text, then
consider adding an input argument that specifies whether to return text as a character array or
a string array. For example, the readtable function provides the 'TextType' name-value
pair argument. This argument specifies whether readtable returns a table with text in cell
arrays of character vectors or string arrays.

Classes

• Treat methods as functions.

• For string adoption, treat methods as though they are functions. Accept string arrays as input
arguments, and in general, do not change the data type of the output arguments, as described
in the previous section.
• Do not change the data types of properties.

• If a property is a character vector or a cell array of character vectors, then do not change its
type. When you access such a property, the value that is returned is still a character vector or a
cell array of character vectors.

As an alternative, you can add a new property that is a string, and make it dependent on the
old property to maintain compatibility.
• Set properties using string arrays.

• If you can set a property using a character vector or cell array of character vectors, then
update your class to set that property using a string array too. However, do not change the
data type of the property. Instead, convert the input string array to the data type of the
property, and then set the property.
• Add a string method.

• If your class already has a char and/or a cellstr method, then add a string method. If you
can represent an object of your class as a character vector or cell array of character vectors,
then represent it as a string array too.

6-64
Update Your Code to Accept Strings

How to Adopt String Arrays in Old APIs


You can adopt strings in old APIs by accepting string arrays as input arguments, and then converting
them to character vectors or cell arrays of character vectors. If you perform such a conversion at the
start of a function, then you do not need to update the rest of it.

The convertStringsToChars function provides a way to process all input arguments, converting
only those arguments that are string arrays. To enable your existing code to accept string arrays as
inputs, add a call to convertStringsToChars at the beginnings of your functions and methods.

For example, if you have defined a function myFunc that accepts three input arguments, process all
three inputs using convertStringsToChars. Leave the rest of your code unaltered.
function y = myFunc(a,b,c)
[a,b,c] = convertStringsToChars(a,b,c);
<line 1 of original code>
<line 2 of original code>
...

In this example, the arguments [a,b,c] overwrite the input arguments in place. If any input
argument is not a string array, then it is unaltered.

If myFunc accepts a variable number of input arguments, then process all the arguments specified by
varargin.
function y = myFunc(varargin)
[varargin{:}] = convertStringsToChars(varargin{:});
...

Performance Considerations

The convertStringsToChars function is more efficient when converting one input argument. If
your function is performance sensitive, then you can convert input arguments one at a time, while
still leaving the rest of your code unaltered.
function y = myFunc(a,b,c)
a = convertStringsToChars(a);
b = convertStringsToChars(b);
c = convertStringsToChars(c);
...

Recommended Approaches for String Adoption in New Code


When your code has few dependencies, or you are developing entirely new code, consider using
strings arrays as the primary text data type. String arrays provide good performance and efficient
memory usage when working with large amounts of text. Unlike cell arrays of character vectors,
string arrays have a homogeneous data type. String arrays make it easier to write maintainable code.
To use string arrays while maintaining backward compatibility to other text data types, follow these
approaches.

Functions

• Accept any text data types as input arguments.

• If an input argument can be a string array, then also allow it to be a character vector or cell
array of character vectors.

6-65
6 Characters and Strings

• Accept character arrays as both names and values in name-value pair arguments.

• In name-value pair arguments, allow names to be specified as either character vectors or


strings—that is, with either single or double quotes around the name. If a value can be a string
array, then also allow it to be a character vector or cell array of character vectors.
• Do not accept cell arrays of string arrays for text input arguments.

• A cell array of string arrays has a string array in each cell. While you can create such a cell
array, it is not recommended for storing text. If your code uses strings as the primary text data
type, store multiple pieces of text in a string array, not a cell array of string arrays.

However, if your code accepts heterogeneous cell arrays as inputs, then consider accepting cell
arrays that contain strings.
• In general, return strings.

• If your function returns output arguments that are text, then return them as string arrays.
• Return the same data type when the function modifies input text.

• If your function modifies input text and returns the modified text as the output argument, then
the input and output arguments should have the same data type.

Classes

• Treat methods as functions.

• Accept character vectors and cell arrays of character vectors as input arguments, as described
in the previous section. In general, return strings as outputs.
• Specify properties as string arrays.

• If a property contains text, then set the property using a string array. When you access the
property, return the value as a string array.

How to Maintain Compatibility in New Code


When you write new code, or modify code to use string arrays as the primary text data type, maintain
backward compatibility with other text data types. You can accept character vectors or cell arrays of
character vectors as input arguments, and then immediately convert them to string arrays. If you
perform such a conversion at the start of a function, then the rest of your code can use string arrays
only.

The convertCharsToStrings function provides a way to process all input arguments, converting
only those arguments that are character vectors or cell arrays of character vectors. To enable your
new code to accept these text data types as inputs, add a call to convertCharsToStrings at the
beginnings of your functions and methods.

For example, if you have defined a function myFunc that accepts three input arguments, process all
three inputs using convertCharsToStrings.

function y = myFunc(a,b,c)
[a,b,c] = convertCharsToStrings(a,b,c);
<line 1 of original code>
<line 2 of original code>
...

6-66
Update Your Code to Accept Strings

In this example, the arguments [a,b,c] overwrite the input arguments in place. If any input
argument is not a character vector or cell array of character vectors, then it is unaltered.

If myFunc accepts a variable number of input arguments, then process all the arguments specified by
varargin.

function y = myFunc(varargin)
[varargin{:}] = convertCharsToStrings(varargin{:});
...

Performance Considerations

The convertCharsToStrings function is more efficient when converting one input argument. If
your function is performance sensitive, then you can convert input arguments one at a time, while
still leaving the rest of your code unaltered.

function y = myFunc(a,b,c)
a = convertCharsToStrings(a);
b = convertCharsToStrings(b);
c = convertCharsToStrings(c);
...

How to Manually Convert Input Arguments


If it is at all possible, avoid manual conversion of input arguments that contain text, and instead use
the convertStringsToChars or convertCharsToStrings functions. Checking the data types of
input arguments and converting them yourself is a tedious approach, prone to errors.

If you must convert input arguments, then use the functions in this table.

Conversion Function
String scalar to character vector char
String array to cell array of character vectors cellstr
Character vector to string scalar string
Cell array of character vectors to string array string

How to Check Argument Data Types


To check the data type of an input argument that could contain text, consider using the patterns
shown in this table.

Required Input Argument Old Check New Check


Type
Character vector or string ischar(X) ischar(X) ||
scalar isStringScalar(X)
validateattributes(X,
{'char','string'},
{'scalartext'})

6-67
6 Characters and Strings

Required Input Argument Old Check New Check


Type
Character vector or string validateattributes(X, validateattributes(X,
scalar {'char'},{'row'}) {'char','string'},
{'scalartext'})
Nonempty character vector or ischar(X) && ~isempty(X) (ischar(X) ||
string scalar isStringScalar(X)) &&
strlength(X) ~= 0
(ischar(X) ||
isStringScalar(X)) && X
~= ""
Cell array of character vectors iscellstr(X) iscellstr(X) ||
or string array isstring(X)
Any text data type ischar(X) || ischar(X) ||
iscellstr(X) iscellstr(X) ||
isstring(X)

Check for Empty Strings

An empty string is a string with no characters. MATLAB displays an empty string as a pair of double
quotes with nothing between them (""). However, an empty string is still a 1-by-1 string array. It is
not an empty array.

The recommended way to check whether a string is empty is to use the strlength function.
str = "";
tf = (strlength(str) ~= 0)

Note Do not use the isempty function to check for an empty string. An empty string has no
characters but is still a 1-by-1 string array.

The strlength function returns the length of each string in a string array. If the string must be a
string scalar, and also not empty, then check for both conditions.
tf = (isStringScalar(str) && strlength(str) ~= 0)

If str could be either a character vector or string scalar, then you still can use strlength to
determine its length. strlength returns 0 if the input argument is an empty character vector ('').
tf = ((ischar(str) || isStringScalar(str)) && strlength(str) ~= 0)

Check for Empty String Arrays

An empty string array is, in fact, an empty array—that is, an array that has at least one dimension
whose length is 0.

The recommended way to create an empty string array is to use the strings function, specifying 0
as at least one of the input arguments. The isempty function returns 1 when the input is an empty
string array.
str = strings(0);
tf = isempty(str)

6-68
Update Your Code to Accept Strings

The strlength function returns a numeric array that is the same size as the input string array. If the
input is an empty string array, then strlength returns an empty array.

str = strings(0);
L = strlength(str)

Check for Missing Strings

String arrays also can contain missing strings. The missing string is the string equivalent to NaN for
numeric arrays. It indicates where a string array has missing values. The missing string displays as
<missing>, with no quotation marks.

You can create missing strings using the missing function. The recommended way to check for
missing strings is to use the ismissing function.

str = string(missing);
tf = ismissing(str)

Note Do not check for missing strings by comparing a string to the missing string.

The missing string is not equal to itself, just as NaN is not equal to itself.

str = string(missing);
f = (str == missing)

Terminology for Character and String Arrays


MathWorks documentation uses these terms to describe character and string arrays. For consistency,
use these terms in your own documentation, error messages, and warnings.

• Character vector — 1-by-n array of characters, of data type char.


• Character array — m-by-n array of characters, of data type char.
• Cell array of character vectors — Cell array in which each cell contains a character vector.
• String or string scalar — 1-by-1 string array. A string scalar can contain a 1-by-n sequence of
characters, but is itself one object. Use the terms "string scalar" and "character vector" alongside
each other when to be precise about size and data type. Otherwise, you can use the term "string"
in descriptions.
• String vector — 1-by-n or n-by-1 string array. If only one size is possible, then use it in your
description. For example, use "1-by-n string array" to describe an array of that size.
• String array — m-by-n string array.
• Empty string — String scalar that has no characters.
• Empty string array — String array with at least one dimension whose size is 0.
• Missing string — String scalar that is the missing value (displays as <missing>).

See Also
char | cellstr | string | strings | convertStringsToChars | convertCharsToStrings |
isstring | isStringScalar | ischar | iscellstr | strlength | validateattributes |
convertContainedStringsToChars

6-69
6 Characters and Strings

More About
• “Create String Arrays” on page 6-5
• “Test for Empty Strings and Missing Values” on page 6-20
• “Compare Text” on page 6-32
• “Search and Replace Text” on page 6-37
• “Frequently Asked Questions About String Arrays” on page 6-58

6-70
7

Dates and Time

• “Represent Dates and Times in MATLAB” on page 7-2


• “Specify Time Zones” on page 7-5
• “Convert Date and Time to Julian Date or POSIX Time” on page 7-12
• “Set Date and Time Display Format” on page 7-15
• “Generate Sequence of Dates and Time” on page 7-19
• “Share Code and Data Across Locales” on page 7-24
• “Extract or Assign Date and Time Components of Datetime Array” on page 7-27
• “Combine Date and Time from Separate Variables” on page 7-30
• “Date and Time Arithmetic” on page 7-32
• “Compare Dates and Time” on page 7-37
• “Plot Dates and Times” on page 7-44
• “Core Functions Supporting Date and Time Arrays” on page 7-53
• “Convert Between Text and datetime or duration Values” on page 7-54
• “Replace Discouraged Instances of Serial Date Numbers and Date Strings” on page 7-63
• “Carryover in Date Vectors and Strings” on page 7-70
• “Converting Date Vector Returns Unexpected Output” on page 7-72
7 Dates and Time

Represent Dates and Times in MATLAB


The primary way to store date and time information is in datetime arrays, which support arithmetic,
sorting, comparisons, plotting, and formatted display. The results of arithmetic differences are
returned in duration arrays or, when you use calendar-based functions, in calendarDuration
arrays.

For example, create a MATLAB datetime array that represents two dates: June 28, 2014 at 6 a.m. and
June 28, 2014 at 7 a.m. Specify numeric values for the year, month, day, hour, minute, and second
components for the datetime.

t = datetime(2014,6,28,6:7,0,0)

t =
28-Jun-2014 06:00:00 28-Jun-2014 07:00:00

Change the value of a date or time component by assigning new values to the properties of the
datetime array. For example, change the day number of each datetime by assigning new values to the
Day property.

t.Day = 27:28

t =

27-Jun-2014 06:00:00 28-Jun-2014 07:00:00

Change the display format of the array by changing its Format property. The following format does
not display any time components. However, the values in the datetime array do not change.

t.Format = 'MMM dd, yyyy'

t =
Jun 27, 2014 Jun 28, 2014

If you subtract one datetime array from another, the result is a duration array in units of fixed
length.

t2 = datetime(2014,6,29,6,30,45)

t2 =

29-Jun-2014 06:30:45

d = t2 - t

d =

48:30:45 23:30:45

By default, a duration array displays in the format, hours:minutes:seconds. Change the display
format of the duration by changing its Format property. You can display the duration value with a
single unit, such as hours.

d.Format = 'h'

d =

48.512 hrs 23.512 hrs

7-2
Represent Dates and Times in MATLAB

You can create a duration in a single unit using the seconds, minutes, hours, days, or years
functions. For example, create a duration of 2 days, where each day is exactly 24 hours.

d = days(2)

d =
2 days

You can create a calendar duration in a single unit of variable length. For example, one month can be
28, 29, 30, or 31 days long. Specify a calendar duration of 2 months.

L = calmonths(2)

L =
2mo

Use the caldays, calweeks, calquarters, and calyears functions to specify calendar durations
in other units.

Add a number of calendar months and calendar days. The number of days remains separate from the
number of months because the number of days in a month is not fixed, and cannot be determined
until you add the calendar duration to a specific datetime.

L = calmonths(2) + caldays(35)

L =
2mo 35d

Add calendar durations to a datetime to compute a new date.

t2 = t + calmonths(2) + caldays(35)

t2 =

Oct 01, 2014 Oct 02, 2014

t2 is also a datetime array.

whos t2

Name Size Bytes Class Attributes

t2 1x2 161 datetime

In summary, there are several ways to represent dates and times, and MATLAB has a data type for
each approach:

• Represent a point in time, using the datetime data type.


Example: Wednesday, June 18, 2014 10:00:00
• Represent a length of time, or a duration in units of fixed length, using the duration data type.
When using the duration data type, 1 day is always equal to 24 hours, and 1 year is always equal
to 365.2425 days.
Example: 72 hours and 10 minutes
• Represent a length of time, or a duration in units of variable length, using the
calendarDuration data type.
Example: 1 month, which can be 28, 29, 30, or 31 days long.

7-3
7 Dates and Time

The calendarDuration data type also accounts for daylight saving time changes and leap years,
so that 1 day might be more or less than 24 hours, and 1 year can have 365 or 366 days.

See Also
datetime | duration | calendarDuration

7-4
Specify Time Zones

Specify Time Zones

In MATLAB®, you can specify time zones for datetime arrays. This example shows how to create
and work with datetime arrays that include time zones.

A time zone is a geographic area that observes a uniform standard time. Time zones include time
offsets from Coordinated Universal Time (UTC), time offsets from daylight saving time (DST), and a
set of historical changes to those offsets. To set a time zone and calculate time zone offsets, the
datetime data type uses code and data provided by the Internet Assigned Numbers Authority (IANA)
in the IANA Time Zone Database.

Display and Search Table of Time Zones

To see a complete table of time zones that are known to MATLAB, use the timezones function. Every
row of the table shows the name of a time zone, its geographic area, its offset from UTC, and its offset
due to DST. For time zones that observe DST, the DST offset is applied according to the current and
historical rules for those time zones.

AllTimeZones = timezones

AllTimeZones=445×4 table
Name Area UTCOffset DSTOffset
______________________ ______ _________ _________

{'Africa/Abidjan' } Africa 0 0
{'Africa/Accra' } Africa 0 0
{'Africa/Addis_Ababa'} Africa 3 0
{'Africa/Algiers' } Africa 1 0
{'Africa/Asmera' } Africa 3 0
{'Africa/Bamako' } Africa 0 0
{'Africa/Bangui' } Africa 1 0
{'Africa/Banjul' } Africa 0 0
{'Africa/Bissau' } Africa 0 0
{'Africa/Blantyre' } Africa 2 0
{'Africa/Brazzaville'} Africa 1 0
{'Africa/Bujumbura' } Africa 2 0
{'Africa/Cairo' } Africa 2 1
{'Africa/Casablanca' } Africa 0 1
{'Africa/Ceuta' } Africa 1 1
{'Africa/Conakry' } Africa 0 0

To see the version of the IANA Time Zone Database that MATLAB uses, return the second output from
timezones.

[~,DBversion] = timezones

DBversion =
'2024b'

To find the name of a time zone, you can search the Name variable of the table of time zones. To find
the whole name when you know part of the name, you can use the contains function. For example,
find the name of the time zone that corresponds to New York. Replace space characters with
underscores in your search string.

7-5
7 Dates and Time

TFindex = contains(AllTimeZones.Name,"New_York");
NewYorkZone = AllTimeZones.Name(TFindex)

NewYorkZone = 1×1 cell array


{'America/New_York'}

Display the corresponding row of the time zones table. To match the exact name of the time zone, use
the matches function.

TFindex = matches(AllTimeZones.Name,NewYorkZone);
AllTimeZones(TFindex,:)

ans=1×4 table
Name Area UTCOffset DSTOffset
____________________ _______ _________ _________

{'America/New_York'} America -5 1

Create datetime Values with Time Zones

Every datetime array has a time zone property. By default, this property is not set, which means the
resulting datetime array is unzoned. You can use unzoned datetime arrays for local time
calculations in which you do not need to consider DST or local times in other time zones.

For example, create a datetime value for the current time, and display its TimeZone property. The
current date and time values come from your system clock. Without a time zone, the datetime value
cannot calculate the time zone offset relative to UTC.

D = datetime("now")

D = datetime
02-Feb-2025 02:16:43

D.TimeZone

ans =

0×0 empty char array

There are two ways to set the time zone of a datetime array. The first way is to specify the
TimeZone name-value argument of the datetime function when creating the datetime array.
Specifying this argument sets the value of the TimeZone property.

D = datetime("now",TimeZone="America/New_York")

D = datetime
02-Feb-2025 02:16:43

D.TimeZone

ans =
'America/New_York'

The second way is to assign a value to the TimeZone property after you have created the datetime
array.

7-6
Specify Time Zones

D = datetime("now")

D = datetime
02-Feb-2025 02:16:43

D.TimeZone = "America/New_York"

D = datetime
02-Feb-2025 02:16:43

You can also specify the TimeZone property as a duration that is just a fixed time zone offset from
UTC. Such an offset does not incorporate any current or historical offset rules, such as daylight
saving time.

For example, specify a time zone offset that is five hours behind UTC. This offset is the same as the
UTC offset for America/New_York, but it does not include any offset for daylight saving time.

D = datetime("now",TimeZone="-05:00")

D = datetime
02-Feb-2025 02:16:43

D.TimeZone

ans =
'-05:00'

You can specify TimeZone as any value in this list:

• "" — No time zone


• Time zone name — Time zone from IANA Time Zone Database
• Time zone offset in +HH:mm or -HH:mm format — Fixed offset from UTC
• Time zone offset as duration scalar (since R2024a) — Fixed offset from UTC, specified using the
hours, minutes, seconds, or duration function
• "UTC" — Coordinated Universal Time
• "UTCLeapSeconds" — Coordinated Universal Time, but also incorporating leap seconds
• "local" — IANA time zone that corresponds to the system time zone

Specify Formats That Include Time Zone Offsets

The default format for datetime arrays does not include the time zone. However, you can include the
time zone offset in the format by using the z or Z identifiers.

For example, change the format to include the date, time, and time zone offset using z. The z
identifier specifies the short localized version of the offset. Its behavior depends on your locale.

D = datetime("now", ...
TimeZone="America/New_York", ...
Format="dd-MMM-uuuu HH:mm:ss z")

D = datetime
02-Feb-2025 02:16:43 EST

7-7
7 Dates and Time

The Z identifier specifies a basic format that displays the offset as hours, minutes, and optionally
seconds.
D.Format = "dd-MMM-uuuu HH:mm:ss Z"

D = datetime
02-Feb-2025 02:16:43 -0500

You can also specify the long UTC format. For a complete list of time zone offset identifiers, see
datetime.
D.Format = "dd-MMM-uuuu HH:mm:ss ZZZZ"

D = datetime
02-Feb-2025 02:16:43 UTC-05:00

Encode Same Time in Different Time Zone

If you change the time zone of a datetime value, it still encodes the same point in time. If the offset
from UTC changes, then the date and time values change in a way that compensates for the change
in the offset.

For example, create a datetime value in the New York time zone, and format it to display the time
zone offset.
D = datetime("today", ...
TimeZone="America/New_York", ...
Format="dd-MMM-uuuu HH:mm:ss z")

D = datetime
02-Feb-2025 00:00:00 EST

Then change its time zone to the zone for Los Angeles. The date and time values change to encode
the same point in time in a different time zone.
D.TimeZone = "America/Los_Angeles"

D = datetime
01-Feb-2025 21:00:00 PST

Compare datetime Values with Different Time Zones

If you compare datetime values that have different time zones, then the comparison takes the time
zone offsets into account. However, you cannot compare zoned and unzoned datetime arrays
because an unzoned array has no known time zone offset. Both arrays must be zoned or unzoned.

For example, create a datetime value. Then copy it and change its time zone.
NYTime = datetime("today", ...
TimeZone="America/New_York", ...
Format="dd-MMM-uuuu HH:mm:ss z")

NYTime = datetime
02-Feb-2025 00:00:00 EST

7-8
Specify Time Zones

LATime = NYTime;
LATime.TimeZone = "America/Los_Angeles"

LATime = datetime
01-Feb-2025 21:00:00 PST

Compare the two values using the == operator. The values are equal because they encode the same
point in time.

AreTimesEqual = NYTime == LATime

AreTimesEqual = logical
1

There is no actual difference between the two times.

TimeDiff = LATime - NYTime

TimeDiff = duration
00:00:00

Then add two hours to the Los Angeles time.

LATime = LATime + hours(2)

LATime = datetime
01-Feb-2025 23:00:00 PST

Compare the two times. Despite their date and time values, the time in Los Angeles occurs later than
the time in New York.

IsLATimeLater = LATime > NYTime

IsLATimeLater = logical
1

The difference between the times is two hours.

TimeDiff = LATime - NYTime

TimeDiff = duration
02:00:00

Concatenate datetime Arrays with Different Time Zones

The TimeZone property applies to every element in a datetime array. However, you can concatenate
datetime arrays that have different time zones. The concatenated array has the same time zone as
the first array. You cannot concatenate zoned and unzoned datetime arrays because the unzoned
array has no known time zone offset. Both arrays must be zoned or unzoned.

For example, concatenate NYTime and LATime. The result has the time zone for New York.

combinedNYZone = [NYTime LATime]

7-9
7 Dates and Time

combinedNYZone = 1×2 datetime


02-Feb-2025 00:00:00 EST 02-Feb-2025 02:00:00 EST

Then concatenate in the reverse order. The result has the time zone for Los Angeles.

combinedLAZone = [LATime NYTime]

combinedLAZone = 1×2 datetime


01-Feb-2025 23:00:00 PST 01-Feb-2025 21:00:00 PST

Account for Leap Seconds Using Special Time Zone

A leap second is a one-second adjustment that is applied to UTC. Leap seconds were introduced in
1972 to account for the difference between precise time based on atomic clocks and observed solar
time, which varies due to small changes in earth's rotation rate. Those changes do not come in a
predictable pattern, so leap seconds have been declared as needed. The leap second data
incorporated into MATLAB is provided by the International Earth Rotation and Reference Systems
Service (IERS). For more information, see the IERS Bulletins.

The datetime data type has a special time zone that accounts for leap seconds. For any calculations
or comparisons that involve leap seconds, specify the time zone as "UTCLeapSeconds". When you
use this time zone, the default format includes the date, time, and the letter Z to indicate UTC,
according to the ISO 8601 standard.

todayLS = datetime("today",TimeZone="UTCLeapSeconds")

todayLS = datetime
2025-02-02T00:00:00.000Z

You cannot combine or compare datetime arrays when one array has leap seconds and the other
array does not.

One way to see the effect of leap seconds is to calculate the length of time between today's date and
January 1, 1972, in the UTC and UTCLeapSeconds time zones. First calculate the duration in the UTC
zone. The duration is shown in hh:mm:ss format.

durationWithoutLS = datetime("today",TimeZone="UTC") - datetime(1972,1,1,TimeZone="UTC")

durationWithoutLS = duration
465384:00:00

Then calculate the duration in the UTCLeapSeconds zone. The difference between the two durations
is due to the cumulative effect of the leap seconds declared since 1972.

durationWithLS = datetime("today",TimeZone="UTCLeapSeconds") - datetime(1972,1,1,TimeZone="UTCLea

durationWithLS = duration
465384:00:27

To see all the leap seconds known to MATLAB and the dates they were declared, use the
leapseconds function.

LS = leapseconds

7-10
Specify Time Zones

LS=27×2 timetable
Date Type CumulativeAdjustment
___________ ____ ____________________

30-Jun-1972 + 1 sec
31-Dec-1972 + 2 sec
31-Dec-1973 + 3 sec
31-Dec-1974 + 4 sec
31-Dec-1975 + 5 sec
31-Dec-1976 + 6 sec
31-Dec-1977 + 7 sec
31-Dec-1978 + 8 sec
31-Dec-1979 + 9 sec
30-Jun-1981 + 10 sec
30-Jun-1982 + 11 sec
30-Jun-1983 + 12 sec
30-Jun-1985 + 13 sec
31-Dec-1987 + 14 sec
31-Dec-1989 + 15 sec
31-Dec-1990 + 16 sec

The second output returns the IERS Bulletin C version number of the leap second data used in
MATLAB.

[~,LSvers] = leapseconds

LSvers =
68

See Also
datetime | timezones | leapseconds

Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Set Date and Time Display Format” on page 7-15
• “Convert Between Text and datetime or duration Values” on page 7-54
• “Share Code and Data Across Locales” on page 7-24
• “Convert Date and Time to Julian Date or POSIX Time” on page 7-12

7-11
7 Dates and Time

Convert Date and Time to Julian Date or POSIX Time

You can convert datetime arrays to represent points in time in specialized numeric formats. In
general, these formats represent a point in time as the number of seconds or days that have elapsed
since a specified starting point. For example, the Julian date is the number of days and fractional days
that have elapsed since the beginning of the Julian period. The POSIX® time is the number of
seconds that have elapsed since 00:00:00 1-Jan-1970 UTC (Coordinated Universal Time). MATLAB®
provides the juliandate and posixtime functions to convert datetime arrays to Julian dates and
POSIX times.

While datetime arrays are not required to have a time zone, converting "unzoned" datetime values
to Julian dates or POSIX times can lead to unexpected results. To ensure the expected result, specify
the time zone before conversion.

Specify Time Zone Before Conversion

You can specify a time zone for a datetime array, but you are not required to do so. In fact, by
default the datetime function creates an "unzoned" datetime array.

Create a datetime value for the current date and time.

d = datetime("now")

d = datetime
02-Feb-2025 03:44:04

d is constructed from the local time on your machine and has no time zone associated with it. In many
contexts, you might assume that you can treat the times in an unzoned datetime array as local
times. However, the juliandate and posixtime functions treat the times in unzoned datetime
arrays as UTC times, not local times. To avoid any ambiguity, it is recommended that you avoid using
juliandate and posixtime on unzoned datetime arrays. For example, avoid using
posixtime(datetime("now")) in your code.

If your datetime array has values that do not represent UTC times, specify the time zone using the
TimeZone name-value pair argument so that juliandate and posixtime interpret the datetime
values correctly.

d = datetime("now","TimeZone","America/New_York")

d = datetime
02-Feb-2025 03:44:04

As an alternative, you can specify the TimeZone property after you create the array.

d.TimeZone = "America/Los_Angeles"

d = datetime
02-Feb-2025 00:44:04

To see a complete list of time zones, use the timezones function.

7-12
Convert Date and Time to Julian Date or POSIX Time

Convert Zoned and Unzoned Datetime Values to Julian Dates

A Julian date is the number of days (including fractional days) since noon on November 24, 4714
BCE, in the proleptic Gregorian calendar, or January 1, 4713 BCE, in the proleptic Julian calendar. To
convert datetime arrays to Julian dates, use the juliandate function.

Create a datetime array and specify its time zone.

DZ = datetime("2016-07-29 10:05:24") + calmonths(1:3);


DZ.TimeZone = "America/New_York"

DZ = 1×3 datetime
29-Aug-2016 10:05:24 29-Sep-2016 10:05:24 29-Oct-2016 10:05:24

Convert D to the equivalent Julian dates.

format longG
JDZ = juliandate(DZ)

JDZ = 1×3

2457630.08708333 2457661.08708333 2457691.08708333

Create an unzoned copy of DZ. Convert D to the equivalent Julian dates. As D has no time zone,
juliandate treats the times as UTC times.

D = DZ;
D.TimeZone = "";
JD = juliandate(D)

JD = 1×3

2457629.92041667 2457660.92041667 2457690.92041667

Compare JDZ and JD. The differences are equal to the time zone offset between UTC and the
America/New_York time zone in fractional days.

JDZ - JD

ans = 1×3

0.166666666511446 0.166666666511446 0.166666666511446

Convert Zoned and Unzoned Datetime Values to POSIX Times

The POSIX time is the number of seconds (including fractional seconds) elapsed since 00:00:00 1-
Jan-1970 UTC (Coordinated Universal Time), ignoring leap seconds. To convert datetime arrays to
POSIX times, use the posixtime function.

Create a datetime array and specify its time zone.

DZ = datetime("2016-07-29 10:05:24") + calmonths(1:3);


DZ.TimeZone = "America/New_York"

7-13
7 Dates and Time

DZ = 1×3 datetime
29-Aug-2016 10:05:24 29-Sep-2016 10:05:24 29-Oct-2016 10:05:24

Convert D to the equivalent POSIX times.

PTZ = posixtime(DZ)

PTZ = 1×3

1472479524 1475157924 1477749924

Create an unzoned copy of DZ. Convert D to the equivalent POSIX times. As D has no time zone,
posixtime treats the times as UTC times.

D = DZ;
D.TimeZone = "";
PT = posixtime(D)

PT = 1×3

1472465124 1475143524 1477735524

Compare PTZ and PT. The differences are equal to the time zone offset between UTC and the
America/New_York time zone in seconds.

PTZ - PT

ans = 1×3

14400 14400 14400

See Also
datetime | timezones | posixtime | juliandate

Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Specify Time Zones” on page 7-5

7-14
Set Date and Time Display Format

Set Date and Time Display Format

In this section...
“Formats for Individual Date and Duration Arrays” on page 7-15
“datetime Display Format” on page 7-15
“duration Display Format” on page 7-16
“calendarDuration Display Format” on page 7-17
“Default datetime Format” on page 7-17

Formats for Individual Date and Duration Arrays


datetime, duration, and calendarDuration arrays have a Format property that controls the
display of values in each array. When you create a datetime array, it uses the MATLAB global default
datetime display format unless you explicitly provide a format. Use dot notation to access the Format
property to view or change its value. For example, to set the display format for the datetime array,
t, to the default format, type:

t.Format = 'default'

Changing the Format property does not change the values in the array, only their display. For
example, the following can be representations of the same datetime value (the latter two do not
display any time components):

Thursday, August 23, 2012 12:35:00


August 23, 2012
23-Aug-2012

The Format property of the datetime, duration, and calendarDuration data types accepts
different formats as inputs.

datetime Display Format


You can set the Format property to one of these character vectors.

Value of Format Description


'default' Use the default display format.
'defaultdate' Use the default date display format that does not
show time components.

To change the default formats, see “Default datetime Format” on page 7-17.

Alternatively, you can specify a custom date format that includes Unicode characters as literal text.
This table shows several common display formats and examples of the formatted output for the date,
Saturday, April 19, 2014 at 9:41:06 PM in New York City. In such formats you can use nonletter ASCII
characters such as hyphens, spaces, or colons, or any non-ASCII characters, to separate date and
time fields. To include the ASCII letters A-Z and a-z as literal characters in the format, enclose them
in quotation marks.

7-15
7 Dates and Time

Value of Format Example


'yyyy-MM-dd' 2014-04-19
'dd/MM/yyyy' 19/04/2014
'dd.MM.yyyy' 19.04.2014
'yyyy 年 MM 月 dd 日' 2014 年 04 月 19 日
'MMMM d, yyyy' April 19, 2014
'eeee, MMMM d, yyyy h:mm a' Saturday, April 19, 2014 9:41 PM
'MMMM d, yyyy HH:mm:ss Z' April 19, 2014 21:41:06 -0400
'yyyy-MM-dd''T''HH:mmXXX' 2014-04-19T21:41-04:00

For a complete list of valid symbolic identifiers, see the Format property for datetime arrays.

Note The letter identifiers that datetime accepts are different from those used by the datestr,
datenum, and datevec functions.

duration Display Format


To display a duration as a single number that includes a fractional part (for example, 1.234 hours),
specify one of these character vectors:

Value of Format Description


'y' Number of exact fixed-length years. A fixed-length year is
equal to 365.2425 days.
'd' Number of exact fixed-length days. A fixed-length day is equal
to 24 hours.
'h' Number of hours
'm' Number of minutes
's' Number of seconds

To specify the number of fractional digits displayed, use the format function.

To display a duration in the form of a digital timer, specify one of the following character vectors.

• 'dd:hh:mm:ss'
• 'hh:mm:ss'
• 'mm:ss'
• 'hh:mm'

You also can display up to nine fractional second digits by appending up to nine S characters. For
example, 'hh:mm:ss.SSS' displays the milliseconds of a duration value to 3 digits.

Changing the Format property does not change the values in the array, only their display.

7-16
Set Date and Time Display Format

calendarDuration Display Format


Specify the Format property of a calendarDuration array as a character vector that can include
the characters y, q, m, w, d, and t, in this order. The format must include m, d, and t.

This table describes the date and time components that the characters represent.

Character Unit Required?


y Years (multiples of 12 no
months)
q Quarters (multiples of 3 no
months)
m Months yes
w Weeks (multiples of 7 no
days)
d Days yes
t Time (hours, minutes, and yes
seconds)

To specify the number of digits displayed for fractional seconds, use the format function.

If the value of a date or time component is zero, it is not displayed.

Changing the Format property does not change the values in the array, only their display.

Default datetime Format


You can set default formats to control the display of datetime arrays created without an explicit
display format. These formats also apply when you set the Format property of a datetime array to
'default' or 'defaultdate'. When you change the default setting, datetime arrays set to use
the default formats are displayed automatically using the new setting.

Changes to the default formats persist across MATLAB sessions.

To specify a default format, type

datetime.setDefaultFormats('default',fmt)

where fmt is a character vector composed of the letters A-Z and a-z described for the Format
property of datetime arrays, above. For example,

datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss')

sets the default datetime format to include a 4-digit year, 2-digit month number, 2-digit day number,
and hour, minute, and second values.

In addition, you can specify a default format for datetimes created without time components. For
example,

datetime.setDefaultFormats('defaultdate','yyyy-MM-dd')

sets the default date format to include a 4-digit year, 2-digit month number, and 2-digit day number.

7-17
7 Dates and Time

To reset the both the default format and the default date-only formats to the factory defaults, type

datetime.setDefaultFormats('reset')

The factory default formats depend on your system locale.

You also can set the default formats in the MATLAB Settings window. Click Settings on the Home
tab in the Environment section. Select MATLAB > Command Window, and then choose Datetime
format options. For more information, see “Modify Command Window Settings”.

See Also
datetime | duration | calendarDuration | format

7-18
Generate Sequence of Dates and Time

Generate Sequence of Dates and Time


In this section...
“Sequence of Datetime or Duration Values Between Endpoints with Step Size” on page 7-19
“Add Duration or Calendar Duration to Create Sequence of Dates” on page 7-20
“Specify Length and Endpoints of Date or Duration Sequence” on page 7-21
“Sequence of Datetime Values Using Calendar Rules” on page 7-22

Sequence of Datetime or Duration Values Between Endpoints with


Step Size

This example shows how to use the colon (:) operator to generate sequences of datetime or
duration values in the same way that you create regularly spaced numeric vectors.

Use Default Step Size

Create a sequence of datetime values starting from November 1, 2013, and ending on November 5,
2013. The default step size is one calendar day.

t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,5,8,0,0);
t = t1:t2

t = 1×5 datetime
01-Nov-2013 08:00:00 02-Nov-2013 08:00:00 03-Nov-2013 08:00:00 04-Nov-2013 08:00:00 05

Specify Step Size

Specify a step size of 2 calendar days using the caldays function.

t = t1:caldays(2):t2

t = 1×3 datetime
01-Nov-2013 08:00:00 03-Nov-2013 08:00:00 05-Nov-2013 08:00:00

Specify a step size in units other than days. Create a sequence of datetime values spaced 18 hours
apart.

t = t1:hours(18):t2

t = 1×6 datetime
01-Nov-2013 08:00:00 02-Nov-2013 02:00:00 02-Nov-2013 20:00:00 03-Nov-2013 14:00:00 04

Use the years, days, minutes, and seconds functions to create datetime and duration sequences
using other fixed-length date and time units. Create a sequence of duration values between 0 and 3
minutes, incremented by 30 seconds.

d = 0:seconds(30):minutes(3)

7-19
7 Dates and Time

d = 1×7 duration
0 sec 30 sec 60 sec 90 sec 120 sec 150 sec 180 sec

Compare Fixed-Length Duration and Calendar Duration Step Sizes

Assign a time zone to t1 and t2. In the America/New_York time zone, t1 now occurs just before a
daylight saving time change.
t1.TimeZone = 'America/New_York';
t2.TimeZone = 'America/New_York';

If you create the sequence using a step size of one calendar day, then the difference between
successive datetime values is not always 24 hours.
t = t1:t2;
dt = diff(t)

dt = 1×4 duration
24:00:00 25:00:00 24:00:00 24:00:00

Create a sequence of datetime values spaced one fixed-length day apart,


t = t1:days(1):t2

t = 1×5 datetime
01-Nov-2013 08:00:00 02-Nov-2013 08:00:00 03-Nov-2013 07:00:00 04-Nov-2013 07:00:00 05

Verify that the difference between successive datetime values is 24 hours.


dt = diff(t)

dt = 1×4 duration
24:00:00 24:00:00 24:00:00 24:00:00

Integer Step Size

If you specify a step size in terms of an integer, it is interpreted as a number of 24-hour days.
t = t1:1:t2

t = 1×5 datetime
01-Nov-2013 08:00:00 02-Nov-2013 08:00:00 03-Nov-2013 07:00:00 04-Nov-2013 07:00:00 05

Add Duration or Calendar Duration to Create Sequence of Dates

This example shows how to add a duration or calendar duration to a datetime to create a sequence of
datetime values.

Create a datetime scalar representing November 1, 2013, at 8:00 AM.


t1 = datetime(2013,11,1,8,0,0);

Add a sequence of fixed-length hours to the datetime.

7-20
Generate Sequence of Dates and Time

t = t1 + hours(0:2)

t = 1×3 datetime
01-Nov-2013 08:00:00 01-Nov-2013 09:00:00 01-Nov-2013 10:00:00

Add a sequence of calendar months to the datetime.

t = t1 + calmonths(1:5)

t = 1×5 datetime
01-Dec-2013 08:00:00 01-Jan-2014 08:00:00 01-Feb-2014 08:00:00 01-Mar-2014 08:00:00 01

Each datetime in t occurs on the first day of each month.

Verify that the dates in t are spaced 1 month apart.

dt = caldiff(t)

dt = 1×4 calendarDuration
1mo 1mo 1mo 1mo

Determine the number of days between each date.

dt = caldiff(t,'days')

dt = 1×4 calendarDuration
31d 31d 28d 31d

Add a number of calendar months to the date, January 31, 2014, to create a sequence of dates that
fall on the last day of each month.

t = datetime(2014,1,31) + calmonths(0:11)

t = 1×12 datetime
31-Jan-2014 28-Feb-2014 31-Mar-2014 30-Apr-2014 31-May-2014 30-Jun-2014 31-Jul-201

Specify Length and Endpoints of Date or Duration Sequence

This example shows how to use the linspace function to create equally spaced datetime or duration
values between two specified endpoints.

Create a sequence of five equally spaced dates between April 14, 2014, and August 4, 2014. First,
define the endpoints.

A = datetime(2014,04,14);
B = datetime(2014,08,04);

The third input to linspace specifies the number of linearly spaced points to generate between the
endpoints.

C = linspace(A,B,5)

7-21
7 Dates and Time

C = 1×5 datetime
14-Apr-2014 12-May-2014 09-Jun-2014 07-Jul-2014 04-Aug-2014

Create a sequence of six equally spaced durations between 1 and 5.5 hours.

A = duration(1,0,0);
B = duration(5,30,0);
C = linspace(A,B,6)

C = 1×6 duration
01:00:00 01:54:00 02:48:00 03:42:00 04:36:00 05:30:00

Sequence of Datetime Values Using Calendar Rules

This example shows how to use the dateshift function to generate sequences of dates and time
where each instance obeys a rule relating to a calendar unit or a unit of time. For instance, each
datetime must occur at the beginning a month, on a particular day of the week, or at the end of a
minute. The resulting datetime values in the sequence are not necessarily equally spaced.

Dates on Specific Day of Week

Generate a sequence of dates consisting of the next three occurrences of Monday. First, define
today's date.

t1 = datetime('today','Format','dd-MMM-yyyy eee')

t1 = datetime
01-Feb-2025 Sat

The first input to dateshift is always the datetime array from which you want to generate a
sequence. Specify 'dayofweek' as the second input to indicate that the datetime values in the
output sequence must fall on a specific day of the week. You can specify the day of the week either by
number or by name. For example, you can specify Monday either as 2 or 'Monday'.

t = dateshift(t1,'dayofweek',2,1:3)

t = 1×3 datetime
03-Feb-2025 Mon 10-Feb-2025 Mon 17-Feb-2025 Mon

Dates at Start of Month

Generate a sequence of start-of-month dates beginning with April 1, 2014. Specify 'start' as the
second input to dateshift to indicate that all datetime values in the output sequence should fall at
the start of a particular unit of time. The third input argument defines the unit of time, in this case,
month. The last input to dateshift can be an array of integer values that specifies how t1 should be
shifted. In this case, 0 corresponds to the start of the current month, and 4 corresponds to the start
of the fourth month from t1.

t1 = datetime(2014,04,01);
t = dateshift(t1,'start','month',0:4)

7-22
Generate Sequence of Dates and Time

t = 1×5 datetime
01-Apr-2014 01-May-2014 01-Jun-2014 01-Jul-2014 01-Aug-2014

Dates at End of Month

Generate a sequence of end-of-month dates beginning with April 1, 2014.

t1 = datetime(2014,04,01);
t = dateshift(t1,'end','month',0:2)

t = 1×3 datetime
30-Apr-2014 31-May-2014 30-Jun-2014

Determine the number of days between each date.

dt = caldiff(t,'days')

dt = 1×2 calendarDuration
31d 30d

The dates are not equally spaced.

Other Units of Dates and Time

You can specify other units of time such as week, day, and hour.

t1 = datetime('now')

t1 = datetime
01-Feb-2025 10:35:44

t = dateshift(t1,'start','hour',0:4)

t = 1×5 datetime
01-Feb-2025 10:00:00 01-Feb-2025 11:00:00 01-Feb-2025 12:00:00 01-Feb-2025 13:00:00 01

Previous Occurrences of Dates and Time

Generate a sequence of datetime values beginning with the previous hour. Negative integers in the
last input to dateshift correspond to datetime values earlier than t1.

t = dateshift(t1,'start','hour',-1:1)

t = 1×3 datetime
01-Feb-2025 09:00:00 01-Feb-2025 10:00:00 01-Feb-2025 11:00:00

See Also
dateshift | linspace

7-23
7 Dates and Time

Share Code and Data Across Locales

In this section...
“Write Locale-Independent Date and Time Code” on page 7-24
“Write Dates in Other Languages” on page 7-25
“Read Dates in Other Languages” on page 7-25

Write Locale-Independent Date and Time Code

Follow these best practices when sharing code that handles dates and time with MATLAB® users in
other locales. These practices ensure that the same code produces the same output display and that
output files containing dates and time are read correctly on systems in different countries or with
different language settings.

Create language-independent datetime values. That is, create datetime values that use month
numbers rather than month names, such as 01 instead of January. Avoid using day of week names.

For example, do this:

t = datetime('today','Format','yyyy-MM-dd')

t = datetime
2025-02-02

instead of this:

t = datetime('today','Format','eeee, dd-MMM-yyyy')

t = datetime
Sunday, 02-Feb-2025

Display the hour using 24-hour clock notation rather than 12-hour clock notation. Use the 'HH'
identifiers when specifying the display format for datetime values.

For example, do this:

t = datetime('now','Format','HH:mm')

t = datetime
03:13

instead of this:

t = datetime('now','Format','hh:mm a')

t = datetime
03:13 AM

7-24
Share Code and Data Across Locales

When specifying the display format for time zone information, use the Z or X identifiers instead of the
lowercase z to avoid the creation of time zone names that might not be recognized in other languages
or regions.

Assign a time zone to t.

t.TimeZone = 'America/New_York';

Specify a language-independent display format that includes a time zone.

t.Format = 'dd-MM-yyyy Z'

t = datetime
02-02-2025 -0500

If you share files but not code, you do not need to write locale-independent code while you work in
MATLAB. However, when you write to a file, ensure that any text representing dates and times is
language-independent. Then, other MATLAB users can read the files easily without having to specify
a locale in which to interpret date and time data.

Write Dates in Other Languages

Specify an appropriate format for text representing dates and times when you use the char or
cellstr functions. For example, convert two datetime values to a cell array of character vectors
using cellstr. Specify the format and the locale to represent the day, month, and year of each
datetime value as text.

t = [datetime('today');datetime('tomorrow')]

t = 2×1 datetime
02-Feb-2025
03-Feb-2025

S = cellstr(t,'dd. MMMM yyyy','de_DE')

S = 2×1 cell
{'02. Februar 2025'}
{'03. Februar 2025'}

S is a cell array of character vectors representing dates in German. You can export S to a text file to
use with systems in the de_DE locale.

Read Dates in Other Languages


You can read text files containing dates and time in a language other than the language that MATLAB
uses, which depends on your system locale. Use the textscan or readtable functions with the
DateLocale name-value pair argument to specify the locale in which the function interprets the
dates in the file. In addition, you might need to specify the character encoding of a file that contains
characters that are not recognized by your computer's default encoding.

7-25
7 Dates and Time

• When reading text files using the textscan function, specify the file encoding when opening the
file with fopen. The encoding is the fourth input argument to fopen.
• When reading text files using the readtable function, use the FileEncoding name-value pair
argument to specify the character encoding associated with the file.

See Also
datetime | char | cellstr | readtable | textscan

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

Extract or Assign Date and Time Components of Datetime


Array

This example shows two ways to extract date and time components from existing datetime arrays:
accessing the array properties or calling a function. Then, the example shows how to modify the date
and time components by modifying the array properties.

Access Properties to Retrieve Date and Time Component

Create a datetime array.

t = datetime('now') + calyears(0:2) + calmonths(0:2) + hours(20:20:60)

t = 1×3 datetime
02-Feb-2025 13:25:06 03-Mar-2026 09:25:06 04-Apr-2027 05:25:06

Get the year values of each datetime in the array. Use dot notation to access the Year property of t.

t_years = t.Year

t_years = 1×3

2025 2026 2027

The output, t_years, is a numeric array.

Get the month values of each datetime in t by accessing the Month property.

t_months = t.Month

t_months = 1×3

2 3 4

You can retrieve the day, hour, minute, and second components of each datetime in t by accessing the
Hour, Minute, and Second properties, respectively.

Use Functions to Retrieve Date and Time Component

Use the month function to get the month number for each datetime in t. Using functions is an
alternate way to retrieve specific date or time components of t.

m = month(t)

m = 1×3

2 3 4

Use the month function rather than the Month property to get the full month names of each datetime
in t.

m = month(t,'name')

7-27
7 Dates and Time

m = 1×3 cell
{'February'} {'March'} {'April'}

You can retrieve the year, quarter, week, day, hour, minute, and second components of each datetime
in t using the year, quarter, week, hour, minute, and second functions, respectively.

Get the week of year numbers for each datetime in t.

w = week(t)

w = 1×3

6 10 15

Get Multiple Date and Time Components

Use the ymd function to get the year, month, and day values of t as three separate numeric arrays.

[y,m,d] = ymd(t)

y = 1×3

2025 2026 2027

m = 1×3

2 3 4

d = 1×3

2 3 4

Use the hms function to get the hour, minute, and second values of t as three separate numeric
arrays.

[h,m,s] = hms(t)

h = 1×3

13 9 5

m = 1×3

25 25 25

s = 1×3

6.0780 6.0780 6.0780

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

Modify Date and Time Components

Assign new values to components in an existing datetime array by modifying the properties of the
array. Use dot notation to access a specific property.

Change the year number of all datetime values in t to 2014. Use dot notation to modify the Year
property.

t.Year = 2014

t = 1×3 datetime
02-Feb-2014 13:25:06 03-Mar-2014 09:25:06 04-Apr-2014 05:25:06

Change the months of the three datetime values in t to January, February, and March, respectively.
You must specify the new value as a numeric array.

t.Month = [1,2,3]

t = 1×3 datetime
02-Jan-2014 13:25:06 03-Feb-2014 09:25:06 04-Mar-2014 05:25:06

Set the time zone of t by assigning a value to the TimeZone property.

t.TimeZone = 'Europe/Berlin';

Change the display format of t to display only the date, and not the time information.

t.Format = 'dd-MMM-yyyy'

t = 1×3 datetime
02-Jan-2014 03-Feb-2014 04-Mar-2014

If you assign values to a datetime component that are outside the conventional range, MATLAB®
normalizes the components. The conventional range for day of month numbers is from 1 to 31. Assign
day values that exceed this range.

t.Day = [-1 1 32]

t = 1×3 datetime
30-Dec-2013 01-Feb-2014 01-Apr-2014

The month and year numbers adjust so that all values remain within the conventional range for each
date component. In this case, January -1, 2014 converts to December 30, 2013.

See Also
datetime | ymd | hms | week

7-29
7 Dates and Time

Combine Date and Time from Separate Variables


This example shows how to read date and time data from a text file. Then, it shows how to combine
date and time information stored in separate variables into a single datetime variable.

Create a space-delimited text file named schedule.txt that contains the following (to create the
file, use any text editor, and copy and paste):
Date Name Time
10.03.2015 Joe 14:31
10.03.2015 Bob 15:33
11.03.2015 Bob 11:29
12.03.2015 Kim 12:09
12.03.2015 Joe 13:05

Read the file using the readtable function. Use the %D conversion specifier to read the first and
third columns of data as datetime values.
T = readtable('schedule.txt','Format','%{dd.MM.uuuu}D %s %{HH:mm}D','Delimiter',' ')

T =
Date Name Time
__________ _____ _____
10.03.2015 'Joe' 14:31
10.03.2015 'Bob' 15:33
11.03.2015 'Bob' 11:29
12.03.2015 'Kim' 12:09
12.03.2015 'Joe' 13:05

readtable returns a table containing three variables.

Change the display format for the T.Date and T.Time variables to view both date and time
information. Since the data in the first column of the file ("Date") have no time information, the time
of the resulting datetime values in T.Date default to midnight. Since the data in the third column of
the file ("Time") have no associated date, the date of the datetime values in T.Time defaults to the
current date.
T.Date.Format = 'dd.MM.uuuu HH:mm';
T.Time.Format = 'dd.MM.uuuu HH:mm';
T

T =
Date Name Time
________________ _____ ________________
10.03.2015 00:00 'Joe' 12.12.2014 14:31
10.03.2015 00:00 'Bob' 12.12.2014 15:33
11.03.2015 00:00 'Bob' 12.12.2014 11:29
12.03.2015 00:00 'Kim' 12.12.2014 12:09
12.03.2015 00:00 'Joe' 12.12.2014 13:05

Combine the date and time information from two different table variables by adding T.Date and the
time values in T.Time. Extract the time information from T.Time using the timeofday function.
myDatetime = T.Date + timeofday(T.Time)

myDatetime =
10.03.2015 14:31
10.03.2015 15:33

7-30
Combine Date and Time from Separate Variables

11.03.2015 11:29
12.03.2015 12:09
12.03.2015 13:05

See Also
readtable | timeofday

7-31
7 Dates and Time

Date and Time Arithmetic

This example shows how to add and subtract date and time values to calculate future and past dates
and elapsed durations in exact units or calendar units. You can add, subtract, multiply, and divide
date and time arrays in the same way that you use these operators with other MATLAB® data types.
However, there is some behavior that is specific to dates and time.

Add and Subtract Durations to Datetime Array

Create a datetime scalar. By default, datetime arrays are not associated with a time zone.

t1 = datetime('now')

t1 = datetime
01-Feb-2025 13:08:21

Find future points in time by adding a sequence of hours.

t2 = t1 + hours(1:3)

t2 = 1×3 datetime
01-Feb-2025 14:08:21 01-Feb-2025 15:08:21 01-Feb-2025 16:08:21

Verify that the difference between each pair of datetime values in t2 is 1 hour.

dt = diff(t2)

dt = 1×2 duration
01:00:00 01:00:00

diff returns durations in terms of exact numbers of hours, minutes, and seconds.

Subtract a sequence of minutes from a datetime to find past points in time.

t2 = t1 - minutes(20:10:40)

t2 = 1×3 datetime
01-Feb-2025 12:48:21 01-Feb-2025 12:38:21 01-Feb-2025 12:28:21

Add a numeric array to a datetime array. MATLAB treats each value in the numeric array as a
number of exact, 24-hour days.

t2 = t1 + [1:3]

t2 = 1×3 datetime
02-Feb-2025 13:08:21 03-Feb-2025 13:08:21 04-Feb-2025 13:08:21

Add to Datetime with Time Zone

If you work with datetime values in different time zones, or if you want to account for daylight saving
time changes, work with datetime arrays that are associated with time zones. Create a datetime
scalar representing March 8, 2014, in New York.

7-32
Date and Time Arithmetic

t1 = datetime(2014,3,8,0,0,0,'TimeZone','America/New_York')

t1 = datetime
08-Mar-2014

Find future points in time by adding a sequence of fixed-length (24-hour) days.


t2 = t1 + days(0:2)

t2 = 1×3 datetime
08-Mar-2014 00:00:00 09-Mar-2014 00:00:00 10-Mar-2014 01:00:00

Because a daylight saving time shift occurred on March 9, 2014, the third datetime in t2 does not
occur at midnight.

Verify that the difference between each pair of datetime values in t2 is 24 hours.
dt = diff(t2)

dt = 1×2 duration
24:00:00 24:00:00

You can add fixed-length durations in other units such as years, hours, minutes, and seconds by
adding the outputs of the years, hours, minutes, and seconds functions, respectively.

To account for daylight saving time changes, you should work with calendar durations instead of
durations. Calendar durations account for daylight saving time shifts when they are added to or
subtracted from datetime values.

Add a number of calendar days to t1.


t3 = t1 + caldays(0:2)

t3 = 1×3 datetime
08-Mar-2014 09-Mar-2014 10-Mar-2014

View that the difference between each pair of datetime values in t3 is not always 24 hours due to the
daylight saving time shift that occurred on March 9.
dt = diff(t3)

dt = 1×2 duration
24:00:00 23:00:00

Add Calendar Durations to Datetime Array

Add a number of calendar months to January 31, 2014.


t1 = datetime(2014,1,31)

t1 = datetime
31-Jan-2014

t2 = t1 + calmonths(1:4)

7-33
7 Dates and Time

t2 = 1×4 datetime
28-Feb-2014 31-Mar-2014 30-Apr-2014 31-May-2014

Each datetime in t2 occurs on the last day of each month.

Calculate the difference between each pair of datetime values in t2 in terms of a number of calendar
days using the caldiff function.

dt = caldiff(t2,'days')

dt = 1×3 calendarDuration
31d 30d 31d

The number of days between successive pairs of datetime values in dt is not always the same
because different months consist of a different number of days.

Add a number of calendar years to January 31, 2014.

t2 = t1 + calyears(0:4)

t2 = 1×5 datetime
31-Jan-2014 31-Jan-2015 31-Jan-2016 31-Jan-2017 31-Jan-2018

Calculate the difference between each pair of datetime values in t2 in terms of a number of calendar
days using the caldiff function.

dt = caldiff(t2,'days')

dt = 1×4 calendarDuration
365d 365d 366d 365d

The number of days between successive pairs of datetime values in dt is not always the same
because 2016 is a leap year and has 366 days.

You can use the calquarters, calweeks, and caldays functions to create arrays of calendar
quarters, calendar weeks, or calendar days that you add to or subtract from datetime arrays.

Adding calendar durations is not commutative. When you add more than one calendarDuration
array to a datetime, MATLAB adds them in the order in which they appear in the command.

Add 3 calendar months followed by 30 calendar days to January 31, 2014.

t2 = datetime(2014,1,31) + calmonths(3) + caldays(30)

t2 = datetime
30-May-2014

First add 30 calendar days to the same date, and then add 3 calendar months. The result is not the
same because when you add a calendar duration to a datetime, the number of days added depends on
the original date.

t2 = datetime(2014,1,31) + caldays(30) + calmonths(3)

7-34
Date and Time Arithmetic

t2 = datetime
02-Jun-2014

Calendar Duration Arithmetic

Create two calendar durations and then find their sum.


d1 = calyears(1) + calmonths(2) + caldays(20)

d1 = calendarDuration
1y 2mo 20d

d2 = calmonths(11) + caldays(23)

d2 = calendarDuration
11mo 23d

d = d1 + d2

d = calendarDuration
2y 1mo 43d

When you sum two or more calendar durations, a number of months greater than 12 roll over to a
number of years. However, a large number of days does not roll over to a number of months, because
different months consist of different numbers of days.

Increase d by multiplying it by a factor of 2. Calendar duration values must be integers, so you can
multiply them only by integer values.
2*d

ans = calendarDuration
4y 2mo 86d

Calculate Elapsed Time in Exact Units

Subtract one datetime array from another to calculate elapsed time in terms of an exact number of
hours, minutes, and seconds.

Find the exact length of time between a sequence of datetime values and the start of the previous
day.
t2 = datetime('now') + caldays(1:3)

t2 = 1×3 datetime
02-Feb-2025 13:08:23 03-Feb-2025 13:08:23 04-Feb-2025 13:08:23

t1 = datetime('yesterday')

t1 = datetime
31-Jan-2025

dt = t2 - t1

7-35
7 Dates and Time

dt = 1×3 duration
61:08:23 85:08:23 109:08:23

whos dt

Name Size Bytes Class Attributes

dt 1x3 40 duration

dt contains durations in the format, hours:minutes:seconds.

View the elapsed durations in units of days by changing the Format property of dt.

dt.Format = 'd'

dt = 1×3 duration
2.5475 days 3.5475 days 4.5475 days

Scale the duration values by multiplying dt by a factor of 1.2. Because durations have an exact
length, you can multiply and divide them by fractional values.

dt2 = 1.2*dt

dt2 = 1×3 duration


3.057 days 4.257 days 5.457 days

Calculate Elapsed Time in Calendar Units

Use the between function to find the number of calendar years, months, and days elapsed between
two dates.

t1 = datetime('today')

t1 = datetime
01-Feb-2025

t2 = t1 + calmonths(0:2) + caldays(4)

t2 = 1×3 datetime
05-Feb-2025 05-Mar-2025 05-Apr-2025

dt = between(t1,t2)

dt = 1×3 calendarDuration
4d 1mo 4d 2mo 4d

See Also
between | diff | caldiff

7-36
Compare Dates and Time

Compare Dates and Time

This example shows how to compare dates, times, and durations by using relational operators and
comparison functions. Because the datetime and duration data types represent dates and times
quantitatively, you can use the same relational operators that you use to compare numeric arrays.
However, the comparisons have slightly different meanings, depending on the data type.

• A datetime value can occur before, at the same time as, or after another datetime value.
• A duration value can be shorter than, the same length of time as, or longer than another
duration value.

The calendarDuration data type does not support comparisons using relational operators.
Calendar units do not necessarily represent fixed lengths of time.

You can compare two datetime arrays, and you can compare two duration arrays. The arrays must
have compatible sizes because relational operators perform element-wise comparisons. In the
simplest cases, the two arrays have the same size or one is a scalar. For more information, see
“Compatible Array Sizes for Basic Operations” on page 2-12.

Dates and times can also be represented by text, while durations can also be represented by text and
by numbers. Therefore, you can compare datetime arrays to text and duration arrays to text and
numbers. Relational operators convert text and numbers to the correct data types before performing
operations.

You cannot compare a datetime array and a duration array. However, you can compare
components of datetime arrays to numbers or to duration arrays.

Compare datetime Values

Create a datetime array. To convert text representing a date and time, use the datetime function.
d1 = datetime("2022-06-05 11:37:05")

d1 = datetime
05-Jun-2022 11:37:05

Create another datetime array by converting input numeric arrays that represent datetime
components—years, months, days, hours, minutes, and seconds.
d2 = datetime(2022,2:4:10,15,12,0,0)

d2 = 1×3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00

Compare the two datetime arrays. The result shows which elements of d2 occur after d1.
tf = d2 > d1

tf = 1×3 logical array

0 1 1

To create a datetime array containing only the matching elements, index into d2 using tf.

7-37
7 Dates and Time

afterd1 = d2(tf)

afterd1 = 1×2 datetime


15-Jun-2022 12:00:00 15-Oct-2022 12:00:00

Text and datetime Values

If you have text that represents dates and times in a format that the datetime function recognizes,
then you can compare the text to a datetime array. The comparison implicitly converts the text.

For example, compare d2 to a string that represents June 1, 2022. (If the string only specifies a date,
then the implicit conversion to datetime sets the time to midnight.) The first element of d2 occurs
before June 1.

tf = d2 >= "2022-06-01"

tf = 1×3 logical array

0 1 1

afterJune1 = d2(tf)

afterJune1 = 1×2 datetime


15-Jun-2022 12:00:00 15-Oct-2022 12:00:00

Numbers and Components of datetime Arrays

The datetime data type provides access to the components of datetime values. Access components
by using the year, quarter, month, day, hour, minute, and second functions. You can compare
components to numbers or duration values because these functions return numbers.

For example, display the datetime array d2. Then display its month component.

d2

d2 = 1×3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00

m = month(d2)

m = 1×3

2 6 10

Another way to access the month component is by using the Month property of d2. You can access
datetime components by their Year, Month, Day, Hour, Minute, and Second properties.

m = d2.Month

m = 1×3

2 6 10

7-38
Compare Dates and Time

To find the elements of d2 that occur before the month of June, compare d2 to the numeric value
corresponding to June. Then index into d2.

tf = month(d2) < 6

tf = 1×3 logical array

1 0 0

beforeJune = d2(tf)

beforeJune = datetime
15-Feb-2022 12:00:00

Compare duration Arrays

Create a duration array. To convert text in hh:mm:ss format, use the duration function.

t1 = duration("03:37:12")

t1 = duration
03:37:12

Create another duration array by converting input numeric arrays that represent hours, minutes,
and seconds.

t2 = duration(0:2:6,30,0)

t2 = 1×4 duration
00:30:00 02:30:00 04:30:00 06:30:00

Compare the two duration arrays. The result show which elements of t2 are longer than t1.

tf = t2 > t1

tf = 1×4 logical array

0 0 1 1

To create a new duration array containing only the matching elements, index into t2 using tf.

longerThanT1 = t2(tf)

longerThanT1 = 1×2 duration


04:30:00 06:30:00

Text and duration Values

If you have text that represents a length of time in a format that the duration function recognizes,
then you can compare the text to a duration array. The comparison implicitly converts the text.

For example, compare t2 to a string that represents two hours and five minutes. The first element of
t2 is shorter.

7-39
7 Dates and Time

tf = t2 >= "02:05:00"

tf = 1×4 logical array

0 1 1 1

longerThan205 = t2(tf)

longerThan205 = 1×3 duration


02:30:00 04:30:00 06:30:00

Numbers and duration Arrays

You can compare numeric arrays to duration arrays. The comparison treats a numeric value as a
number of fixed-length (24-hour) days.

Compare the elements of t2 to one day. Every element is shorter.


tf = t2 < 1

tf = 1×4 logical array

1 1 1 1

t2(tf)

ans = 1×4 duration


00:30:00 02:30:00 04:30:00 06:30:00

Compare the elements of t2 to one hour. Only the first element of t2 is shorter.
tf = t2 < 1/24

tf = 1×4 logical array

1 0 0 0

t2(tf)

ans = duration
00:30:00

Compare datetime Arrays in Different Time Zones

Create datetime values for October 1, 2022, at 4:00 p.m. in Los Angeles and October 1, 2022 at
5:00 p.m. in New York. The two cities are in different time zones.

You can create datetime arrays with time zones by specifying the TimeZone name-value argument.
To show the time zone when displaying these values, specify the Format name-value argument. Note
that you can specify a datetime display format that differs from the format of the input text.
LAtime = datetime("2022-10-01 16:00:00", ...
"TimeZone","America/Los_Angeles",...
"Format","dd-MMM-yyyy hh:mm:ss a z")

7-40
Compare Dates and Time

LAtime = datetime
01-Oct-2022 04:00:00 PM PDT

NYtime = datetime("2022-10-01 17:00:00", ...


"TimeZone","America/New_York",...
"Format","dd-MMM-yyyy hh:mm:ss a z")

NYtime = datetime
01-Oct-2022 05:00:00 PM EDT

Compare the times in the two cities. On the same day, 4:00 p.m. in Los Angeles occurs after 5:00 p.m.
in New York. When you specify time zones, comparisons of datetime arrays account for the time
zone information of each array.

tf = NYtime < LAtime

tf = logical
1

Compare two datetime values with the same clock time using the == operator. The two values are
not equal because their time zones are different.

NYtime4 = datetime("2022-10-01 16:00:00", ...


"TimeZone","America/New_York",...
"Format","dd-MMM-yyyy hh:mm:ss a z")

NYtime4 = datetime
01-Oct-2022 04:00:00 PM EDT

tf = NYtime4 == LAtime

tf = logical
0

You cannot compare a datetime array with a time zone to a datetime array without a time zone. If
only one datetime array has a time zone, then there is not enough information for a comparison.

Compare Dates and Times Using Other Functions

MATLAB provides other functions for date and time comparisons.

• isbetween — Determine if elements of a datetime or duration array are within an interval


• isdst — Determine if elements of a datetime array occur during daylight saving time
• isweekend — Determine if elements of a datetime array occur during a weekend (Saturday and
Sunday)
• ismissing — Determine if elements of an array are missing values (NaTs for datetime arrays,
NaNs for duration arrays)

You can also perform set operations on datetime or duration arrays.

• union — Union of two datetime or two duration arrays

7-41
7 Dates and Time

• intersect — Intersection of two datetime or two duration arrays


• ismember — Elements of first datetime or duration array that are elements of second
datetime or duration array
• setdiff — Difference of two datetime or two duration arrays
• setxor — Exclusive OR of two datetime or two duration arrays

For example, determine if any elements of a datetime array occur during the first quarter of 2022.
(The end of the first quarter is the same as the first moment of the second quarter.)

start1Q = datetime("2022-01-01");
end1Q = datetime("2022-04-01");
d = datetime(2022,2:4:10,15,12,0,0)

d = 1×3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00

To determine which elements of d are between the start and the end of the first quarter, use
isbetween. Specify the time interval between start1Q and end1Q as an open-right interval.

tf = isbetween(d,start1Q,end1Q,"openright")

tf = 1×3 logical array

1 0 0

When you use isbetween and specify an open-right interval, it is equivalent to this expression. The
interval includes the moment at the start of January 1, 2022 and every moment up to, but not
including, the start of April 1, 2022. When you specify the end of a time period by using the start of
the next time period, consider that time period to be an open-right time interval.

tf = (start1Q <= d & d < end1Q)

tf = 1×3 logical array

1 0 0

Display the elements of d that occur during the first quarter.

d(tf)

ans = datetime
15-Feb-2022 12:00:00

Specify the time zone of d by setting its TimeZone property. Then determine if any elements occur
during daylight saving time.

d.TimeZone = "America/New_York";
isdst(d)

ans = 1×3 logical array

0 1 1

7-42
Compare Dates and Time

Determine if any elements occur during a weekend.

isweekend(d)

ans = 1×3 logical array

0 0 1

To show the day of the week of the matching elements, use the day function.

weekendDays = d(isweekend(d))

weekendDays = datetime
15-Oct-2022 12:00:00

day(weekendDays,"name")

ans = 1×1 cell array


{'Saturday'}

See Also
datetime | duration | isbetween | isdst | isweekend | ismissing | day | month

More About
• “Set Date and Time Display Format” on page 7-15
• “Specify Time Zones” on page 7-5
• “Generate Sequence of Dates and Time” on page 7-19
• “Extract or Assign Date and Time Components of Datetime Array” on page 7-27
• “Date and Time Arithmetic” on page 7-32
• “Convert Between Text and datetime or duration Values” on page 7-54
• “Array Comparison with Relational Operators” on page 2-16

7-43
7 Dates and Time

Plot Dates and Times

This example shows how to create line plots with dates and times that are stored as datetime and
duration arrays. The datetime data type represents points in time, such as August 24, 2020,
10:50:30 a.m., while the duration data type represents lengths of time, such as 12 hours and 30
minutes. Most plotting functions accept datetime and duration arrays as x-, y-, and z-coordinates
and show tick values with appropriate date and time units. You can specify your own axis limits and
tick values using datetime and duration values. You can also change the format of tick values to
show date and time units of your choice. Data tips show datetime and duration values for cursor
positions on a plot, and you can export those values to workspace variables. When you read data from
a spreadsheet or comma-separated value (CSV) file, you can include the date and time data in your
plots.

Plot Date and Time Data

You can plot datetime and duration arrays without converting them to numeric arrays. Most
plotting functions accept datetime and duration arrays as input arguments.

For example, plot a data set that has datetime values on the x-axis and numeric values on the y-axis.
The x-coordinates are the datetime values for every day in June and July 2021. The plot
automatically displays tick values with an appropriate format on the x-axis. In this case, the
appropriate format shows month names and day numbers with the year.

XDates = [datetime(2021,6,1:30) datetime(2021,7,1:31)];


YNumsForXDates = sin(0:0.1:6);
plot(XDates,YNumsForXDates)

Similarly, plot a data set that has duration values on the x-axis. To create a duration array in units
of seconds, use the seconds function.

7-44
Plot Dates and Times

XTimes = seconds(0:120);
YNumsForXTimes = cos(0:0.05:6);
plot(XTimes,YNumsForXTimes)

Specify Axes Limits

When you change the limits on a plot, the tick values that are shown for datetime and duration
values are updated automatically. You can update limits interactively or by calling the xlim, ylim, or
zlim functions for the corresponding axis. Specify the new limits as a datetime or duration array.
If you change limits to zoom in or zoom out far enough, then the tick values can show other date and
time components, not just new tick values.

For example, plot the XDates and YNumsForXDates arrays. Then change the x-axis limits to June 20
and July 7, 2021, using xlim. The plot displays new tick values.

plot(XDates,YNumsForXDates)
xlim([datetime("2021-06-20") datetime("2021-07-07")])

7-45
7 Dates and Time

Change the x-axis limits to June 20 and June 22, 2021. The tick values show hour and minute
components in hh:mm format because the plot is zoomed in enough to show smaller time units on the
x-axis.

xlim([datetime("2021-06-20") datetime("2021-06-22")])

7-46
Plot Dates and Times

Specify Tick Values

You do not have to change axes limits to change tick values. Instead, you can specify your own tick
values along the x-, y-, or z-axes by using the xticks, yticks, or zticks functions. Specify the tick
values as a datetime or duration array.

For example, plot the XTimes and YNumsForXTimes arrays. Then specify tick values at 0, 60, and
120 seconds by using xticks.

plot(XTimes,YNumsForXTimes)
xticks(seconds([0 60 120]))

Specify Tick Format

Plotting functions use default formats to display datetime and duration values as tick values. To
override the format for the tick values on an axis, use the xtickformat, ytickformat, or
ztickformat functions.

For example, plot XDates and YNumsForXDates. Specify a tick value format showing year, month,
and day numbers by using xtickformat.

plot(XDates,YNumsForXDates)
xtickformat("yyyy-MM-dd")

7-47
7 Dates and Time

As an alternative, you can also call plot with the DatetimeTickFormat or DurationTickFormat
name-value arguments. For example, this call to the plot function creates the same plot.

plot(XDates,YNumsForXDates,"DatetimeTickFormat","yyyy-MM-dd")

However, these name-value arguments can be used with the plot function only. You can use
functions such as xtickformat after calling any plotting function, such as scatter, stem, and
stairs.

Axes Properties That Store Dates and Times

Axis limits, the locations of tick labels, and the x-, y-, and z-values for datetime and duration
arrays in line plots are also stored as properties of an Axes object. These properties represent those
aspects of line plots.

• XLim, YLim, ZLim


• XTick, YTick, ZTick
• XData, YData, ZData

For example, the XLim and XTick properties associated with the plot of XDates and
YNumsForXDates store datetime values. Get the Axes object for the plot and display these
properties.

ax = gca;
ax.XLim

ans = 1×2 datetime


2021-06-01 2021-08-03

ax.XTick

7-48
Plot Dates and Times

ans = 1×5 datetime


2021-06-01 2021-06-15 2021-06-29 2021-07-13 2021-07-27

Export and Convert Data Tip Values

When you click on a plot, you create a data tip at that cursor position that displays its x- and y-
coordinates. Data tips display numeric values as well as datetime and duration values. However,
when you export the cursor data to the workspace, the coordinates are reported as a pair of numeric
values. To convert exported cursor data to the datetime or duration value, use the num2ruler
function.

For example, plot XDates and YNumsForXDates. Then create a data tip by clicking on the plot.

To export the cursor data to the workspace, right-click the data tip and select Export Cursor Data
to Workspace. This action exports the cursor data to a structure in the workspace.

cursor_info =

struct with fields:

Target: [1×1 Line]


Position: [25 0.5985]
DataIndex: 26

The cursor_info.Position field represents the cursor data as a pair of numeric values. The Axes
object associated with the plot has the information needed to convert the numeric value of the x-

7-49
7 Dates and Time

coordinate to a datetime value. Get the Axes object for the plot. Then pass the numeric x-
coordinate and the x-axis from the Axes object to num2ruler.

ax = gca;
datetimePosition = num2ruler(cursor_info.Position(1),ax.XAxis)

datetimePosition =

datetime

26-Jun-2021

You do not need to convert the numeric y-coordinate, cursor_info.Position(2) because the y-
values in this plot are numeric.

Plot Dates and Times from File

Data files such as spreadsheets and CSV files often store dates and times as formatted text. When you
read in data from such files, you can convert text representing dates and times to datetime or
duration arrays. Then you can create plots of that data.

For example, create a plot of data from the example data file outages.csv. This CSV file contains six
columns of data. Two columns contain text that represent dates and times.

Region,OutageTime,Loss,Customers,RestorationTime,Cause
SouthWest,2002-02-01 12:18,458.9772218,1820159.482,2002-02-07 16:50,winter storm
SouthEast,2003-01-23 00:49,530.1399497,212035.3001,,winter storm
SouthEast,2003-02-07 21:15,289.4035493,142938.6282,2003-02-17 08:14,winter storm
...

The recommended way to read data from a CSV file is to use the readtable function. This function
reads data from a file and returns it in a table.

Read in outages.csv. The readtable function automatically converts the text in the OutageTime
and RestorationTime columns to datetime arrays. The columns that represent numbers (Loss
and Customers) are read in as numeric arrays. The remaining columns are read in as strings. The
table stores the columns of data from outages.csv in table variables that have the same names.
Finally, sort the rows of T by the dates and times in OutageTime by using the sortrows function. If
a table is not sorted by time, then it is a best practice to sort the table by time before plotting or
analyzing the data.

T = readtable("outages.csv","TextType","string");
T = sortrows(T,"OutageTime")

T=1468×6 table
Region OutageTime Loss Customers RestorationTime Cause
___________ ________________ ______ __________ ________________ ______________

"SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm"


"MidWest" 2002-03-05 17:53 96.563 2.8666e+05 2002-03-10 14:41 "wind"
"MidWest" 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 "severe storm"
"MidWest" 2002-03-26 01:59 388.04 5.6422e+05 2002-03-28 19:55 "winter storm"
"MidWest" 2002-04-20 16:46 23141 NaN NaT "unknown"
"SouthWest" 2002-05-08 20:34 50.732 34481 2002-05-08 22:21 "thunder storm
"MidWest" 2002-05-18 11:04 1389.1 1.3447e+05 2002-05-21 01:22 "unknown"
"NorthEast" 2002-05-20 10:57 9116.6 2.4983e+06 2002-05-21 15:22 "unknown"
"SouthEast" 2002-05-27 09:44 237.28 1.7101e+05 2002-05-27 16:19 "wind"

7-50
Plot Dates and Times

"SouthEast" 2002-06-02 16:11 0 0 2002-06-05 05:55 "energy emerge


"West" 2002-06-06 19:28 311.86 NaN 2002-06-07 00:51 "equipment fau
"SouthEast" 2002-06-17 23:01 42.542 39877 2002-06-17 23:49 "thunder storm
"MidWest" 2002-07-01 04:33 203.94 60650 2002-07-02 14:54 "severe storm"
"MidWest" 2002-07-01 08:18 100.71 1.8116e+05 2002-07-01 11:33 "severe storm"
"MidWest" 2002-07-10 01:49 168.02 NaN 2002-07-10 17:20 "equipment fau
"SouthEast" 2002-07-14 21:32 90.83 60133 2002-07-14 23:53 "thunder storm

You can access table variables by using dot notation, referring to a table variable by name. With dot
notation, you can treat table variables like arrays.

Plot the power loss against outage time. To access these variables from the table, use dot notation.

plot(T.OutageTime,T.Loss)

Calculate the durations of the power outages and plot them against OutageTime. To calculate the
durations, subtract OutageTime from RestorationTime. The result, OutageDuration, is a
duration array, because arithmetic with datetime values produces lengths of time as output. Some
of these outage durations are long, so change the format of the y-axis tick values from hours to years
by using ytickformat. The fact that some outages apparently last for years indicates there might be
a few questionable data values in the file. Depending on how you plan to analyze the data, you can
either reprocess it in some way or remove the rows containing bad values.

OutageDuration = T.RestorationTime - T.OutageTime;


plot(T.OutageTime,OutageDuration)
ytickformat("y")

7-51
7 Dates and Time

See Also
plot | datetime | duration | seconds | readtable | sortrows | xlim | xtickformat | xticks

Related Examples
• “2-D and 3-D Plots”
• “Types of MATLAB Plots”
• “Generate Sequence of Dates and Time” on page 7-19
• “Compare Dates and Time” on page 7-37
• “Date and Time Arithmetic” on page 7-32
• “Convert Between Text and datetime or duration Values” on page 7-54

7-52
Core Functions Supporting Date and Time Arrays

Core Functions Supporting Date and Time Arrays


Many functions in MATLAB operate on date and time arrays in much the same way that they operate
on other arrays.

This table lists notable MATLAB functions that operate on datetime, duration, and
calendarDuration arrays in addition to other arrays.

size isequal intersect plus plot


length isequaln ismember minus plot3
ndims setdiff uminus scatter
numel eq setxor times scatter3
ne unique rdivide bar
isrow lt union ldivide barh
iscolumn le mtimes histogram
ge abs mrdivide
cat gt floor mldivide stem
horzcat ceil diff stairs
vertcat sort round sum area
sortrows mesh
permute issorted min char surf
reshape max string surface
transpose mean cellstr
ctranspose median semilogx
mode semilogy
linspace fill
fill3
line
text

7-53
7 Dates and Time

Convert Between Text and datetime or duration Values

This example shows how to convert between text and data types that represent dates and times. The
datetime data type represents points in time, such as August 24, 2020, 10:50:30 a.m., and the
duration data type represents lengths of time, such as 3 hours, 47 minutes, and 16 seconds. A
common reason for converting dates and times to text is to append them to strings that are used as
plot labels or file names. Similarly, if a file has columns of data that store dates and times as text, you
can read the data from those columns into datetime or duration arrays, making the data more
useful for analysis.

To convert:

• datetime or duration values to text, use the string function. (You can also use the char
function to convert these values to character vectors.)
• text to datetime values, use the datetime function.
• text to duration values, use the duration function.

Also, some functions, such as the readcell, readvars, and readtable functions, read text from
files and automatically convert text representing dates and times to datetime or duration arrays.

Convert datetime and duration Values to Text

Create a datetime value that represents the current date and time.

d = datetime("now")

d = datetime
02-Feb-2025 03:42:31

To convert d to text, use the string function.

str = string(d)

str =
"02-Feb-2025 03:42:31"

Similarly, you can convert duration values. For example, first create a duration value that
represents 3 hours and 30 minutes. One way to create this value is to use the hours and minutes
functions. These functions create duration values that you can then combine.

d = hours(3) + minutes(30)

d = duration
3.5 hr

Convert d to text.

str = string(d)

str =
"3.5 hr"

7-54
Convert Between Text and datetime or duration Values

One common use of such strings is to add them to plot labels or file names. For example, create a
simple plot with a title that includes today's date. First convert the date and add it to the string
myTitle.

d = datetime("today")

d = datetime
02-Feb-2025

myTitle = "Plot generated on: " + string(d)

myTitle =
"Plot generated on: 02-Feb-2025"

Create the plot with your title.

plot(rand(10,1))
title(myTitle)

Convert Arrays to String Arrays

You can also convert arrays of datetime or duration values. When you convert them by using the
string function, the resulting string array has the same size.

For example, create a datetime array.

D = datetime(2021,1:3,15,12,0,0)'

D = 3×1 datetime
15-Jan-2021 12:00:00
15-Feb-2021 12:00:00
15-Mar-2021 12:00:00

7-55
7 Dates and Time

Convert D to a string array.

str = string(D)

str = 3×1 string


"15-Jan-2021 12:00:00"
"15-Feb-2021 12:00:00"
"15-Mar-2021 12:00:00"

Similarly, you can create a duration array and convert it. One way to create a duration array is to
use the duration function. Call it with numeric inputs that specify hours, minutes, and seconds.

D = duration(1:3,30,0)'

D = 3×1 duration
01:30:00
02:30:00
03:30:00

Convert the duration array.

str = string(D)

str = 3×1 string


"01:30:00"
"02:30:00"
"03:30:00"

Specify Format of Output Text

The datetime and duration data types have properties that specify the format for display. Live
scripts and the Command Window use that format to display values. When you convert datetime or
duration arrays by using the string function, you can specify a different format.

For example, create a datetime value and display it.

d = datetime("now")

d = datetime
02-Feb-2025 03:42:32

Specify a format using letter identifiers for the full name of the month, the day, year, and time.
Convert d to a string that represents the date and time using that format.

fmt = "dd MMMM yyyy, hh:mm:ss a";


str = string(d,fmt)

str =
"02 February 2025, 03:42:32 AM"

Similarly, you can specify a format when you convert a duration array. First create a duration
value.

d = hours(1) + minutes(30) + seconds(45)

7-56
Convert Between Text and datetime or duration Values

d = duration
1.5125 hr

Convert d to a string using the identifiers hh:mm:ss for the hour, minute, and second.

fmt = "hh:mm:ss";
string(d,fmt)

ans =
"01:30:45"

Note: The string function does not provide a second input argument for a format when converting
other data types.

Specify Locale of Output Text

You can also convert datetime and duration arrays using different locales. The locale provides
appropriate names for the day and month. To use a locale that is not the default locale, provide it as
another input argument.

For example, specify fr_FR as the locale to represent the current date and time using the French
name for the month.

d = datetime("now")

d = datetime
02-Feb-2025 03:42:32

fmt = "dd MMMM yyyy, hh:mm:ss a";


locale = "fr_FR";
str = string(d,fmt,locale)

str =
"02 février 2025, 03:42:32 AM"

Similarly, you can specify a locale when you convert duration arrays. The locale for France uses a
different abbreviation for hours.

d = hours(5)

d = duration
5 hr

fmt = "h";
locale = "fr_FR";
str = string(d,fmt,locale)

str =
"5 h"

Note: The string function does not provide a third input argument for a locale when converting
other data types.

7-57
7 Dates and Time

Convert Text to datetime Values

You can convert text to datetime values if the text specifies dates and times in a format that the
datetime function recognizes.

Create a string that represents a date and a time.

str = "2021-09-15 09:12:34"

str =
"2021-09-15 09:12:34"

Convert str to a datetime value.

d = datetime(str)

d = datetime
15-Sep-2021 09:12:34

Interpret Format of Input Text

The datetime function recognizes many commonly used text formats. However, if your text is in a
format that datetime does not recognize, you can specify the format as an input argument.

For example, create a string that specifies a date and time using the ISO 8601 standard.

str = "2021-09-15T091234"

str =
"2021-09-15T091234"

The datetime function does not recognize this format. To convert this string to a datetime value,
specify the format of the input text. Then call the datetime function. (When the format includes
literal text, enclose it in quotation marks. In this example specify the literal text T as 'T'.)

infmt = "yyyy-MM-dd'T'HHmmss";
d = datetime(str,"InputFormat",infmt)

d = datetime
15-Sep-2021 09:12:34

Convert Text to duration Values

You can convert text to duration values if the text specifies times in a format that the duration
function recognizes.

Create a string that represents a length of time.

str = "00:34:01"

str =
"00:34:01"

Convert str to a duration value.

d = duration(str)

7-58
Convert Between Text and datetime or duration Values

d = duration
00:34:01

Interpret Format of Input Text

The duration function recognizes formats that specify days, hours, minutes, and seconds separated
by colons. These formats are:

• "dd:hh:mm:ss"
• "hh:mm:ss"
• "mm:ss"
• "hh:mm"
• Any of the first three formats, with up to nine S characters to indicate fractional second digits,
such as "hh:mm:ss.SSSS"

If the input text is ambiguous, which means that it could be interpreted as matching the "mm:ss" or
"hh:mm" formats, specify the format as an input argument.

For example, create a string that represents a length of time.


str = "34:01"

str =
"34:01"

To convert this string to a duration of 34 minutes and 1 second, specify the format. Then call the
duration function.
infmt = "mm:ss";
d = duration(str,"InputFormat",infmt)

d = duration
00:34:01

Read Dates and Times from Files

Many files, such as spreadsheets and text files, store dates and times as text. If the dates and times
are in recognized formats, then functions such as readcell, readvars, and readtable can read
them and automatically convert them to datetime or duration arrays.

For example, the CSV file outages.csv is a sample file that is distributed with MATLAB®. The file
contains data for a set of electrical power outages. The first line of outages.csv has column names.
The rest of the file has comma-separated data values for each outage. The file has 1468 lines of data.
The first few lines are shown here.
Region,OutageTime,Loss,Customers,RestorationTime,Cause
SouthWest,2002-02-01 12:18,458.9772218,1820159.482,2002-02-07 16:50,winter storm
SouthEast,2003-01-23 00:49,530.1399497,212035.3001,,winter storm
SouthEast,2003-02-07 21:15,289.4035493,142938.6282,2003-02-17 08:14,winter storm
West,2004-04-06 05:44,434.8053524,340371.0338,2004-04-06 06:10,equipment fault
MidWest,2002-03-16 06:18,186.4367788,212754.055,2002-03-18 23:23,severe storm
...

To read the first three columns from outages.csv and store them directly in arrays, use the
readvars function. To read text into variables that store string arrays, specify the TextType name-

7-59
7 Dates and Time

value argument. However, the function recognizes the values in the second column of the CSV file as
dates and times and creates the OutageTime variable as a datetime array. Display the first five
rows of each output array.

[Region,OutageTime,Loss] = readvars("outages.csv","TextType","string");
whos Region OutageTime Loss

Name Size Bytes Class Attributes

Loss 1468x1 11744 double


OutageTime 1468x1 23520 datetime
Region 1468x1 94936 string

Loss(1:5)

ans = 5×1

458.9772
530.1399
289.4035
434.8054
186.4368

OutageTime(1:5)

ans = 5×1 datetime


2002-02-01 12:18
2003-01-23 00:49
2003-02-07 21:15
2004-04-06 05:44
2002-03-16 06:18

Region(1:5)

ans = 5×1 string


"SouthWest"
"SouthEast"
"SouthEast"
"West"
"MidWest"

To read th