0% found this document useful (0 votes)
136 views

Matlab Programming Fundamentals Unknown download

The document provides links to various MATLAB programming resources and ebooks, including titles by Mathworks and Michael C Weeks. It also includes information about MATLAB programming fundamentals, syntax basics, and program components. Additionally, it outlines contact information for MathWorks and includes a revision history of the document.

Uploaded by

ziminmauti2u
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)
136 views

Matlab Programming Fundamentals Unknown download

The document provides links to various MATLAB programming resources and ebooks, including titles by Mathworks and Michael C Weeks. It also includes information about MATLAB programming fundamentals, syntax basics, and program components. Additionally, it outlines contact information for MathWorks and includes a revision history of the document.

Uploaded by

ziminmauti2u
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/ 88

Matlab Programming Fundamentals Unknown download

https://ebookbell.com/product/matlab-programming-fundamentals-
unknown-49157074

Explore and download more ebooks at ebookbell.com


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

Matlab R2023a Programming Fundamentals Mathworks

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

Programming Fundamentals Using Matlab Michael C Weeks

https://ebookbell.com/product/programming-fundamentals-using-matlab-
michael-c-weeks-35997544

Programming Fundamentals Using Matlab Phd Michael C Weeks

https://ebookbell.com/product/programming-fundamentals-using-matlab-
phd-michael-c-weeks-11483636

Programming Fundamentals Using Matlab Phd Michael C Weeks Michael C


Weeks

https://ebookbell.com/product/programming-fundamentals-using-matlab-
phd-michael-c-weeks-michael-c-weeks-11483638
Fundamental Concepts Of Matlab Programming From Learning The Basics To
Solving A Problem With Matlab Dr Brijesh Bakariya

https://ebookbell.com/product/fundamental-concepts-of-matlab-
programming-from-learning-the-basics-to-solving-a-problem-with-matlab-
dr-brijesh-bakariya-44583186

Matlab Programming Mathematical Problem Solutions Dingy Xue Tsinghua


University Press

https://ebookbell.com/product/matlab-programming-mathematical-problem-
solutions-dingy-xue-tsinghua-university-press-50340062

Matlab Programming For Engineers 6th Edition Stephen J Chapman

https://ebookbell.com/product/matlab-programming-for-engineers-6th-
edition-stephen-j-chapman-34563734

Matlab Programming With Applications For Engineers 1st Edition Stephen


J Chapman

https://ebookbell.com/product/matlab-programming-with-applications-
for-engineers-1st-edition-stephen-j-chapman-4705226

Matlab Programming For Numerical Analysis 1st Edition Cesar Perez


Lopez

https://ebookbell.com/product/matlab-programming-for-numerical-
analysis-1st-edition-cesar-perez-lopez-4916454
MATLAB®
Programming Fundamentals

R2022b
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–2022 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)
Contents

Language

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

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

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

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


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

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

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


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

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


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

Program Components
2
MATLAB Operators and Special Characters . . . . . . . . . . . . . . . . . . . . 2-2
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
String and Character Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16

Array vs. Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22

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

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


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

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


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

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

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

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

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

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

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

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


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

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


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

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


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

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


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

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


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

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

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


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

Classes (Data Types)

Overview of MATLAB Classes


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

Detect States of Variables Using is* Functions . . . . . . . . . . . . . . . . . . 3-5

Numeric Classes
4
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Creating Integer Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Arithmetic Operations on Integer Classes . . . . . . . . . . . . . . . . . . . . . 4-4
Largest and Smallest Values for Integer Classes . . . . . . . . . . . . . . . . 4-4

Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6


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

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

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


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

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

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

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

Single Precision Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-27

The Logical Class


5
Find Array Elements That Meet a Condition . . . . . . . . . . . . . . . . . . . . 5-2

Reduce Logical Arrays to Single Value . . . . . . . . . . . . . . . . . . . . . . . . 5-6

Characters and Strings


6
Text in String and Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2

Create String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5

Cell Arrays of Character Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12


Create Cell Array of Character Vectors . . . . . . . . . . . . . . . . . . . . . . 6-12
Access Character Vectors in Cell Array . . . . . . . . . . . . . . . . . . . . . . 6-12
Convert Cell Arrays to String Arrays . . . . . . . . . . . . . . . . . . . . . . . . 6-13

Analyze Text Data with String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 6-15

Test for Empty Strings and Missing Values . . . . . . . . . . . . . . . . . . . . 6-20

Formatting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24


Fields of the Formatting Operator . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Setting Field Width and Precision . . . . . . . . . . . . . . . . . . . . . . . . . . 6-28
Restrictions on Using Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30

Compare Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-32

Search and Replace Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-37

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

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

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

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

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

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

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


What Are String Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-63
Recommended Approaches for String Adoption in Old APIs . . . . . . 6-63
How to Adopt String Arrays in Old APIs . . . . . . . . . . . . . . . . . . . . . 6-65
Recommended Approaches for String Adoption in New Code . . . . . 6-65
How to Maintain Compatibility in New Code . . . . . . . . . . . . . . . . . . 6-66
How to Manually Convert Input Arguments . . . . . . . . . . . . . . . . . . 6-67
How to Check Argument Data Types . . . . . . . . . . . . . . . . . . . . . . . . 6-67
Terminology for Character and String Arrays . . . . . . . . . . . . . . . . . 6-69

Dates and Time


7
Represent Dates and Times in MATLAB . . . . . . . . . . . . . . . . . . . . . . . 7-2

Specify Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5

Convert Date and Time to Julian Date or POSIX Time . . . . . . . . . . . . 7-7

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


Formats for Individual Date and Duration Arrays . . . . . . . . . . . . . . 7-10
datetime Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10
duration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
calendarDuration Display Format . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12
Default datetime Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12

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


Sequence of Datetime or Duration Values Between Endpoints with
Step Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14
Add Duration or Calendar Duration to Create Sequence of Dates . . 7-16
Specify Length and Endpoints of Date or Duration Sequence . . . . . 7-17
Sequence of Datetime Values Using Calendar Rules . . . . . . . . . . . . 7-17

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


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

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

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

Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-28

ix
Compare Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33

Plot Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-36

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

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

Replace Discouraged Instances of Serial Date Numbers and Date


Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-57
Convert Serial Date Numbers and Date Strings . . . . . . . . . . . . . . . 7-57
Replace Functions That Use Date Numbers . . . . . . . . . . . . . . . . . . 7-57
Discouraged Syntaxes for Date and Time Components . . . . . . . . . . 7-61
Guidelines for Updating Your Own Functions . . . . . . . . . . . . . . . . . 7-62

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

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

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

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

Plot Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10

Compare Categorical Array Elements . . . . . . . . . . . . . . . . . . . . . . . . 8-16

Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-19

Combine Categorical Arrays Using Multiplication . . . . . . . . . . . . . . 8-22

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


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

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

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


Natural Representation of Categorical Data . . . . . . . . . . . . . . . . . . 8-34
Mathematical Ordering for Character Vectors . . . . . . . . . . . . . . . . . 8-34
Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-34

Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-36


Order of Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-36
How to Create Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . 8-36
Working with Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . 8-38

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

x Contents
Tables
9
Create 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

Modify Units, Descriptions, and Table Variable Names . . . . . . . . . . 9-24

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

Access Data in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-32


Summary of Table Indexing Syntaxes . . . . . . . . . . . . . . . . . . . . . . . 9-32
Table Indexing with Specified Rows and Variables . . . . . . . . . . . . . 9-37
Find Rows Where Values Meet Logical Conditions . . . . . . . . . . . . . 9-41
Dot Notation with Any Variable Name or Expression . . . . . . . . . . . . 9-44
Extract Data from Specified Rows and Variables . . . . . . . . . . . . . . . 9-46

Calculations on Data in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-48

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

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

Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-71

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


Grouping Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-76
Group Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-76
The Split-Apply-Combine Workflow . . . . . . . . . . . . . . . . . . . . . . . . . 9-77
Missing Group Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-77

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

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

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

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

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

Combine Timetables and Synchronize Their Data . . . . . . . . . . . . . . 10-8

xi
Retime and Synchronize Timetable Variables Using Different
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14

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

Clean Timetable with Missing, Duplicate, or Nonuniform Times


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

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

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

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

Add Events from External Data to Timetable . . . . . . . . . . . . . . . . . 10-71

Find Events in Timestamped Data . . . . . . . . . . . . . . . . . . . . . . . . . . 10-83

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

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

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

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

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

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


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

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

Cell Arrays
12
What Is a Cell Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2

Create Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3

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

xii Contents
Add Cells to Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8

Delete Data from Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-10

Combine Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11

Pass Contents of Cell Arrays to Functions . . . . . . . . . . . . . . . . . . . . 12-12

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

Cell vs. Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17

Multilevel Indexing to Access Parts of Cells . . . . . . . . . . . . . . . . . . 12-21

Function Handles
13
Create Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Is a Function Handle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Creating Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-3
Arrays of Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Saving and Loading Function Handles . . . . . . . . . . . . . . . . . . . . . . 13-4

Pass Function to Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5

Call Local Functions Using Function Handles . . . . . . . . . . . . . . . . . 13-6

Compare Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8

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

Dictionaries and Custom Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-6


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

Map Containers
15
Overview of Map Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-2

xiii
Description of Map Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-4
Properties of Map Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-4
Methods of Map Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-4

Create Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6


Construct Empty Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6
Construct Initialized Map Object . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6
Combine Map Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-7

Examine Contents of Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-8

Read and Write Using Key Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-9


Read From Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-9
Add Key/Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-10
Build Map with Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-10

Modify Keys and Values in Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-13


Remove Keys and Values from Map . . . . . . . . . . . . . . . . . . . . . . . . 15-13
Modify Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-13
Modify Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-14
Modify Copy of Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-14

Map to Different Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-15


Map to Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-15
Map to Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-16

Combining Unlike Classes


16
Valid Combinations of Unlike Classes . . . . . . . . . . . . . . . . . . . . . . . . 16-2

Combining Unlike Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-3


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

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

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

Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-7

Concatenation Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-8


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

xiv Contents
Using Objects
17
Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Handle Object Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Handle Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-3
Testing for Handle or Value Class . . . . . . . . . . . . . . . . . . . . . . . . . . 17-5

Defining Your Own Classes


18

Scripts and Functions

Scripts
19
Create Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2

Add Comments to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-3

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


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

Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-10

Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12


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

Live Scripts and Functions


20
What Is a Live Script or Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Differences with Plain Code Scripts and Functions . . . . . . . . . . . . . 20-3

xv
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4
Unsupported Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-5

Create Live Scripts in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . 20-6


Create Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-6
Add Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-6
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-7
Display Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-7
Change View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-8
Format Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10
Save Live Scripts as Plain Code . . . . . . . . . . . . . . . . . . . . . . . . . . 20-11

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


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

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


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

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


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

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


Insert Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-34
Modify Control Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-35
Link Variables to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-36
Specify Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-37
Modify Control Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-37
Create Live Script with Multiple Interactive Controls . . . . . . . . . . 20-38
Share Live Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-41

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


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

Create Live Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-49


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

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

Share Live Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-56


Hide Code Before Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-57

xvi Contents
Live Code File Format (.mlx) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-58
Benefits of Live Code File Format . . . . . . . . . . . . . . . . . . . . . . . . . 20-58
Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-58

Introduction to the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-59

Accelerate Exploratory Programming Using the Live Editor . . . . . 20-64

Create an Interactive Narrative with the Live Editor . . . . . . . . . . . 20-69

Create Interactive Course Materials Using the Live Editor . . . . . . 20-77

Create Examples Using the Live Editor . . . . . . . . . . . . . . . . . . . . . . 20-83

Create an Interactive Form Using the Live Editor . . . . . . . . . . . . . 20-84

Create a Real-Time Dashboard Using the Live Editor . . . . . . . . . . 20-87

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-89

Function Basics
21
Create Functions in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-2
Syntax for Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-2
Contents of Functions and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-3
End Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-4

Add Help for Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-5

Configure the Run Button for Functions . . . . . . . . . . . . . . . . . . . . . . 21-7

Base and Function Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-9

Share Data Between Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-10


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-10
Best Practice: Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 21-10
Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-10
Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-11
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-12
Evaluating in Another Workspace . . . . . . . . . . . . . . . . . . . . . . . . . 21-12

Check Variable Scope in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-14


Use Automatic Function and Variable Highlighting . . . . . . . . . . . . 21-14
Example of Using Automatic Function and Variable Highlighting . 21-14

Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-17


Local and Nested Functions in a File . . . . . . . . . . . . . . . . . . . . . . 21-17
Private Functions in a Subfolder . . . . . . . . . . . . . . . . . . . . . . . . . . 21-18
Anonymous Functions Without a File . . . . . . . . . . . . . . . . . . . . . . 21-18

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

Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-25

Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-27


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

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


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-33
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-33

Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-36

Function Precedence Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-37


Change in Rules For Function Precedence Order . . . . . . . . . . . . . 21-38

Update Code for R2019b Changes to Function Precedence Order


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

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


Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-46
Supported Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-46

xviii Contents
Function Arguments
22
Find Number of Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . 22-2

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

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

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

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

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

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

Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13

Input Parser Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 22-17

Debugging MATLAB Code


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

Set Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23-9


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

Examine Values While Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 23-14


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

xix
Presenting MATLAB Code
24
Publish and Share MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Create and Share Live Scripts in the Live Editor . . . . . . . . . . . . . . . 24-2
Publish MATLAB Code Files (.m) . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-2
Add Help and Create Documentation . . . . . . . . . . . . . . . . . . . . . . . 24-4

Publishing Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-6


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

Output Preferences for Publishing . . . . . . . . . . . . . . . . . . . . . . . . . 24-21


How to Edit Publishing Options . . . . . . . . . . . . . . . . . . . . . . . . . . 24-21
Specify Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-22
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-23
Manipulate Graphics in Publishing Output . . . . . . . . . . . . . . . . . . 24-24
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-28
Manage a Publish Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 24-29

Coding and Productivity Tips


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

Check Code for Errors and Warnings Using the Code Analyzer . . . 25-5
Enable Continuous Code Checking . . . . . . . . . . . . . . . . . . . . . . . . . 25-5
View Code Analyzer Status for File . . . . . . . . . . . . . . . . . . . . . . . . . 25-5
View Code Analyzer Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-6
Fix Problems in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-7
Create a Code Analyzer Message Report . . . . . . . . . . . . . . . . . . . . . 25-8
Adjust Code Analyzer Message Indicators and Messages . . . . . . . . 25-9
Understand Code Containing Suppressed Messages . . . . . . . . . . . 25-11
Understand the Limitations of Code Analysis . . . . . . . . . . . . . . . . 25-12
Enable MATLAB Compiler Deployment Messages . . . . . . . . . . . . . 25-14

xx Contents
Edit and Format Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-16
Column Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-16
Change Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-16
Automatically Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-16
Refactor Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-17
Indent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-17
Fold Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-18
Change the Right-Side Text Limit Indicator . . . . . . . . . . . . . . . . . . 25-19

Find and Replace Text in Files and Go to Location . . . . . . . . . . . . . 25-21


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

Add Reminders to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-27


Working with TODO/FIXME Reports . . . . . . . . . . . . . . . . . . . . . . . 25-27

MATLAB Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-29


Run the Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-29
Change Code Based on Code Analyzer Messages . . . . . . . . . . . . . 25-30
Other Ways to Access Code Analyzer Messages . . . . . . . . . . . . . . 25-30

MATLAB Code Compatibility Analyzer . . . . . . . . . . . . . . . . . . . . . . . 25-32


Open the Code Compatibility Analyzer . . . . . . . . . . . . . . . . . . . . . 25-32
Programmatic Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-34
Unsupported Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-34

Code Generation Readiness Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-35


Issues Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-35
Files Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-36

Programming Utilities
26
Identify Program Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Simple Display of Program File Dependencies . . . . . . . . . . . . . . . . 26-2
Detailed Display of Program File Dependencies . . . . . . . . . . . . . . . 26-2
Dependencies Within a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2

Security Considerations to Protect Your Source Code . . . . . . . . . . . 26-6


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

Create a Content-Obscured File with P-Code . . . . . . . . . . . . . . . . . . 26-9


Create P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-9
Invoke P-Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-9

xxi
Run Older P-Code Files on Later Versions of MATLAB . . . . . . . . . . . 26-9

Create Hyperlinks that Run Functions . . . . . . . . . . . . . . . . . . . . . . 26-11


Run a Single Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-11
Run Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-12
Provide Command Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-12
Include Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-12

Create and Share Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-14


Create Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-14
Share Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-18

Run Parallel Language in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 26-20


Run Parallel Language in Serial . . . . . . . . . . . . . . . . . . . . . . . . . . 26-20
Use Parallel Language Without a Pool . . . . . . . . . . . . . . . . . . . . . . 26-21

Function Argument Validation


27
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-2
Introduction to Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . 27-2
Where to Use Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . 27-2
arguments Block Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-2
Examples of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 27-5
Kinds of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-7
Required and Optional Positional Arguments . . . . . . . . . . . . . . . . . 27-7
Repeating Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-9
Name-Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-11
Robust Handling of Name-Value Arguments . . . . . . . . . . . . . . . . . 27-14
Name-Value Arguments from Class Properties . . . . . . . . . . . . . . . 27-15
Order of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-17
Avoiding Class and Size Conversions . . . . . . . . . . . . . . . . . . . . . . 27-17
nargin in Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-19
Restrictions on Variable and Function Access . . . . . . . . . . . . . . . . 27-20
Debugging Arguments Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-21

Argument Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-23


Numeric Value Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-23
Comparison with Other Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-24
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-24
Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-24
Membership and Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-25
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-25
Define Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-25

Transparency in MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-27


Writing Transparent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-27

xxii Contents
Software Development

Error Handling
28
Exception Handling in a MATLAB Application . . . . . . . . . . . . . . . . . 28-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-2
Getting an Exception at the Command Line . . . . . . . . . . . . . . . . . . 28-2
Getting an Exception in Your Program Code . . . . . . . . . . . . . . . . . . 28-3
Generating a New Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-3

Throw an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-4


Suggestions on How to Throw an Exception . . . . . . . . . . . . . . . . . . 28-4

Respond to an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-6


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-6
The try/catch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-6
Suggestions on How to Handle an Exception . . . . . . . . . . . . . . . . . 28-7

Clean Up When Functions Complete . . . . . . . . . . . . . . . . . . . . . . . . . 28-9


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-9
Examples of Cleaning Up a Program Upon Exit . . . . . . . . . . . . . . . 28-10
Retrieving Information About the Cleanup Routine . . . . . . . . . . . . 28-11
Using onCleanup Versus try/catch . . . . . . . . . . . . . . . . . . . . . . . . 28-12
onCleanup in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-12

Issue Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-14


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

Suppress Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-17


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

Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-20


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

Change How Warnings Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-22


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

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

xxiii
Program Scheduling
29
Schedule Command Execution Using Timer . . . . . . . . . . . . . . . . . . . 29-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2
Example: Displaying a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2

Timer Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-4


Associating Commands with Timer Object Events . . . . . . . . . . . . . . 29-4
Creating Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-5
Specifying the Value of Callback Function Properties . . . . . . . . . . . 29-6

Handling Timer Queuing Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . 29-8


Drop Mode (Default) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-8
Error Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-9
Queue Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-10

Performance
30
Measure the Performance of Your Code . . . . . . . . . . . . . . . . . . . . . . 30-2
Overview of Performance Timing Functions . . . . . . . . . . . . . . . . . . 30-2
Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-2
Time Portions of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-2
The cputime Function vs. tic/toc and timeit . . . . . . . . . . . . . . . . . . . 30-2
Tips for Measuring Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-3

Profile Your Code to Improve Performance . . . . . . . . . . . . . . . . . . . . 30-4


What Is Profiling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-4
Profile Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-4
Profile Multiple Statements in Command Window . . . . . . . . . . . . . 30-10
Profile an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-11

Techniques to Improve Performance . . . . . . . . . . . . . . . . . . . . . . . . 30-12


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

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

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

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

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


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

Create Responsive Apps by Running Calculations in the Background


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

Run Functions in Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-13

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

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

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


Issue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-6
Possible Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-6

How MATLAB Allocates Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12

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


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

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

Check Which Programs Have Help . . . . . . . . . . . . . . . . . . . . . . . . . . 33-8

Create Help Summary Files — Contents.m . . . . . . . . . . . . . . . . . . . 33-10


What Is a Contents.m File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10
Create a Contents.m File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10
Check an Existing Contents.m File . . . . . . . . . . . . . . . . . . . . . . . . 33-11

Customize Code Suggestions and Completions . . . . . . . . . . . . . . . 33-12


Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Signature Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13
Argument Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-14
Create Function Signature File . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-17
How Function Signature Information is Used . . . . . . . . . . . . . . . . 33-18
Multiple Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-19

Display Custom Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Create HTML Help Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-22
Create info.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23
Create helptoc.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-24
Build a Search Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-26
Address Validation Errors for info.xml Files . . . . . . . . . . . . . . . . . 33-27

Display Custom Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29


How to Display Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29
Elements of the demos.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30

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

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


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

Set MATLAB Projects Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 34-10

xxvi Contents
Determine Order for Resolving Conflicts Using Dependency Analyzer
.................................................... 34-13

Manage Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-15


Automatic Updates When Renaming, Deleting, or Removing Files
................................................ 34-16

Find Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-17


Group and Sort Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-17
Search for and Filter Project Files . . . . . . . . . . . . . . . . . . . . . . . . 34-17
Search the Content in Project Files . . . . . . . . . . . . . . . . . . . . . . . . 34-17

Create Shortcuts to Frequent Tasks . . . . . . . . . . . . . . . . . . . . . . . . 34-19


Run Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-19
Create Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-19
Organize Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-19

Add Labels to Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-21


Add Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-21
View and Edit Label Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-21
Create Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-22

Create Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-23


Create a Custom Task Function . . . . . . . . . . . . . . . . . . . . . . . . . . 34-23
Run a Custom Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-23
Save Custom Task Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-24

Componentize Large Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-25


Add or Remove Reference to a Project . . . . . . . . . . . . . . . . . . . . . 34-25
View, Edit, or Run Referenced Project Files . . . . . . . . . . . . . . . . . 34-25
Extract Folder to Create a Referenced Project . . . . . . . . . . . . . . . 34-26
Manage Changes in Referenced Project Using Checkpoints . . . . . 34-26

Share Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-28


Create an Export Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-31

Upgrade Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-32


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

Analyze Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-35


Run a Dependency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-35
Explore the Dependency Graph, Views, and Filters . . . . . . . . . . . . 34-37
Investigate and Resolve Problems . . . . . . . . . . . . . . . . . . . . . . . . . 34-43
Find Required Products and Add-Ons . . . . . . . . . . . . . . . . . . . . . . 34-46
Find File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-47
Export Dependency Analysis Results . . . . . . . . . . . . . . . . . . . . . . 34-49

Clone Remote Git Repository into New Project . . . . . . . . . . . . . . . 34-51

Use Source Control with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . 34-52


Setup Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-52
Perform Source Control Operations . . . . . . . . . . . . . . . . . . . . . . . 34-54
Work with Derived Files in Projects . . . . . . . . . . . . . . . . . . . . . . . 34-60
Find Project Files With Unsaved Changes . . . . . . . . . . . . . . . . . . . 34-61

xxvii
Manage Open Files When Closing a Project . . . . . . . . . . . . . . . . . 34-61

Create and Edit Projects Programmatically . . . . . . . . . . . . . . . . . . 34-62

Explore an Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-69

Source Control Interface


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

Set Source Control Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-4


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

Use Git in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-5


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

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


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

Branch and Merge with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-19


Create Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-19
Switch Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-20
Compare Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-21
Merge Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-21
Revert to Head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-21
Delete Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-22

Add Git Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-23


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

Review Changes in SVN Source Control . . . . . . . . . . . . . . . . . . . . . 35-25

Mark Files for Addition to SVN Source Control . . . . . . . . . . . . . . . 35-26

xxviii Contents
Move, Rename, or Delete Files Under SVN Source Control . . . . . . 35-27

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


Examining and Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Merge Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Extract Conflict Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-29

Commit Modified Files to SVN Source Control . . . . . . . . . . . . . . . . 35-31

Revert Changes in SVN Source Control . . . . . . . . . . . . . . . . . . . . . . 35-32


Revert Local Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-32
Revert a File to a Specified Revision . . . . . . . . . . . . . . . . . . . . . . . 35-32

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


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

Check Out from SVN Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-38


Retrieve Tagged Version of Repository . . . . . . . . . . . . . . . . . . . . . 35-38

Update SVN File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . 35-40


Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-40
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-40

Get SVN File Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41


Manage SVN Repository Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41

Continuous Integration Using MATLAB Projects and Jenkins . . . 35-42


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

Customize External Source Control to Use MATLAB for Diff and


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

MSSCCI Source Control Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 35-51

Set Up MSSCCI Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-52


Create Projects in Source Control System . . . . . . . . . . . . . . . . . . . 35-52
Specify Source Control System with MATLAB Software . . . . . . . . 35-53
Register Source Control Project with MATLAB Software . . . . . . . . 35-54
Add Files to Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-56

xxix
Check Files In and Out from MSSCCI Source Control . . . . . . . . . . 35-57
Check Files Into Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 35-57
Check Files Out of Source Control . . . . . . . . . . . . . . . . . . . . . . . . 35-57
Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-58

Additional MSSCCI Source Control Actions . . . . . . . . . . . . . . . . . . 35-59


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

Access MSSCCI Source Control from Editors . . . . . . . . . . . . . . . . . 35-65

Troubleshoot MSSCCI Source Control Problems . . . . . . . . . . . . . . 35-66


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

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

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

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

Extend Script-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-14


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

Run Tests in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-17

Write Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-20


Create Test Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-20
Run the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-22
Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-23

Write Simple Test Case Using Functions . . . . . . . . . . . . . . . . . . . . . 36-24

Write Test Using Setup and Teardown Functions . . . . . . . . . . . . . . 36-27

Extend Function-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-32


Fixtures for Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . 36-32
Test Logging and Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-33

xxx Contents
Test Suite Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-33
Test Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-33
Test Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-34
Programmatic Access of Test Diagnostics . . . . . . . . . . . . . . . . . . . 36-34
Test Runner Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-35

Author Class-Based Unit Tests in MATLAB . . . . . . . . . . . . . . . . . . . 36-36


The Test Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-36
The Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-36
Additional Features for Advanced Test Classes . . . . . . . . . . . . . . . 36-37

Write Simple Test Case Using Classes . . . . . . . . . . . . . . . . . . . . . . . 36-39

Write Setup and Teardown Code Using Classes . . . . . . . . . . . . . . . 36-42


Test Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-42
Test Case with Method-Level Setup Code . . . . . . . . . . . . . . . . . . . 36-42
Test Case with Class-Level Setup Code . . . . . . . . . . . . . . . . . . . . . 36-43

Table of Verifications, Assertions, and Other Qualifications . . . . . 36-45

Tag Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-48


Tag Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-48
Select and Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-49

Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . 36-52

Create Basic Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-55

Create Advanced Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-57

Use Parameters in Class-Based Tests . . . . . . . . . . . . . . . . . . . . . . . 36-62


How to Write Parameterized Tests . . . . . . . . . . . . . . . . . . . . . . . . 36-62
How to Initialize Parameterization Properties . . . . . . . . . . . . . . . . 36-63
Specify Parameterization Level . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-64
Specify How Parameters Are Combined . . . . . . . . . . . . . . . . . . . . 36-65
Use External Parameters in Tests . . . . . . . . . . . . . . . . . . . . . . . . . 36-67

Create Basic Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-68

Create Advanced Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . 36-73

Use External Parameters in Parameterized Test . . . . . . . . . . . . . . . 36-80

Define Parameters at Suite Creation Time . . . . . . . . . . . . . . . . . . . 36-84

Create Simple Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-91

Run Tests for Various Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-93


Set Up Example Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-93
Run All Tests in Class or Function . . . . . . . . . . . . . . . . . . . . . . . . . 36-93
Run Single Test in Class or Function . . . . . . . . . . . . . . . . . . . . . . . 36-93
Run Test Suites by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-94
Run Test Suites from Test Array . . . . . . . . . . . . . . . . . . . . . . . . . . 36-94
Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . . 36-95

xxxi
Programmatically Access Test Diagnostics . . . . . . . . . . . . . . . . . . . 36-96

Add Plugin to Test Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-97

Write Plugins to Extend TestRunner . . . . . . . . . . . . . . . . . . . . . . . 36-100


Custom Plugins Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-100
Extending Test Session Level Plugin Methods . . . . . . . . . . . . . . . 36-100
Extending Test Suite Level Plugin Methods . . . . . . . . . . . . . . . . 36-101
Extending Test Class Level Plugin Methods . . . . . . . . . . . . . . . . 36-101
Extending Test Level Plugin Methods . . . . . . . . . . . . . . . . . . . . . 36-102

Create Custom Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-103

Run Tests in Parallel with Custom Plugin . . . . . . . . . . . . . . . . . . . 36-108

Write Plugin to Add Data to Test Results . . . . . . . . . . . . . . . . . . . 36-116

Write Plugin to Save Diagnostic Details . . . . . . . . . . . . . . . . . . . . 36-121

Plugin to Generate Custom Test Output Format . . . . . . . . . . . . . . 36-125

Analyze Test Case Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-128

Analyze Failed Test Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-131

Rerun Failed Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-133

Dynamically Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-136


Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-136
Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . 36-138
Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . 36-139

Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-142

Create Custom Boolean Constraint . . . . . . . . . . . . . . . . . . . . . . . . 36-145

Overview of App Testing Framework . . . . . . . . . . . . . . . . . . . . . . . 36-149


App Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-149
Gesture Support of UI Components . . . . . . . . . . . . . . . . . . . . . . 36-149
Write a Test for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-151

Write Tests for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-154

Write Tests That Use App Testing and Mocking Frameworks . . . 36-158
Create App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-158
Test App With Manual Intervention . . . . . . . . . . . . . . . . . . . . . . . 36-159
Create Fully Automated Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-160

Overview of Performance Testing Framework . . . . . . . . . . . . . . . . 36-163


Determine Bounds of Measured Code . . . . . . . . . . . . . . . . . . . . . 36-163
Types of Time Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-164
Write Performance Tests with Measurement Boundaries . . . . . . . 36-164
Run Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-165
Understand Invalid Test Results . . . . . . . . . . . . . . . . . . . . . . . . . 36-165

xxxii Contents
Test Performance Using Scripts or Functions . . . . . . . . . . . . . . . . 36-167

Test Performance Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 36-171

Measure Fast Executing Test Code . . . . . . . . . . . . . . . . . . . . . . . . 36-177

Create Mock Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-180

Specify Mock Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-187


Define Mock Method Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 36-187
Define Mock Property Behavior . . . . . . . . . . . . . . . . . . . . . . . . . 36-188
Define Repeating and Subsequent Behavior . . . . . . . . . . . . . . . . 36-189
Summary of Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-191

Qualify Mock Object Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . 36-192


Qualify Mock Method Interaction . . . . . . . . . . . . . . . . . . . . . . . . 36-192
Qualify Mock Property Interaction . . . . . . . . . . . . . . . . . . . . . . . 36-193
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-194
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-196

Ways to Write Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-198


Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-198
Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-199
Class-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-199
Extend Unit Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . 36-200

Compile MATLAB Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-201


Run Tests with Standalone Applications . . . . . . . . . . . . . . . . . . . 36-201
Run Tests in Parallel with Standalone Applications . . . . . . . . . . . 36-202
TestRand Class Definition Summary . . . . . . . . . . . . . . . . . . . . . . 36-202

Generate Code Coverage Report in HTML Format . . . . . . . . . . . . 36-204


Statement Coverage for MATLAB Source Code . . . . . . . . . . . . . . 36-204
Function Coverage for MATLAB Source Code . . . . . . . . . . . . . . . 36-204
Generate and Analyze HTML Coverage Report . . . . . . . . . . . . . . 36-205
QuadraticPolynomial Class Definition . . . . . . . . . . . . . . . . . . . . . 36-207

Insert Test Code Using Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-209


Create Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-209
Add Parameters and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-209
Run Tests in Test Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-212

Develop and Integrate Software with Continuous Integration . . 36-213


Continuous Integration Workflow . . . . . . . . . . . . . . . . . . . . . . . . 36-213
Continuous Integration with MathWorks Products . . . . . . . . . . . 36-215

Generate Artifacts Using MATLAB Unit Test Plugins . . . . . . . . . . 36-217

Continuous Integration with MATLAB on CI Platforms . . . . . . . . 36-221


Azure DevOps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-221
Bamboo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-221
CircleCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-221
GitHub Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-221
GitLab CI/CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-222
Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-222

xxxiii
Travis CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-222
Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36-222

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

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


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

System object Usage and Authoring


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

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


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

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


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

Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-11


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

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

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


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

xxxiv Contents
Initialize Properties and Setup One-Time Calculations . . . . . . . . . . . . . 38-18

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

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


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

Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-24


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

Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-26


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

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


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

Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-32

Define Composite System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-34

Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-36


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

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


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

Define System Object Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-41

Handle Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-43


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

Summary of Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-45


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

Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-48


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

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


General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-50

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

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


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

Analyze System Object Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-58

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


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

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

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

Create Composite System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38-76

xxxvi Contents
Language

37
1

Syntax Basics

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


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

Continue Long Statements on Multiple Lines


This example shows how to continue a statement to the next line using ellipsis (...).

s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...


- 1/6 + 1/7 - 1/8 + 1/9;

Build a long character vector by concatenating shorter vectors together:

mytext = ['Accelerating the pace of ' ...


'engineering and science'];

The start and end quotation marks for a character vector must appear on the same line. For example,
this code returns an error, because each line contains only one quotation mark:

mytext = 'Accelerating the pace of ...


engineering and science'

An ellipsis outside a quoted text is equivalent to a space. For example,

x = [1.23...
4.56];

is the same as

x = [1.23 4.56];

1-2
Name=Value in Function Calls

Name=Value in Function Calls


Since R2021a

MATLAB supports two syntaxes for passing name-value arguments.

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

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

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

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

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

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

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

Function authors do not need to code differently to support both the name,value and name=value
syntaxes. For information on authoring functions that accept name-value arguments, see “Name-Value
Arguments” on page 27-11.

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

1-4
Variable Names

Variable Names

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

Valid Names
A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB is case
sensitive, so A and a are not the same variable. The maximum length of a variable name is the value
that the namelengthmax command returns.

You cannot define variables with the same names as MATLAB keywords, such as if or end. For a
complete list, run the iskeyword command.

Examples of valid names: Examples of invalid names:


x6 6x
lastValue end
n_factorial n!

Conflicts with Function Names

Avoid creating variables with the same name as a function (such as i, j, mode, char, size, and
path). In general, variable names take precedence over function names. If you create a variable that
uses the name of a function, you sometimes get unexpected results.

Check whether a proposed name is already in use with the exist or which function. exist returns
0 if there are no existing variables, functions, or other artifacts with the proposed name. For example:

exist checkname

ans =
0

If you inadvertently create a variable with a name conflict, remove the variable from memory with the
clear function.

Another potential source of name conflicts occurs when you define a function that calls load or eval
(or similar functions) to add variables to the workspace. In some cases, load or eval add variables
that have the same names as functions. Unless these variables are in the function workspace before
the call to load or eval, the MATLAB parser interprets the variable names as function names. For
more information, see:

• “Unexpected Results When Loading Variables Within a Function”


• “Alternatives to the eval Function” on page 2-91

1-5
1 Syntax Basics

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

1-6
Case and Space Sensitivity

Case and Space Sensitivity


MATLAB code is sensitive to casing, and insensitive to blank spaces except when defining arrays.

Uppercase and Lowercase

In MATLAB code, use an exact match with regard to case for variables, files, and functions. For
example, if you have a variable, a, you cannot refer to that variable as A. It is a best practice to use
lowercase only when naming functions. This is especially useful when you use both Microsoft®
Windows® and UNIX®1 platforms because their file systems behave differently with regard to case.

When you use the help function, the help displays some function names in all uppercase, for
example, PLOT, solely to distinguish the function name from the rest of the text. Some functions for
interfacing to Oracle® Java® software do use mixed case and the command-line help and the
documentation accurately reflect that.

Spaces

Blank spaces around operators such as -, :, and ( ), are optional, but they can improve readability.
For example, MATLAB interprets the following statements the same way.

y = sin (3 * pi) / 2
y=sin(3*pi)/2

However, blank spaces act as delimiters in horizontal concatenation. When defining row vectors, you
can use spaces and commas interchangeably to separate elements:

A = [1, 0 2, 3 3]

A =

1 0 2 3 3

Because of this flexibility, check to ensure that MATLAB stores the correct values. For example, the
statement [1 sin (pi) 3] produces a much different result than [1 sin(pi) 3] does.

[1 sin (pi) 3]

Error using sin


Not enough input arguments.

[1 sin(pi) 3]

ans =

1.0000 0.0000 3.0000

1 UNIX is a registered trademark of The Open Group in the United States and other countries.

1-7
1 Syntax Basics

Choose Command Syntax or Function Syntax

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

For introductory information on calling functions, see “Calling Functions”. For information related to
defining functions, see “Create Functions in Files” on page 21-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 variables. 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

disp A

1-8
Choose Command Syntax or Function Syntax

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

ls ./d

1-9
1 Syntax Basics

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 can access the current workspace and path
to determine whether ls and d are functions or variables. However, some components, such as the
Code Analyzer and the Editor/Debugger, operate without reference to the path or workspace. 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
any ambiguity.

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

1-10
Resolve Error: Undefined Function or Variable

Resolve Error: Undefined Function or Variable

Issue
You may encounter the following error message, or something similar, while working with functions
or variables in MATLAB:

Undefined function or variable 'x'.

These errors usually indicate that MATLAB cannot find a particular variable or MATLAB program file
in the current directory or on the search path.

Possible Solutions
Verify Spelling of Function or Variable Name

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

Often, when you misspell a MATLAB function, a suggested function name appears in the Command
Window. For example, this command fails because it includes an uppercase letter in the function
name:

accumArray

Undefined function or variable 'accumArray'.

Did you mean:


>> accumarray

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

Verify Inputs Correspond to the Function Syntax

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

Make Sure Function Name Matches File Name

When you write a function, you establish its name when you write its function definition line. This
name should always match the name of the file you save it to. For example, if you create a function
named curveplot,

function curveplot(xVal, yVal)


- program code -

then you should name the file containing that function curveplot.m. If you create a pcode file for
the function, then name that file curveplot.p. In the case of conflicting function and file names, the
file name overrides the name given to the function. In this example, if you save the curveplot

1-11
1 Syntax Basics

function to a file named curveplotfunction.m, then attempts to invoke the function using the
function name will fail:
curveplot
Undefined function or variable 'curveplot'.

If you encounter this problem, change either the function name or file name so that they are the
same.

To Locate the file that defines this function, use the MATLAB Find Files utility as follows:
1
On the Home tab, in the File section, click Find Files.
2 Under Find files named, enter *.m
3 Under Find files containing text, enter the function name.
4 Click the Find button

Make Sure Necessary Toolbox Is Installed and Correct Version

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

If you do not know which toolbox contains the function you need, search for the function
documentation at https://www.mathworks.com/help. The toolbox name appears at the top of the
function reference page. Alternatively, for steps to identify toolboxes that a function depends on, see
“Identify Program Dependencies” on page 26-2.

Once you know which toolbox the function belongs to, use the ver function to see which toolboxes
are installed on the system from which you run MATLAB. The ver function displays a list of all
currently installed MathWorks® products. If you can locate the toolbox you need in the output
displayed by ver, then the toolbox is installed. If you cannot, you need to install it in order to use it.
For help with installing MathWorks products, see “Install License Manager on License Server”.

1-12
Random documents with unrelated
content Scribd suggests to you:
an end. All that can be claimed on its behalf is that a few further
steps in advance may have been made good. It may seem to have
resulted rather in the restatement of some of the problems than in
their solution. But this is what might be expected from the attempt
to approach a subject which has many sides especially with light
from the tribal side only. Following the true method of working from
the known to the unknown, it is not until such a problem has been
approached separately from its different sides that a final solution
can be reached; and this involves the fellow work of many historical
students.
In the meantime, without ignoring or seeking to minimise the
force of other important influences, it may, I think, safely be said
that we have found the influence of tribal custom upon Anglo-Saxon
polity and economic conditions as apparent, all things considered, as
there could be reason to expect.
It was a factor in economic development which, among others and
in due proportion, has to be reckoned with, and its study has the
special value that it helps to bring the student of the Anglo-Saxon
laws to regard them from the point of view of the Anglo-Saxon
settlers themselves.
FOOTNOTES
[1] Origin of Currency and Weight Standards, Camb. U. Press,
1892.
[2] For convenience I adhere throughout to reckoning in wheat-
grains. Professor Ridgeway informs me that three barleycorns
were equated with four wheat-grains, and that a passage in
Theophrastus shows that in the fourth century b.c. 12 barleycorns
= obol and 12 obols = the stater. The Greek diobol = therefore
24 barleycorns, i.e. 32 wheat-grains, and the stater = 144
barleycorns, i.e. 192 wheat-grains. The reader will understand
that as Romans, Celts, Anglo-Saxons, and Normans reckoned in
wheat-grains, there will be great convenience in adhering
throughout to wheat-grains in this inquiry. And further the
theoretic building up of weights in wheat-grains was preserved
traditionally more easily than the actual standards of weight.
[3] The range of the variation in the actual weight of the stater
as a coin (without necessarily implying variation in the theoretic
weight in wheat-grains) is given by metrologists as follows:

Grammes
Babylonian 8·18
Crœsus 8·18
Darius 8·36 to 8·41
Attic 8·64 to 8·73
Philip of Macedon and Alexander the Great 8·73
The Greek cities on the Black Sea 9·06

[4] Kinship &c. in Arabia, p. 53.


[5] Ordinances of Manu, xi. pp. 128-131.
[6] Sections 8 and 11.
[7] Herod. v. c. 77 and vi. c. 79.
[8] The latest results of metrological research are most
conveniently stated by Hultsch in his Die Gewichte des Alterthums
nach ihrem Zusammenhange dargestellt, Leipzig, 1898. And Mr. F.
G. Hill, of the British Museum, has recently issued an excellent
hand-book of the Greek and Roman coins containing information
on these points.
[9] The relation of the ancient Gallic gold currency to the
subject of wergelds is interesting and important, but cannot be
enlarged upon here.
[10] For the authorities for the following short statement see
infra, Chap. VII. s. 1.
[11] Besides these silver tremisses some silver scripula were
issued, but it is with the sceatts mainly that we have to do. In
connection with the next section, however, the fact that the
scripulum was current as a coin is worth notice.
[12] Metrologicorum Scriptorum Reliquiae (Lipsiae, 1866).
[13] Hultsch, Die Gewichte des Alterthums, pp. 53 and 203.
[14] Metr. Script. ii. 131-139.
[15] Hultsch, Metr. Script. i. pp. 66 and 87.
[16] Athelstan, vi. 6, s. 2 and vi. 3; and see Schmid’s Glossary
under Geldrechnung.
[17] There can hardly have been at Tours at this moment any
other Liutgarda than the queen under Alcuin’s spiritual charge.
[18] For this incident see Alcuini Epist. xxv.
[19] Metr. Script. ii. 31, 99, 114, &c.
[20] For the references to the Codes and Extents, and
authorities for the statements in this summary, the reader must
be referred to the former volume. But for additional statements
full references will be given. Where not otherwise stated, the
figures refer to the two volumes of Ancient Laws of Wales.
[21] Prof. Rhys informs me that da in Carnarvonshire local
dialect still means ‘cattle,’ while in other parts of Wales it has the
wider meaning of ‘goods.’
The allotment of cattle involved grazing rights, and often
separate homesteads. Accordingly in the Denbigh Extent we find
that so and so ‘habet domum’ or ‘non habet domum.’
This dependence for maintenance of the boy upon the higher
chieftain is indirectly confirmed by the Extents, which mention
among the chieftain’s rights the ‘fosterage of youths’ &c. See
Tribal System in Wales, p. 169.
That the chieftain who gives the da was the ‘chief of kindred’
and not a mere territorial lord is shown by the fact that when a
stranger family have lived in the land till they have formed a
kindred by intermarriage with Cymraes, all the members of the
family become ‘man and kin’ to the chief of kindred of the new
kindred. Tribal System in Wales, p. 132.
[22] i. pp. 167-169.
[23] p. 543.
[24] p. 549, s. 19.
[25] i. 96 and 545.
[26] If the sister was married to an alltud and her son killed a
person, ⅔ of the galanas fell on the mother’s kindred (i. p. 209),
but there was no liability beyond the gwely or second cousins (ii.
p. 657).
[27] ‘The galanas of every female shall always be to the
kindred,’ i. p. 241.
[28] ‘Three cases wherein a wife is to answer without her
husband. The first is for homicide,’ i. p. 463. But for accessories
to murder she and her husband pay her camlwrw and derwy, i. p.
105; and she can claim spearpenny, i. pp. 103, 705; ii. p. 65.
[29] i. pp. 231-3, 409, 517, 747; ii. p. 695. On separation
husband and wife divided the cattle and most other things
equally.
[30] ii. pp. 281-2, 740.
[31] i. p. 765; ii. p. 269.
[32] ii. p. 693.
[33] ii. p. 531.
[34] i. p. 415.
[35] i. pp. 259, 447.
[36] Tribal System in Wales, App. p. 59, &c.
[37] The principal tref as contrasted with summer bothy on the
mountains.
[38] ii. p. 563.
[39] i. p. 795.
[40] Fol. 280.
[41] i. pp. 283, 499.
[42] i. pp. 111, 459, 745; ii. p. 257.
[43] i. pp. 201, 535.
[44] ii. p. 493.
[45] i. p. 141.
[46] i. p. 229.
[47] For the following statements see Venedotian Code, i. p.
223, &c.; and Dimetian Code, i. p. 407, &c.
[48] Sisters paid for their possible children, and if these
children were of age they paid instead of their mothers. After the
age at which they could not have children, the sisters did not pay
(i. p. 99). That the daughter after twelve was independent of her
father with da of her own, see i. p. 205.
[49] i. p. 229.
[50] i. pp. 77, 103.
[51] ii. p. 693.
[52] i. p. 747.
[53] i. p. 231.
[54] i. p. 271.
[55] i. p. 565.
[56] English Village Community, c. ix.
[57] For details and references to the Codes I must refer the
reader to Chap. V. of The Tribal System in Wales.
[58] See infra, p. 319.
[59] In the quotation of passages from Beowulf I have mostly
followed Professor Earle’s translations.
[60] See Structure of Greek Tribal Society, by H. E. Seebohm,
chap. ii.
[61] Nefan cannot mean son or grandson, for Hygelac was his
father and his grandfather was Hrethel.
[62] The references in this chapter are to the four volumes of
The Ancient Laws of Ireland. I regret very much not to have had
the advantage of vol. v. edited by Dr. Atkinson and not yet
published, but I am greatly indebted to him for his kind help and
advice on many difficult points.
[63] iv. p. 259. This passage is abridged.
[64] iii. p. 69.
[65] Ibid. and iv. 245-248.
[66] Cours de Littérature Celtique, tome vii. Etude sur le Droit
Celtique, tome i. p. 186.
[67] The view here taken, that the four fines in the geilfine
division are classes or grades of relationship, makes more
intelligible the rules laid down in the Book of Aicill (iii. 331-335),
especially the one which determines that ‘if one person comes up
into the “geilfine” so as to make it excessive, a man must go out
of it into the “deirbhfine,” and a man is to pass from one division
into the other up as far as the indfine, and a man is to pass from
that into the community.’ Obviously, as a fresh generation comes
into the nearest hearth, a generation at the top naturally moves
out of the group. The great-grandfather becomes a great-great-
grandfather, and so on.
[68] i. p. 263, and iv. p. 245.
[69] iv. p. 245.
[70] iii. p. 99.
[71] p. 101.
[72] ii. p. 195.
[73] iv. p. 283.
[74] Dr. Atkinson has kindly given me a reference to MS. H. 3-
18, 237 and 485, the former of which ends its paragraph on
‘sencleithe’ thus:—‘If he serve from that onward, till the fifth man
come and during the time (his time?), then he is a sencleithe and
he cannot go from the heirs [comarba] for ever after.’
[75] See Senchus Mor, i. p. 76 and iii. p. 43.
[76] i. pp. 65-77.
[77] Round Towers of Ireland, p. 219.
[78] Fol. 181, b.b. This will be inserted in Dr. Atkinson’s vol. v.
of the Brehon Laws.
[79] In one MS. ‘six score ounces.’
[80] Petrie, Round Towers of Ireland, p. 214.
[81] Tripartite Life of St. Patrick, ii. p. 372.
[82] Ibid. i. p. 212.
[83] Altilia, i.e. fattened heifers, Skeat, sub voce ‘heifer.’
[84] The samaisc heifer of the Brehon Laws being ½ oz., and
the dairt heifer ⅙ oz., the fattened heifer would naturally take the
middle place between them as ¼ oz.
[85] Wasserschleben refers these canons to the fifth century
Synod under St. Patrick.
[86] ‘Si colirio indiguerit’ seems to be equivalent to the Irish
‘that requires a tent.’ But Dr. Atkinson informs me that the Irish
word literally means ‘a plug of lint.’
[87] Compare this clause with the ‘Book of the Angel,’ Tripartite
Life, ii. p. 355. ‘Item si non receperit prædictum præsulem in
hospitium eundem et reclusserit suam habitationem contra illum,
septem ancillas (cumala) sive septem annos pœnitentiæ similiter
reddere cogatur.’
[88] See Senchus Mor, i. p. 43: ‘Equal dire-fine for a king and a
bishop, i.e. equal honour-price to the “rig tuath” and the bishop,
i.e. of the church of a “rig tuath.”’
[89] P. 141.
[90] i. p. 127.
[91] Pœnitentiale Vinnicii, s. 23; Wasserschleben, p. 113.
[92] Tripartite Life, p. 378 n.
[93] Questions Historiques, pp. 105-117.
[94] Pertz, Bluhme’s preface, p. 498.
[95] Bindung’s Das Burgundisch-Romanische Königreich von
443 bis 532 n. Chr. chap. i.
[96] Lib. vii. Tit. iii. s. 3.
[97] Lib. vi. Tit. v.
[98] Hessels and Kern, Codex x. Tit. lxi.
[99] Brunner (Sippe und Wergeld, p. 31) prefers the reading of
the other codices, ‘on either side,’ but the principle is the same;
the fisc gets whatever share lapses, whether it be ¼ or ½.
[100] Hessels and Kern, Tit. ci., p. 412. Pertz, Legg. 11, 5.
[101] This translation of the final clause does not materially
vary in meaning from that of Brunner, Sippe und Wergeld, p. 34.
[102] Brunner, Sippe und Wergeld, p. 34.
[103] Sippe und Wergeld, p. 34.
[104] Sippe und Wergeld, p. 36. ‘Es dünkt mir sehr
wahrscheinlich, dass auch in der Lex Salica unter den ‘tres
proximiores’ Verwandte von drei verschiedenen Parentelen
gemeint sind.’
Later examples of division of wergelds in other districts quoted
by Brunner show that the division of the kindred into three similar
grades or groups was prevalent also in Frisian and Saxon districts.
[105] Codices 3, 7, 8, 9 have ‘de quod non.’
[106] Tit. xxix.
[107] See his essay on this subject in his Problèmes d’Histoire,
pp. 361 &c.
[108] Brunner, in his Sippe und Wergeld, shows this clearly, pp.
1-3.
[109] Blumenstock, i. 266.
[110] Tit. lvi.
[111] Tit. vi.
[112] The clause De reipus is very important in regard to some
of these points. But the subject is too difficult a one to be
discussed here.
[113] Guérard, on the other hand, says: ‘C’est l’alleu d’un
Salien défunt que la loi divise en deux parts: dans l’une est la
terre salique, et dans l’autre la terre non salique; mais ces deux
terres sont également partie de la succession du défunt.’
Polyptique d’Irminon, i. p. 487. But he does not seem to have
noted the use of ‘land’ unqualified in the saving clause of the first
4 codices.
[114] Erbenfolge, &c., pp. 12-14.
[115] Deutsches Wirtschaftsleben, i. p. 39.
[116] Polypt. d’Irminon, i. p. 495.
[117] English Historical Review, January 1893.
[118] Hessels and Kern, Tit. 78; Pertz, Leg. ii. p. 10.
[119] See note in Hessels and Kern, and Amira in his
Erbenfolge, p. 16 (München, 1874).
[120] See supra, pp. 26, 27.
[121] This is repeated, ii. p. 391. ‘The argluyd takes him as a
son, and if he die receives his da unless he leaves a son.’ Up to
14 his father was his ‘argluyd.’
[122] Sohm, in his preface to the Lex in Pertz (dated 1882), p.
188, concludes that this clause and clause 36 must be referred to
the sixth century. There is a Formula in Marculf’s collection in
which instructions are given to a newly appointed official, inter
alia, to judge Franks, Romans, Burgundians, and those of other
nations ‘secundum lege et consuetudine eorum’ (Marc. Form., Lib.
i. 8.)
[123] In the Burgundian Law the wergeld is 150 solidi; in the
Alamannic Law, 160 solidi; in the Bavarian law, 160 solidi. That
this was also the wergeld of the Frisian and Saxon see infra.
[124] See Merkel’s preface to the laws in Pertz, p. 14.
[125] In the Burgundian laws the division is into ‘optimatus’
with a wergeld of 300 sol., ‘mediocris’ with 200 sol., and ‘minores
’ with 150 sol.
[126] Introduction, p. iv.
[127] Martini, Metrologia, sub ‘Roma.’ See also Traité de
Numismatique du Moyen Age, par A. Engel, vol. i. p. 222.
[128] Pertz, p. 119.
[129] Pertz, p. 149.
[130] Hludowici et Hlotharii Capitularia, Pertz, p. 251.
[131] De Mirac. S. Martini, l. i. c. 31. Mention of the aureus
occurs twenty-four times in the index to his works. Mention of
trientes occurs twelve times, and of argentei five times.
[132] In some codices placed at the end of Lib. xii., Tit. ii. See
edition of Walter (1824), p. 669.
[133] ‘Ex Isidori Etymologiarum Libris, c. De ponderibus.’
Hultsch, ii. 113.
[134] Twelve argentei (12 × 72 w.g.) = 864 w.g., or at 1:10 the
Merovingian solidus of 86·4 w.g.
[135] Pertz, p. 18.
[136] Pertz, p. 31.
[137] Pertz, p. 39.
[138] In this Capitulare three grades of payments are stated, a
pound, a half-pound, and five solidi. Five solidi in this scale should
be ¼ lb., and in wheat-grains the scale would be 6912, 3456, and
1728. 1728 wheat-grains is 5 solidi of 12 denarii of 28·8.
[139] Capitulare Mantuanum, s. 9, ‘De moneta: ut nullus post
Kalendas Augustas istos dinarios quos modo habere visi sumus
dare audeat aut recipere: si quis hoc fecerit, vannum nostrum
conponat.’
[140] Beiträge zur Geschichte des Geld- und Münzwesens in
Deutschland.
[141] Forty argentei or drachmæ to the solidus would have
meant a ratio of about 1:30.
[142] Polyptique d’Irminon, Introduction, i. 151. See also No.
82 of St. Gall Charters (Wastmann, i. p. 78), in which is an annual
payment of ‘i bovem v solidos valentem’ sub anno a.d. 778.
[143] Pertz, p. 85.
[144] Pertz, p. 114.
[145] Pertz, p. 116.
[146] Pertz, p. 72. Refusing to receive the new denarii must
have meant as 12 to the solidus, for the new denarii themselves
were heavier than the old ones, 32 wheat-grains instead of 28·8.
[147] Pertz, p. 494. Karoli II. Edictum Pistense, a.d. 864: Ut in
omni regno nostro non amplius vendatur libra auri purissime
cocti, nisi duodecim libris argenti de novis et meris denariis.
Illud vero aurum quod coctum quidem fuerit, sed non tantum
ut ex deauratura fieri possit eo libra una de auro vendatur decem
libris argenti de novis et meris denariis.
[148] Traité de Numismatique du Moyen Age, par Arthur Engel
(Paris, 1891), vol. i. pp. 329-332.
[149] Sohm, in his preface to the Ripuarian law in Pertz,
against his own former opinion, concludes that clause xxxvi. did
go back to the sixth century, and was originally a part of the Lex
(p. 188).
[150] See Richthofen’s preface to the Frisian Laws in Pertz, p.
631.
[151] They appear in the ‘Additio Sapientium,’ Tit. ii., clauses
lxiii. and lxxviii.
[152] ‘Inter Wisaram et Laubachi, duo denarii novi solidus est.’
[153] ‘Inter Laubachi et inter Flehi, tres denarii novæ monetæ
solidum faciunt.’
[154] ‘Inter Flehi et Sincfalam solidus est duo denarii et
dimidius ad novam monetam.’ That the word denarius was
applied to gold as well as silver coins, see mention of the ‘gold
penninck’ of Gondebald in Chronijck van Vrieslandt, sub a.d. 739.
[155] ‘Inter Laubachi et Wisaram weregildus nobilis 106 solidi
et duo denarii, liberi 53 solidi et denarium, liti 26 solidi et dimidius
et dimidius tremissis.’
[156] ‘Si nobilis [or liber or litus] nobilem occiderit, 80 solidos
componat; de qua muleta duæ partes ad hæredem occisi, tertia
ad propinquos ejus proximos pertineat … liberum solidos 53 et
unum denarium solvat … litum 27 solidos uno denario minus
componat domino suo, et propinquis occisi solidos 9 excepta
tertia parte unius denarii.’
[157] ‘Inter Fli et Sincfalam weregeldus nobilis 100 solidi, liberi
50, liti 25 (solidi denarii 3 novæ monetæ).’
[158] ii. lxxxiv.
[159] Tit. vi.
[160] Engel’s Traité de Numismatique du Moyen Age, i. 233 and
329.
[161] Martini’s Manuale de Metrologia, sub ‘Emden.’ And
compare Ridgeway, p. 871. He shows that in Italy and Sicily 10
sheep = 1 cow.
[162] It is true that in the clauses trebling the amounts for
wounds it is not directly stated that the wergelds were also
trebled; but the use of the words in Tit. I., ‘in simplo,’ suggests
that it may have been so; whilst the facts that the triple payment
for the loss, e.g. of the eye, which in the title De Dolg was a half
wergeld, would otherwise exceed the full wergeld, and that, in
the one case in which in the ‘De Dolg’ the whole wergeld was
payable, the amount in the Additio is the treble wergeld, make it
almost certain that it was so, otherwise the injury would be paid
for at three times the value of a man’s life.
[163] 4608 × 3 = 13824, i.e. 160 solidi of 86·4 wheat-grains.
The wergeld of the Island of Gotland was also 3 gold marks or
160 solidi of Merovingian standard. See also on the whole
question Dr. Brunner’s article ‘Nobiles und Gemeinfreie der
Karolingischen Volksrechte’ in Zeitschrift der Savigny-Stiftung &c.,
vol. xix.
[164] It would exactly equal 200 of the local solidi of two
tremisses at a ratio of 1:8, or 160 solidi of 80 wheat-grains
instead of 86·4.
[165] Pertz, p. 83.
[166] Pertz, p. 84.
[167] Pertz, p. 34.
[168] Pertz, p. 84.
[169] Pertz, p. 118.
[170] See Du Cange sub voce ‘Pecunia,’ and the cases there
mentioned in which the word = pecudes, grex, &c.
[171] See Études sur la Lex dicta Francorum Chamavorum et
sur ‘Les Francs du Pays d’Amor,’ par Henri Froidevaux. Paris, 1891,
chap. ii.
[172] It has already been stated that the wergeld of the Island
of Gotland was three gold marks or 160 Merovingian solidi. But
owing to the late date of the Gotland laws it cannot be regarded
as certain that the amount was the same at the date of the
Ripuarian laws.
[173] The depreciation in weight cannot have been the result
of ignorance of the Roman standard. We learn from the excellent
table given by Montelius in his Remains from the Iron Age of
Scandinavia that the gold solidi of the Eastern Empire found their
way into the Islands of Gotland, Oland, and Bornholm in
considerable numbers, between a.d. 395 and 518. He shows that,
while no silver coins of the Republic or before Nero have been
found in Scandinavia, coins belonging to the silver currency of
Rome after Nero found their way northwards in considerable
numbers. Of Roman coins a.d. 98-192 only four gold coins are
known to have been found and 2304 silver coins. Then the gold
currency begins, and of dates between a.d. 235-395, sixty-four
gold coins have been found and only one solitary silver coin.
Lastly came the gold currency of the solidus of Constantine and
his successors a.d. 395-518, and of this period 286 gold coins and
one silver coin are recorded as having been found in Scandinavia.
It is clear, then, that the Roman standard as well as the Roman
system of division of the lb. was known in the North. For a long
period no doubt the chief trade of the Baltic was with the
Byzantine Empire and the East.
[174] Die Entstehungszeit der älteren Gulathingslög von Dr.
Konrad Maurer, p. 5.
[175] The Reksthane is an official, and quite a different person
from the Bónde.
[176] The Árborinn man seems to be the same as the
Aettborinn man, i.e. ‘a man born in a kindred.’
[177] Elsewhere called the Odal-born-man.
[178] The hauld seems to have been the same as the odal-born
man.
[179] See also the Frostathing Law IV. 31, in which in a similar
case the person is outlawed.
[180] The Nefgildi-men include the slayer’s mother’s father,
daughter’s son, mother’s brother, sister’s son, father’s sister’s son,
mother’s sister’s sons, son’s daughter’s son, daughter’s daughter’s
son, brother’s daughter’s son, sister’s daughter’s son.
[181] 4608 × 30 = 138240, and this divided by 8 = 17280 w.g.
of gold, i.e. 200 gold solidi of 86·4 w.g.
[182] The following is from the Venedotian Code, i. p. 179. ‘The
ecclesiastical law says that no son is to have the patrimony but
the eldest born to the father by the married wife: the law of
Howell, however, adjudges it to the youngest son as well as to
the oldest (i.e. all the sons), and decides that sin of the father or
his illegal act is not to be brought against a son as to his
patrimony.’ Bastards were not excluded till the Statute of Rothllan.
[183] ‘Geschlecht und Verwandtschaft im alt-norwegischen
Rechte,’ in the Zeitschrift für Social- und Wirthschaftsgeschichte,
vol. vii. (Weimar). To this essay I am much indebted.
[184] Some authorities infer from this that the parents alone
were put in the grave. K. von Maurer thinks only the children, and
apologises for it as ‘nur eine aus grauer Vorzeit überlieferte
Antiquität.’
[185] Skåne, being only divided from the island of Zealand by
the Sound, during the Viking period belonged to Denmark. It
afterwards became a Swedish province, being finally ceded by
Denmark in 1658.
[186] The various views upon the relation of the two versions
to each other are very usefully discussed in the introduction to M.
Beauchet’s Loi de Vestrogothie (Paris, 1894), pp. 67-75. The Latin
version was published in 1846 at Copenhagen as Vol. I. of the
Samling af Danske Love and both Latin and Danish versions in Dr.
Schlyter’s Corpus Juris Sueo-Gotorum antiqui, Lund. 1859.
[187] See Du Cange, s. v. ‘Moventes’ = pecudes.
[188] ‘Filius-familias’ in another MS.
[189] As to the fælagh or partnership between husband and
wife, see the Gulathing Law, 53. The word fælagh seems to be
equivalent to the ‘definitio’ of the Latin text, the definitio of the
property being made at the time of the marriage. The word
seems to be allied to the English word ‘fellowship.’ See Skeat, sub
‘fellow,’ who refers it to Icelandic ‘felag,’ literally ‘a laying together
of property.’
[190] See Untersuchungen zur Erbenfolge &c., Julius Ficker, ii.
p. 143: ‘Gulathingsbuch und Frostathingsbuch kennen keinen
Eintritt der Sohnessöhne in das volle Recht des Parens.’
[191] Beauchet, p. 60.
[192] Addition F. 1.
[193] Skanska Stadsrätten, s. 43.
[194] See I. s. 92 of the Danish version. The word Manbötær =
mulcta homicidii, Schlyter, Gloss. sub voce.
[195] See Ancient Laws of Scotland, preface, p. 42.
[196] Ibid. i. 8.
[197] These extracts are abridged and put into modern English.
[198] Compare the colpindach with the Irish ‘colpach heifer.’ In
the Crith Gabhlach, p. 300, the Irish text has the word colpdaig
translated ‘colpach heifer.’ Probably the xxix should be ixˣˣ, i.e.
180. See Ancient Laws of Scotland, p. 270 (red paging), as to the
next clause.
[199] ‘Oc-thigernd’ = ‘Jung herr,’ Windisch, p. 757.
[200] Scotland under her early Kings, i. p. 258 n., and ii. p.
307.
[201] Ancient Laws of Scotland, i. p. 233.
[202] History of English Law, Pollock and Maitland, i. pp. 145
and 202. There is an elaborate comparison of this Scotch treatise
with Glanville’s in the Ancient Laws of Scotland commencing at p.
136 (red), which is very helpful.
[203] Book of Deer, preface, p. lxxxi. Toshach (toisech). The
two officers in a townland were the mormaer and the toisech.
Ced in Irish = hundred. Tosh-ced-erach possibly may have meant
‘head of the hundred.’
[204] See infra, c. xi.
[205] Robertson’s Historical Essays, p. 47.
[206] See preface to the Ancient Laws of Scotland.
[207] Gulathing law, s. 152.
[208] See Windisch, Wörterbuch, sub voce ‘ter-fochrice,’ also
‘fo-chraic.’
[209] Vol. i. p. 655.
[210] This passage is from the last clause in the so-called
treaty between Edward and Guthrum, ‘when the English and
Danes fully took to peace and to friendship, and the Witan also
who were afterwards, oft and unseldom that same renewed and
increased with good.’ Thorpe, p. 71; and see Schmid’s Einleitung,
p. xlii.
[211] 120s. of 5d. = 50s. of 12d.
[212] Three marks are double 12 ores.
[213] See the instances of services of sochemen given by Mr.
Round in his invaluable chapter on the Domesday book in his
Feudal England, pp. 30-34, from the ‘Ely placitum’ of 1072-1075:
‘Qui quotiens abbas preceperit in anno arabunt suam terram’ &c.
And again quotienscunque ipse præceperit in anno arabunt’ &c.
These are services of the sochemanni of Suffolk and Norfolk ‘qui
non possunt recedere.’
[214] Cf. Ine, 74. The xls. to be paid for the ‘Waliscus’ slave
who had committed homicide may be double value by way of
penalty.
[215] Laws of Cnut, s. 63 and s. 66.
[216] Mr. Keary’s Introduction to the Catalogue of the Coins in
the British Museum, Anglo-Saxon series, vol. ii. p. lxxxi.
[217] Engel, vol. ii. p. 849 et seq.
[218] Introduction, vol. ii. p. lvii.
[219] The word is used in the sense of mint-master or money
coiner. See Du Cange, sub voce ‘Monetarius.’
[220] The Anglo-Saxon pound of 240 pence or 364 grammes
divided by fifteen = 24·2 grammes.
[221] The normal weight of the English penny of 32 wheat-
grains was 1·51 grammes. The coins of Cnut’s predecessors
sometimes fully reached this standard, though oftener somewhat
below it. The exact weight of 1/20 of the Danish ore would be
1·21 grammes, and Cnut’s silver pence seem to aim at this
weight. Out of 574 silver pence of Cnut described in the
Catalogue of the British Museum 400 weigh between ·972 and
1·23 grammes. Only 1½ per cent. are of greater weight.
Ethelred’s silver pence were not by any means generally of full
standard of 32 wheat-grains or 1·51 grammes, but still, out of
339 in the British Museum 25 per cent. are fairly up to this
standard and 90 per cent. are above the weight of the new silver
pence of Cnut—1/20 of his ore. Cnut also reduced the size of the
pence. See the B. M. Catalogue plates.
[222] ‘Grith’ seems to be a Danish word of nearly the same
meaning as ‘frith.’ See Schmid’s Glossary, sub voce.
[223] This is in accordance with Ine, 6.
[224] Laws of Ethelred, ix. (Thorpe, p. 145).
[225] Thorpe, p. 124.
[226] MS. G. British Museum, Cott. Nero A. 1. fol. 5.
[227] Thorpe, p. 141, Schmid, Anhang iv.
[228] Compare Æthelstan, iv. 4.
[229] This, from the Kentish Laws, was correctly quoted.
[230] Schmid, Anhang xii.
[231] Pollock and Maitland, i. p. 20. But see Laws of King
Edmund, s. 4, ‘On Blood-shedding.’ ‘Also I make known that I will
not have to “socn” in my “hirede” that man who sheds man’s
blood before he has undertaken ecclesiastical “bot” and made
“bot” to the kindred,’ &c. See also in s. 6 the use of the words
‘mund-brice and Ham-socn.’
[232] Another reading has xxx. See Schmid, p. 206. The Latin
version has xxv, and the quotation in the Laws of Henry I also has
xxv.
[233] 25 × 240 = 6000 pence = 1200 Wessex scillings of 5d.
[234] Catalogue of English Coins, Anglo-Saxon series.
Introduction, p. xxxi, to vol. ii.
[235] Thorpe (p. 75) appends this clause to the so-called Laws
of Edward and Guthrum. But Schmid considers it as a fragment
and places it in his Anhang vii.
[236] Schmid, Anhang vii. 2; Thorpe, p. 79.
[237] A ceorl’s wergeld is cclxvi thrymsas, i.e. cc scillings by
Mercian law. 266⅔ × 3 = 800 pence or 200 Mercian scyllings of
4 pence.
[238] From the text of MS. D.
[239] The fragment itself is a combination of two or more. But
the statement of wergelds in thrymsas seems to unite them.
Schmid also points out that the eorl had not yet superseded the
ealdorman. See Einleitung, p. lxv.
[240] 2000 thrymsas of 3d. equalled 1200 Wessex scillings of
5d., so that the ceorl with five hides to the king’s utware became
a twelve-hynde man. There is no allusion to the six-hynde status
as a halfway step towards the gesithcund status. And the use of
the word ‘gesithcund’ seems to throw back the original date of
these clauses to that of Ine’s law, the word not being used in
later laws. See Schmid’s Glossary, sub voce ‘Gesith.’
[241] I.e. of pure silver. Compare the same phrase ‘de novis et
meris denariis’ in the Edictum Pistense, a.d. 864, quoted supra, p.
191, n.
[242] See supra, p. 344.
[243] Ancient Laws of Ireland, vol. iv. p. 227.
[244] See supra, p. 345.
[245] Ine came to the throne in a.d. 688, and Alfred’s treaty
with Guthrum was in a.d. 880.
[246] See Schmid’s Glossary sub voce ‘Eideshülfe.’ There is only
one mention of oaths of so many hides in the later Anglo-Saxon
laws, viz. in Alfred, s. 11, in which it is stated that a woman must
clear herself from a charge of previous unchastity with 60 hides.
[247] The monk’s oath was one fourth of the priest’s in value:
so 400 argentei = one fourth of 800 sicli.
[248] See Schmid’s introduction, where he states his reasons
for placing Ine’s Dooms before Alfred’s in his edition of the Laws.
[249] This is repeated in Henry I. lxix.
[250] Schmid, Anhang ii.
[251] Schmid, Glossary, sub voc. ‘Die Britischen Einwohner von
Cumberland.’ But the mention of York is conclusive.
[252] See Schmid’s note on this passage, and see also
Liebermann’s translation.
[253] Thorpe, p. 150; Schmid, Anhang i.
[254] The only mark of the geographical position of the district
is that in the final clause: ‘Formerly the Went-sætas belonged to
the Dun-sætas, but more properly they belong to the West
Saxons; there they shall give tribute and hostages.’
[255] Translated in the Latin version by ‘corium,’ the meaning
probably being that 12 scillings would buy off a scourging.
[256] In the Laws of Henry I. (lxx. s. 5) the ‘theow-wealh’ is
translated ‘servus Waliscus,’ and is worth double the ordinary
slave, unless the amount be a double penalty.
[257] The usual explanation of these terms is that they are
derived from the number of shillings in the wergeld. Mr. Earle in
his valuable Handbook to the Land Charters &c. (p. 1) considers
‘hynde’ to be an old form of ‘ten’ and to refer to the number of
soldiers of whom the twelve-hynde and six-hynde men were
captains. ‘The former was a captain of 120 and the latter of 60.’
Neither of these explanations seems to me to be satisfactory.
[258] This view that the single oath of the twelve-hyndeman
was reckoned as a 10 hide oath is confirmed by the translation in
the Latin of the Quadripartitus of Ine’s Laws, s. 46. The Anglo-
Saxon ‘þonne sceal he be lx hyda onsacan,’ is translated by ‘tunc
debet per lx hidas i.e. per vi homines abnegare.’ And in s. 19
‘potest jurare pro lx hidis i.e. pro hominibus vi.’ Schmid remarks
on these passages: ‘Hiernach würde also jeder Eideshelfer 10
Hiden vertreten.’
[259] Schmid, p. 157; Thorpe, p. 97.
[260] Judicia Civitatis Lundoniæ, c. 8, s. 2; Ath. L. vi.
[261] Decretum Episcoporum et aliorum sapientum de Kancia
de pace observanda. Ath. L. iii.
[262] Birch, No. 102, a.d. 701.
[263] Ib. 113, a.d. 705.
[264] Ib. 142, a.d. 725.
[265] Hist. Eccl. lib. iii. c. 24.
[266] Glossary, sub voce ‘Gesith,’ and see Bede, iii. 14 and 22,
iv. 4 and 10, and v. 4 and 5.
[267] Bede, ii. c. ix.
[268] English Village Community, chap. v.
[269] See Transactions of the Royal Historical Society, New
Series, vol. xiv.
[270] English Village Community, p. 117 et seq.
[271] Birch, 412.
[272] Roxburgh Club, p. 138.
[273] Compare ærdian, to inhabit; and so burbærde and
theowbærde, as below.
[274] About a.d. 995. Cod. Dip. 1290.
[275] Cod. Dip. mcccliv. See also Liber Eliensis, p. 120.
[276] Alfred, s. 37.
[277] See supra, pp. 180-185.
[278] The difference in spelling will be noticed. The Kentish
spelling is mostly scætt. Elsewhere the spelling is sceatt.
[279] Schmid, Anhang vii. p. 398.
[280] It cannot be right, I think, to reason the other way with
Schmid, that as there were 30,000 sceatts in the King’s wergeld
of 120 pounds, there must have been 250 sceatts in the pound
and 4·166 sceatts in the Mercian scilling instead of four.
[281] Catalogue &c., Introduction, p. xviii.
[282] ‘We must remember further that many of the coins of the
Kings of Mercia were probably likewise struck in Kent, and that
when we find, as we do, the same moneyers’ names occurring on
the coins of a King of Mercia … and on the coins of Ecgbeorht,
the probability is that these moneyers were Kentishmen who
struck first for one master of their country and then for the other’
(Ib. p. xvii).
[283] See Schmid’s Glossary, sub voce.
[284] See Laws of Ethelbert, ss. 77, 78 and 79, and 83.
[285] In translating Luke xx. 24 and Mark xii. 15, ‘Show me a
penny,’ the word used to translate ‘denarius’ is skatt.
Again, Luke vii. 41, the two debtors, one owing 500 and the
other 50 denarii, are translated by Ulphilas as owing ‘skatte
finfhunda’ and ‘skatte finftiguns.’
Again in John xii. 5, ‘Why was not the ointment sold for 300
denarii?’ ‘ccc skatti’ are the words used, and so also in the parallel
passage Mark xi. 5, ‘thrijahunda skatti.’
In all these cases it seems to be clear that the skatt is the coin.
And that it was a silver coin seems to be shown by the use by
Ulphilas of the word skatt in reference to the ‘thirty pieces of
silver’ in Matt. xxvii. 6-9.
[286] The word occurs seven times in the five Gothic records
from Naples and Arezzo generally appended to editions of ‘Ulfilas.’
In the edition of Massmann (Stuttgart, 1857) see vol. ii. p. 810.
In that of Heyne and Wrede (Paderborn, 1896) see p. 227 &c.
[287] Schmid, Anhang x. p. 404; Thorpe, p. 76.
[288] This may be doubtful: Sceatta scilling-rim, ‘gold to the
worth of 600 scillings,’ Grein, ii. p. 408; sceatta, gen. plural of
‘sceatt,’ nummus, pecunia. Grein, ii. p. 405.
[289] British Museum Catalogue, Anglo-Saxon series, vol. i. xiii.
[290] See Schmid’s Glossary, sub ‘Geldrechnung,’ p. 594. The
inference seems to be too strong to be disregarded. Comparing s.
54 with ss. 70-72, the great toe is valued at 10 scillings, i.e. half
the value of the thumb in s. 54, viz. 20 scillings. And it is stated in
s. 54 that the thumb nail is worth 3 scillings, and in s. 72 that the
toe nail is to be paid for at 30 scætts, which would be half 3
scillings of 20 sceatts. The other toes are said in s. 71 to be
respectively worth half the fingers. The finger nail in s. 71 at 1
scilling compares with the other toe nails at 10 scætts in s. 72—
again one half. Presuming that the scale of one half is maintained
throughout, 30 scætts is half 3 scillings and 10 scætts half one
scilling. The scilling, therefore, must be 20 scætts.
This conclusion is strengthened by the graduated scale of
payments in ss. 33-36, viz. 50 scætts (i.e. 1½ scilling) 3, 4, 10,
20 scillings. See also s. 16, where the scale is 30, 50 (? 60)
sceatts and 6 scillings (120 scætts). In ss. 58-60 a bruise is 1
scilling, covered 30 scætts, uncovered 20 scætts. It seems to be
impossible to make these figures comport with the Mercian
scilling of 4 scætts or the Wessex of 5 scætts or the Salic solidus
of 40 scætts. The conclusion must be that the Kentish scilling was
of 20 scætts.
[291] 576 divided by 10 = 57·6, i.e. two tremisses of 28·8
wheat grains.
[292] Alfred’s words were: ‘But those things which I met with,
either of the days of Ine my kinsman, or of Offa King of the
Mercians, or of Æthelbryht, who first among the English race
received baptism, those which seemed to me the rightest, those I
have here gathered together and omitted the others.’
[293] British Museum Cott. Nero A. 1. fol. 5, and supra, p. 346.
[294] British Museum, ibid. fol. 33 b.
[295] See Gulathing, 178.
[296] Compare Cnut’s secular laws, s. 59, on Borh-bryce. In
both passages the additional words ‘and three to the archbishop’
do not seem to be taken from Kentish law. It is obvious from the
fragment ‘Of Grith and of Mund’ that it was well known that in
Kentish law ‘the mund-bryce of the King and the archbishop were
the same.’
[297] See also Anhang iv. Schmid, p. 385.
[298] See Schmid, Glossary, sub ‘Geldrechnung,’ p. 594.
[299] Konrad von Maurer’s ‘Ueber Angelsächsische
Rechtsverhältnisse,’ in the Kritische Ueberschau, vol. iii. p. 48.
[300] Compare the ‘octogild’ and ‘novigild’ of the Alamannic
and other laws. The literal meaning of ‘xii gylde’ seems to be
payable with ‘twelve times the gylde.’
[301] The division of the words in the MS. is as follows: ‘Gif
cyninges ambiht smið oþþe laadrinc mannan of slehð
medumanleod gelde forgelde.’
[302] So also Grimm in his Deutsche Rechts Alterthümer, p.
653, ‘dimidio, nicht moderato, wie Wilk. übersetzt.’ Compare
‘medeme mynster,’ supra, p. 346, and ‘medeme thegn,’ Cnut, ii.
71, s. 2.
[303] Possibly the King’s servants were otherwise exempt for
injuries done in carrying out their work.
[304] Cf. Book of Aicill, p. 267, where injury inflicted in quick
driving or at work has only a half fine; ‘the excitement of the
work or of quick driving takes the other half fine off them.’ See
also the elaborate rules with regard to accidents of the smith in
his smithy, p. 187 &c. The general rule stated is ‘that the person
who plies the sledge on the anvil is exempt from penalties for
injuries arising from the work he is engaged on;’ and again ‘if
either the sledge or anvil break, he is exempt for injuries to idlers,
and he pays one third compensation to fellow labourers, &c.’
Clerical influence may perhaps be recognised in both the Brehon
and Kentish clauses.
[305] That the soul-scot in later times was paid at the open
grave see Ethelred, v. 12, vi. 20, ix. 13; C. E. 13.
[306] Compare s. 86 and 87, where ealne weorðe means a
‘whole worth’ of an esne, and contrast the ‘medume leodgild’ of
100 scillings payable as bot by the lender with the ‘ealne leod’
payable by the slayer.
[307] That the esne was very near in position to the ‘theow’
see Alf. 43, where Church holidays are to be given to ‘all freemen
but not to theow-men and esne work-men’—‘butan þeowum
mannum & esne-wyrhtum.’
[308] Liebermann considers that the 300 and 100 scillings are
the wergeld of the eorlcundman and the freeman. His translation
reads: ‘welcher steht im 300-Scillwergelde’ and ‘welcher im 100-
Scillwergelde steht.’ Whether these payments are the wergelds is
the point at issue. Schmid, in his note to this passage, favours the
view that 300 scillings was the half-wergeld of the eorl and 100
scillings the half-wergeld of the freeman.
[309] xxxv. 5. ‘Si servus alienus aut laetus hominem ingenuum
occiderit, ipse homicida pro medietatem compositionis illius
hominis occisi parentibus tradatur, et dominus servi aliam
medietatem compositionis se noverit soluiturum.’
[310] ‘Ceorlian,’ to marry a husband; ‘wifian,’ to marry a wife.
Bosworth, sub voce.
[311] Supra, p. 259.
[312] Supra, p. 176.
[313] Supra, p. 199.
[314] Supra, p. 169.
[315] In the Bavarian and Saxon laws the litus was paid for at
one fourth the wergeld of the liber. The inference from this might
strengthen the view that the Kentish wergeld of the ceorl could
hardly be as low as 100 scillings.
[316] I adhere to this view after careful consideration of the
elaborate argument in the Die Gemeinfreien der Karolingischen
Volksrechte, von Philipp Heck (Halle, 1900), in reply to the
criticism by H. Brunner in the Savigny-Stiftung für
Rechtsgeschichte, xix Band, 1899.
[317] 1200 scillings of 4d. with one fourth added = 1200
scillings of 5d.
[318] Deutsche Rechtsgeschichte, i. 225-6.
[319] 60 + 40 Kentish scillings = 1200 + 800 scætts. The
average 1000 sceatts = 200 Wessex scillings of 5 scætts.
[320] Supra, p. 265.
[321] Supra, p. 367.
[322] Supra, p. 322; and Laws of Alfred, s. 27 and 38.
[323] Supra, pp. 415-416.
[324] This is not the place to enter into the details of the
Kentish holdings, but reference may be made by way of example
to the 5½ ‘sulings’ of ‘Christelet’ in the Black Book of St.
Augustine. The suling is still the unit for services and payments.
The ‘Suling de Fayreport’ contains 300 acres (and was probably
originally a suling and a half), but it is divided into 11 holdings, 8
of 25 acres each and 3 of 33⅓ acres each. Six of the eleven
holdings are still occupied by persons bearing the name of ‘de
Fayreport’ or the ‘heredes’ of such persons, and probably the
others may belong to relatives. The ‘Suling de Ores’ is, on the
other hand, divided into about 40 quite irregular holdings, varying
from less than an acre to 44 acres. Several are still occupied by
‘heredes’ of persons of the family ‘de Ores.’ (Cottonian MSS.
Faustina, A. 1, British Museum, fol. 567 et seq.) The manor ‘de
Ores’ is in the list of those afterwards disgavelled: see Elton’s
Tenures of Kent, p. 400.
[325] See Mr. Round’s interesting chapter, ‘Sokemen and their
Services.’ (Feudal England, pp. 28-34.)
[326] Domesday Book and beyond, p. 306 et seq.
[327] Ibid. pp. 204-209.
[328] Compare Brunner’s chapter 32, ‘Adel und Freie,’ in his
Deutsche Rechtsgeschichte, p. 247 et seq., with Das Römische
Recht in den Germanischen Volksstaaten, von Prof. Dr. Alfred von
Halban (Breslau, 1899), pp. 132, 207, 262, 280, and 294. And see
Dahn’s chapter ‘Der Adel,’ p. 88 et seq., in his Die Könige der
Germanen, Band vi. (Leipzig, 1885).
[329] Compare the tendency to triple divisions in the Kentish
Laws: supra, p. 465.
[330] Marculfe, ii. 18 and 16. Formulæ Lindenbrogianæ, 16.
And see F. de Coulanges’ useful chapter on ‘Organisation
judiciaire chez les Francs’ in Quelques problèmes d’histoire
(1885).
INDEX.

Aillts and Alltuds (strangers in blood) under Cymric law, 50, 51;
kindreds of, recognised at fourth generation, 52
Alamannic Laws, 172-178;
wergelds, 172-175;
value of animals, 178
Alcuin uses Roman currency, 19, 184
Alfred, K., his laws, 370-377, 392, 396;
compact with Guthrum, 352-355, 500
Alod of land, a family holding, 508;
Lex Salica ‘de alodis,’ 151;
Ripuarian law, 170;
Lex Angliorum et Werinorum, 226
Ancilla as currency, see ‘Cumhal’
Anglii and Werini, Laws of, 224-228;
Wergelds of liber 200 sol., 225;
triple wergeld of the Adaling, 225
Anglo-Saxon Custom, 321 et seq.;
from Norman point of view, 321-336;
from Danish point of view, 337-350;
from Viking or Northmen’s point of view, 351-368;
from early custom (Alfred’s Laws), 370-377;
Archbishop Egbert’s Dialogue, 377-385;
King Ine’s Dooms, 386-439;
Kentish Laws, 441-495;
Twelve-hynde and twy-hynde classes, 406-416;
Gesithcund and Ceorlisc classes, 417-436;
Six-hynde stranger class, 371, 392, 396;
position of wife, 326
Anglo-Saxon Wergelds, position of paternal and maternal
parentes in payment of, 322, 323, 328, 358;
of thane or twelve-hynde man, 325;
of ‘freeman,’ Dane and English, 326, 349, 353-55;
of ‘cyrlisci vel villani,’ 328;
of ‘villanus et socheman’ in Danelaga, 331-332;
of ‘ceorl on gafol-land’ and Danish ‘lysing,’ 353, 355;
how paid, 329, 357-59
Animals, value of as currency:
Ripuarian, 171;
Saxon, 215, 217, 221;
Alamannic, 178;
Cymric cow 3 oz., 48, 49;
Irish bo 1 oz., 97;
Frisian dog, 202
Argenteus (silver drachma) of Roman currency.
See ‘Currency’

Bavarian Laws, 175-177;


wergelds, 174
Beowulf, evidence of, as to feuds, 56-72;
as ‘sister’s son’ becomes chieftain, 68;
as to marriage, 71, 72
Borhbryce, fine for breach of pledge or protection, like
mundbryce, 347;
of various classes, 377
Bullock as currency in Saxon Laws, 217
Burgundian Laws, 121-125, 527;
original wergeld of 160 sol., 167;
Roman and Christian influence on, 527
Burh-bryce (Burg-bryce) (breach of fence of precinct), of various
classes, 372, 377, 387

Cæsar, evidence of, as to Gallic wergelds, 115-120;


and on Gallic landholding, 116;
as to division of classes, 528
Canones Hibernenses, 101
Canones Wallici, 105-109
Ceorl = man—husband, 482;
so man with household and flet or precinct, 371, 394, 482
—‘who sits on gafol-land’ twy-hynde, 353-355, 361;
ceorlisc class mostly gafolgeldas, and twy-hynde, 373;
once could rise to be twelve-hynde, 366, 503;
accused of theft, 388;
harbouring a fugitive, 390;
his mundbyrd in Kent, see ‘Mundbyrd’
Chamavi, laws of, 229-231;
wergeld of ingenuus 200 sol., 229;
triple wergeld of ‘Homo Francus,’ 229
Charlemagne, conquest of Italy, 181;
becomes emperor, 19, 181;
and issues nova moneta in silver solidi of 12d. and at 1:4
with gold, 182-194;
conquers Frisians and Saxons, 182, 195
Cnut. His greater Scandinavia, 339;
his ore of 1/15th lb. or 16d., 341;
his smaller silver pence, 343
Compurgation, under Frisian law, 203-205;
under Anglo-Saxon law, see ‘Hyndens’ and see ‘Werborh’
Congildones = gegildas, sureties in lieu of kinsmen, 323, 389,
415
Cows, as currency, 1.
In Cymric law, 49;
Irish, 97;
Alamannic, 178;
value of, see ‘Animals’;
Norse, 247-250;
Bretts and Scots, 307
Cumhal in Irish currency, 97-98 = female slave and ‘ancilla’ of
the Canones Hibernenses and Wallici, 101, 109
Currency, in oxen: ox-unit of Professor Ridgeway, 2;
in cows, Cymric, 1, 49;
Irish, 97;
Norse, 247-250;
Bretts and Scots, 307;
in cumhals, ancillæ or female slaves, 97-98, 101, 109;
in gold torques, &c., 17;
Anglo-Saxon in silver sceatts of 28·8 w.g. or 20 to the
Roman ounce, 12, 443-455;
in silver pence of 32 w.g. or 20 to the Frankish and Norman
ounce, 12;
gold and silver mancus of 30d., 18, 329;
Mercian scilling of 4d., 12, 363;
Wessex scilling of 5d., 12, 325;
Kentish scilling of 20d., or two gold tremisses, 443-455;
Northumbrian thrymsa of 3d., 362-368;
Danish in marks and half-marks, 16, 353-354;
Cnut’s in ores of 16d., 306, 341, 343 (see ‘Ore’);
Imperial in gold solidi and tremisses of 32 w.g., 5, 6;
in silver sicli (didrachmæ) and argentei (drachmæ), 184,
382;
Merovingian in gold solidi and tremisses of 28·8 w.g., 9;
afterwards in silver tremisses, 10, 180, 443-445;
Charlemagne’s nova moneta in silver solidi of 12d. of 32
w.g., 10, 11, 186, et seq.;
of Norse laws in gold and silver marks, ores and ortugs at
ratio 1:8, 233-238
Cymric tribal custom as to galanas, 30;
fiscal unit for food-rents, the tref and treffgordd, 33-42;
strangers, how treated, 50-54;
as to marriage, 32;
galanas of several classes paid in cows, 46-55.
See ‘Gwely,’ ‘Galanas’

Danelaga, 331-332, 338, 522

Ealdorman in judicial position, 387;


his burgbryce, 387;
his fightwite, 394;
his residence, 420
Egbert, Archbishop, Dialogue of, 377 et seq.;
uses Roman currency, 20, 379;
wergeld of his monks, 382, 491;
value of their oaths, 379
Eye, hand, and foot, payments for, 175, 222, 225, 252, 300,
465, 489

Fightwite, fine for fighting within a person’s precinct or


jurisdiction, 328-332, 359, 393;
in a ceorl’s flet, 371, 394, 482
Firma unius noctis, mode of paying food-rents to chieftain, 41,
431
Frankish Tribal Custom.
Wergeld of Lex Salica of 200 sol., 131-146;
division of classes, 147;
triple wergeld of officials, 148;
half wergeld of strangers, 149;
the Alod or family holding of terra Salica, 150;
the ‘de alodis,’ 151, 170, 226;
edict of Chilperic, 159;
Ripuarian Law, 163-171;
wergeld of 200 sol., 163;
division of classes, 165
Fredus, payment for breach of king’s peace, equivalent to A.S.
mundbryce or grithbryce, 488, 489
Freedman (libertus) under Frankish Law, 168-170, 199;
under Bavarian Law, 175;
under Kentish Law, 478, 484.
And see ‘Læt’ and ‘Leysing’
Frisian Laws, 194-212;
wergeld of 160 sol., 167, 195, 201, 210;
ordeal under, 203-5
Frith, between Ethelred II. and Olaf, 349;
between Alfred and Guthrum, 352-355
Frostathing Law (Ancient Norse), 238-276.
See ‘Norse Tribal Custom’

Gafolgeldas, tenants on others’ land paying gafol to their lord,


with twy-hynde wergelds, 353-355;
fighting in gafolgelda or gebur’s house, 394.
See ‘Twy-hynde’
Galanas (Cymric death fine or wergeld), 30;
liability of kindred for, 30-32;
method of payment, 42-46;
amount of, 46-49;
of non-tribesmen goes to the lord, 54
Gebur, tenant of a yardland doing work and paying gafol to lord
for house and oxen, 422-429;
fighting in house of gafolgelda or gebur, 394
Gegildas (see ‘Congildones’) sureties in lieu of kinsmen, 323,
389;
hyndens of frith-gegildas in the city, 415
Gesithcund class, in direct service to the king and twelve-hynde,
366;
in landed position with five hides to king’s utware, 369;
forfeit land if they neglect the fyrd, 391;
in their connection with land, 417 et seq.;
sometimes evicted, 433
Grith, Danish for frith or peace, 344-348;
grithbryce of English and mundbryce of Kentish law the
same, 346;
extent in area, 348;
in duration of time, 346;
of different moots, 345
Gulathing Law, oldest Norse law, 238-276.
See ‘Norse Tribal Custom’
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

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


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

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


personal growth every day!

ebookbell.com

You might also like