Matlab Prog
Matlab Prog
Matlab Prog
Programming Fundamentals
R2019b
How to Contact MathWorks
Phone: 508-647-7000
Language
Syntax Basics
1
Continue Long Statements on Multiple Lines . . . . . . . . . . 1-2
v
Program Components
2
MATLAB Operators and Special Characters . . . . . . . . . . . 2-2
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
String and Character Formatting . . . . . . . . . . . . . . . . . 2-20
vi Contents
Steps for Building Expressions . . . . . . . . . . . . . . . . . . . 2-66
Operators and Characters . . . . . . . . . . . . . . . . . . . . . . 2-69
vii
Classes (Data Types)
Numeric Classes
4
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Creating Integer Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
Arithmetic Operations on Integer Classes . . . . . . . . . . . . 4-4
Largest and Smallest Values for Integer Classes . . . . . . . 4-5
viii Contents
Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
ix
Convert from Numeric Values to Character Array . . . . . 6-56
Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-56
Convert Numbers to Character Codes . . . . . . . . . . . . . . 6-57
Represent Numbers as Text . . . . . . . . . . . . . . . . . . . . . 6-57
Convert to Specific Radix . . . . . . . . . . . . . . . . . . . . . . . 6-57
x Contents
Dates and Time
7
Represent Dates and Times in MATLAB . . . . . . . . . . . . . . 7-2
xi
Scatter Plot with Dates and Durations . . . . . . . . . . . . . 7-49
Plots that Support Dates and Durations . . . . . . . . . . . . 7-50
Categorical Arrays
8
Create Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
xii Contents
Advantages of Using Categorical Arrays . . . . . . . . . . . . . 8-43
Natural Representation of Categorical Data . . . . . . . . . 8-43
Mathematical Ordering for Character Vectors . . . . . . . . 8-43
Reduce Memory Requirements . . . . . . . . . . . . . . . . . . . 8-43
Tables
9
Create and Work with Tables . . . . . . . . . . . . . . . . . . . . . . . 9-2
xiii
Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . . . 9-74
Timetables
10
Create Timetables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
xiv Contents
Structures
11
Create Structure Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Cell Arrays
12
What Is a Cell Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
xv
Preallocate Memory for Cell Array . . . . . . . . . . . . . . . . . 12-18
Function Handles
13
Create Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Is a Function Handle? . . . . . . . . . . . . . . . . . . . . . 13-2
Creating Function Handles . . . . . . . . . . . . . . . . . . . . . . 13-2
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Arrays of Function Handles . . . . . . . . . . . . . . . . . . . . . 13-4
Saving and Loading Function Handles . . . . . . . . . . . . . 13-5
Map Containers
14
Overview of Map Data Structure . . . . . . . . . . . . . . . . . . . 14-2
xvi Contents
Read and Write Using Key Index . . . . . . . . . . . . . . . . . . 14-11
Read From Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-11
Add Key/Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . 14-12
Build Map with Concatenation . . . . . . . . . . . . . . . . . . 14-13
xvii
Using Objects
16
Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Handle Object Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Value Object Copy Behavior . . . . . . . . . . . . . . . . . . . . . 16-2
Handle Object Copy Behavior . . . . . . . . . . . . . . . . . . . . 16-3
Testing for Handle or Value Class . . . . . . . . . . . . . . . . . 16-6
Scripts
18
Create Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
xviii Contents
Add Functions to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Add Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Access Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-19
Run Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-19
Add and Run Sections in Live Scripts . . . . . . . . . . . . . 18-20
xix
Format Files in the Live Editor . . . . . . . . . . . . . . . . . . . . 19-39
Autoformatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-41
xx Contents
Create Interactive Course Materials Using the Live Editor
.......................................... 19-104
Function Basics
20
Create Functions in Files . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Syntax for Function Definition . . . . . . . . . . . . . . . . . . . 20-3
Contents of Functions and Files . . . . . . . . . . . . . . . . . . 20-4
End Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4
xxi
Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-24
What Are Anonymous Functions? . . . . . . . . . . . . . . . . 20-24
Variables in the Expression . . . . . . . . . . . . . . . . . . . . . 20-25
Multiple Anonymous Functions . . . . . . . . . . . . . . . . . . 20-26
Functions with No Inputs . . . . . . . . . . . . . . . . . . . . . . 20-27
Functions with Multiple Inputs or Outputs . . . . . . . . . 20-27
Arrays of Anonymous Functions . . . . . . . . . . . . . . . . . 20-28
xxii Contents
Anonymous functions can include resolved and unresolved
identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-52
Function Arguments
21
Find Number of Function Arguments . . . . . . . . . . . . . . . 21-2
xxiii
End Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . 22-7
xxiv Contents
Run Code During Publishing . . . . . . . . . . . . . . . . . . . . 23-29
Manipulate Graphics in Publishing Output . . . . . . . . . 23-31
Save a Publish Setting . . . . . . . . . . . . . . . . . . . . . . . . 23-36
Manage a Publish Configuration . . . . . . . . . . . . . . . . . 23-38
xxv
Go To Location in File . . . . . . . . . . . . . . . . . . . . . . . . . 24-33
Programming Utilities
25
Identify Program Dependencies . . . . . . . . . . . . . . . . . . . . 25-2
Simple Display of Program File Dependencies . . . . . . . 25-2
Detailed Display of Program File Dependencies . . . . . . 25-2
Dependencies Within a Folder . . . . . . . . . . . . . . . . . . . 25-3
xxvi Contents
Function Argument Validation
26
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . 26-2
Introduction to Argument Validation . . . . . . . . . . . . . . . 26-2
Where to Use Argument Validation . . . . . . . . . . . . . . . . 26-3
arguments Block Syntax . . . . . . . . . . . . . . . . . . . . . . . . 26-3
Examples of Argument Validation . . . . . . . . . . . . . . . . . 26-7
Kinds of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-9
Required and Optional Positional Arguments . . . . . . . . 26-9
Repeating Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 26-11
Name-Value Arguments . . . . . . . . . . . . . . . . . . . . . . . 26-14
Name-Value Arguments from Class Properties . . . . . . 26-17
Argument Validation in Class Methods . . . . . . . . . . . . 26-19
Order of Argument Validation . . . . . . . . . . . . . . . . . . . 26-20
Avoiding Class and Size Conversions . . . . . . . . . . . . . 26-21
nargin in Argument Validation . . . . . . . . . . . . . . . . . . 26-23
Restrictions on Variable and Function Access . . . . . . . 26-25
Software Development
Error Handling
27
Exception Handling in a MATLAB Application . . . . . . . . 27-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-2
xxvii
Getting an Exception at the Command Line . . . . . . . . . 27-2
Getting an Exception in Your Program Code . . . . . . . . . 27-3
Generating a New Exception . . . . . . . . . . . . . . . . . . . . 27-4
xxviii Contents
Use try/catch to Handle Errors . . . . . . . . . . . . . . . . . . . . 27-42
Program Scheduling
28
Schedule Command Execution Using Timer . . . . . . . . . . 28-2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-2
Example: Displaying a Message . . . . . . . . . . . . . . . . . . 28-3
Performance
29
Measure Performance of Your Program . . . . . . . . . . . . . 29-2
Overview of Performance Timing Functions . . . . . . . . . 29-2
Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2
Time Portions of Code . . . . . . . . . . . . . . . . . . . . . . . . . . 29-2
The cputime Function vs. tic/toc and timeit . . . . . . . . . . 29-3
Tips for Measuring Performance . . . . . . . . . . . . . . . . . . 29-3
xxix
Techniques to Improve Performance . . . . . . . . . . . . . . . 29-14
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Programming Practices for Performance . . . . . . . . . . . 29-14
Tips on Specific MATLAB Functions . . . . . . . . . . . . . . 29-15
Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-17
Preallocating a Nondouble Matrix . . . . . . . . . . . . . . . 29-17
Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-19
Using Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . 29-19
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-20
Logical Array Operations . . . . . . . . . . . . . . . . . . . . . . 29-22
Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-23
Ordering, Setting, and Counting Operations . . . . . . . . 29-24
Functions Commonly Used in Vectorization . . . . . . . . 29-26
Memory Usage
30
Strategies for Efficient Use of Memory . . . . . . . . . . . . . . 30-2
Use Appropriate Data Storage . . . . . . . . . . . . . . . . . . . 30-2
Avoid Temporary Copies of Data . . . . . . . . . . . . . . . . . . 30-4
Reclaim Used Memory . . . . . . . . . . . . . . . . . . . . . . . . . 30-5
xxx Contents
Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-25
xxxi
Projects
32
Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
What Are Projects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Create Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Open Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-3
Set up Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-3
Add Files to Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-6
Other Ways to Create Projects . . . . . . . . . . . . . . . . . . . 32-7
xxxii Contents
View, Edit, or Run Referenced Project Files . . . . . . . . 32-26
Extract Folder to Create a Referenced Project . . . . . . 32-26
Manage Changes in Referenced Project Using Checkpoints
....................................... 32-27
xxxiii
Disable Source Control . . . . . . . . . . . . . . . . . . . . . . . . . 33-6
xxxiv Contents
Register Binary Files with Git . . . . . . . . . . . . . . . . . . . 33-32
Add Git Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . 33-34
xxxv
Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . 33-63
Unit Testing
34
Write Test Using Live Script . . . . . . . . . . . . . . . . . . . . . . . 34-3
xxxvi Contents
Run the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-28
Analyze the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 34-28
xxxvii
Create Advanced Parameterized Test . . . . . . . . . . . . . . . 34-88
xxxviii Contents
Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . 34-162
xxxix
Qualify Mock Property Interaction . . . . . . . . . . . . . . 34-232
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . 34-233
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . 34-235
xl Contents
Reset Algorithm and Release Resources . . . . . . . . . . . . . . . . 35-30
Reset Algorithm State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-30
Release System Object Resources . . . . . . . . . . . . . . . . . . . . 35-30
xli
Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-62
setup Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-62
Running the Object or step Call Sequence . . . . . . . . . . . . . 35-63
reset Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-63
release Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-63
xlii Contents
Language
43
1
Syntax Basics
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:
x = [1.23...
4.56];
is the same as
x = [1.23 4.56];
1-2
Call Functions
Call Functions
These examples show how to call a MATLAB function. To run the examples, you must first
create numeric arrays A and B, such as:
A = [1 3 5];
B = [10 6 4];
max(A)
max(A,B)
maxA = max(A)
Call a function that does not require any inputs, and does not return any outputs, by
typing only the function name:
clc
disp('hello world')
See Also
Related Examples
• “Ignore Function Outputs” on page 1-4
1-3
1 Syntax Basics
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:
[~,~,ext] = fileparts(helpFile);
1-4
See Also
See Also
More About
• “Ignore Function Inputs” on page 21-13
1-5
1 Syntax Basics
Variable Names
In this section...
“Valid Names” on page 1-6
“Conflicts with Function Names” on page 1-6
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.
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.
1-6
See Also
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:
See Also
clear | exist | iskeyword | isvarname | namelengthmax | which
1-7
1 Syntax Basics
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]
1. UNIX is a registered trademark of The Open Group in the United States and other countries.
1-8
Case and Space Sensitivity
[1 sin(pi) 3]
ans =
1-9
1 Syntax Basics
If you do not require any outputs from the function, and all of the inputs are character
vectors (that is, text enclosed in single quotation marks), you can use this simpler
command syntax:
functionName input1 ... inputN
With command syntax, you separate inputs with spaces rather than commas, and do not
enclose input arguments in parentheses. Command syntax always passes inputs as
character vectors. To use strings as inputs, use the function syntax. If a character vector
contains a space, use the function syntax. For example:
When a function input is a variable, you must use function syntax to pass the value to the
function. Command syntax always passes inputs as character vectors and cannot pass
variable values. For example, create a variable and call the disp function with function
syntax to pass the value of the variable:
A = 123;
disp(A)
You cannot use command syntax to pass the value of A, because this call
disp A
is equivalent to
1-10
Command vs. Function Syntax
disp('A')
and returns
A
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
1-11
1 Syntax Basics
whos('-file','durer.mat','X')
ls ./d
This could be a call to the ls function with the folder ./d as its argument. It also could
request element-wise division on the array ls, using the variable d as the divisor.
If you issue such a statement at the command line, MATLAB can access the current
workspace and path to determine whether ls and d are functions or variables. However,
some components, such as the Code Analyzer and the Editor/Debugger, operate without
reference to the path or workspace. In those cases, MATLAB uses syntactic rules to
determine whether an expression is a function call using command syntax.
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, the potentially ambiguous statement ls ./d is a call to the ls function using
command syntax.
1-12
Command vs. Function Syntax
The best practice is to avoid defining variable names that conflict with common functions,
to prevent any ambiguity.
1-13
1 Syntax Basics
Certain uses of the eval and load functions can also result in a similar conflict between
variable and function names. For more information, see:
These errors usually indicate that MATLAB cannot find a particular variable or MATLAB
program file in the current directory or on the search path. The root cause is likely to be
one of the following:
1-14
Common Errors When Calling Functions
One of the most common errors is misspelling the function name. Especially with longer
function names or names containing similar characters (e.g., letter l and numeral one), it
is easy to make an error that is not easily detected.
If you misspell a MATLAB function, a suggested function name appears in the Command
Window. For example, this command fails because it includes an uppercase letter in the
function name:
accumArray
You establish the name for a function when you write its function definition line. This
name should always match the name of the file you save it to. For example, if you create a
function named curveplot,
then you should name the file containing that function curveplot.m. If you create a
pcode file for the function, then name that file curveplot.p. In the case of conflicting
function and file names, the file name overrides the name given to the function. In this
example, if you save the curveplot function to a file named curveplotfunction.m,
then attempts to invoke the function using the function name will fail:
curveplot
Undefined function or variable 'curveplot'.
1-15
1 Syntax Basics
If you encounter this problem, change either the function name or file name so that they
are the same. If you have difficulty locating the file that uses this function, use the
MATLAB Find Files utility as follows:
1
On the Home tab, in the File section, click Find Files.
2 Under Find files named: enter *.m
3 Under Find files containing text: enter the function name.
4 Click the Find button
If you are unable to use a built-in function from MATLAB or its toolboxes, make sure that
the function is installed.
If you do not know which toolbox supports the function you need, search for the function
documentation at https://www.mathworks.com/help. The toolbox name appears at
the top of the function reference page.
1-16
Common Errors When Calling Functions
Once you know which toolbox the function belongs to, use the ver function to see which
toolboxes are installed on the system from which you run MATLAB. The ver function
displays a list of all currently installed MathWorks® products. If you can locate the
toolbox you need in the output displayed by ver, then the toolbox is installed. For help
with installing MathWorks products, see the Installation Guide documentation.
If you do not see the toolbox and you believe that it is installed, then perhaps the MATLAB
path has been set incorrectly. Go on to the next section.
Because MATLAB stores the toolbox information in a cache file, you will need to first
update this cache and then reset the path. To do this,
1
On the Home tab, in the Environment section, click Preferences.
A small dialog box opens warning that you will lose your current path settings if you
proceed. Click Yes if you decide to proceed.
(If you have added any custom paths to MATLAB, you will need to restore those later)
Run ver again to see if the toolbox is installed. If not, you may need to reinstall this
toolbox to use this function. For more information about installing a toolbox, see How do I
install additional toolboxes into an existing installation of MATLAB.
Once ver shows your toolbox, run the following command to see if you can find the
function:
1-17
1 Syntax Basics
replacing <functionname> with the name of the function. You should be presented with
the path(s) of the function file. If you get a message indicating that the function name was
not found, you may need to reinstall that toolbox to make the function active.
1-18
2
Program Components
Arithmetic Operators
Symbol Role More Information
+ Addition plus
+ Unary plus uplus
- Subtraction minus
- Unary minus uminus
.* Element-wise multiplication times
* Matrix multiplication mtimes
./ Element-wise right division rdivide
/ Matrix right division mrdivide
.\ Element-wise left division ldivide
\ Matrix left division mldivide
Relational Operators
Symbol Role More Information
== Equal to eq
~= Not equal to ne
> Greater than gt
2-2
MATLAB Operators and Special Characters
Logical Operators
Symbol Role More Information
& Logical AND and
| Logical OR or
&& Logical AND (with short- Logical Operators:
circuiting) Short-Circuit && ||
|| Logical OR (with short-
circuiting)
~ Logical NOT not
2-3
2 Program Components
Special Characters
@ Name: At symbol
Uses:
Description: The @ symbol forms a handle to either the named function that
follows the @ sign, or to the anonymous function that follows the @ sign. You
can also use @ to call superclass methods from subclasses.
Examples
fhandle = @myfun
disp@MySuper(obj)
Call the superclass constructor from a subclass using the object being
constructed:
obj = obj@MySuper(arg1,arg2,...)
More Information:
2-4
MATLAB Operators and Special Characters
Uses:
• Decimal point
• Element-wise operations
• Structure field access
• Object property or method specifier
Description: The period character separates the integral and fractional parts
of a number, such as 3.1415. MATLAB operators that contain a period always
work element-wise. The period character also enables you to access the fields
in a structure, as well as the properties and methods of an object.
Examples
Decimal point:
102.5543
Element-wise operations:
A.*B
A.^2
myStruct.f1
myObj.PropertyName
More Information
2-5
2 Program Components
Description: Three or more periods at the end of a line continues the current
command on the next line. If three or more periods occur before the end of a
line, then MATLAB ignores the rest of the line and continues to the next line.
This effectively makes a comment out of anything on the current line that
follows the three periods.
Examples
To comment out one line in a multiline command, use ... at the beginning of
the line to ensure that the command remains complete. If you use % to
comment out a line it produces an error:
y = 1 +...
2 +...
% 3 +...
4;
However, this code runs properly since the third line does not produce a gap in
the command:
y = 1 +...
2 +...
2-6
MATLAB Operators and Special Characters
... 3 +...
4;
More Information
Uses: Separator
Examples
A = [12,13; 14,15]
Separate subscripts:
A(1,2)
[Y,I] = max(A,[],2)
More Information
• horzcat
2-7
2 Program Components
: Name: Colon
Uses:
• Vector creation
• Indexing
• For-loop iteration
Description: Use the colon operator to create regularly spaced vectors, index
into arrays, and define the bounds of a for loop.
Examples
Create a vector:
x = 1:10
x = 1:3:19
A(:)
A = rand(3,4);
A(:) = 1:12;
A(2:5,3)
A(:,3)
x = 1;
for k = 1:25
x = x + x^2;
end
2-8
MATLAB Operators and Special Characters
More Information
• colon
• “Creating, Concatenating, and Expanding Matrices”
; Name: Semicolon
Uses:
Examples
A = [12,13; 14,15]
Y = max(A);
More Information
• vertcat
2-9
2 Program Components
( ) Name: Parentheses
Uses:
• Operator precedence
• Function argument enclosure
• Indexing
Examples
Precedence of operations:
(A.*(B./C)) - D
plot(X,Y,'r*')
C = union(A,B)
Indexing:
A(3,:)
A(1,2)
A(1:5,1)
More Information
2-10
MATLAB Operators and Special Characters
Uses:
• Array construction
• Array concatenation
• Empty matrix and array element deletion
• Multiple output argument assignment
Examples
X = [10 12 -3]
A = rand(3);
A = [A; 10 20 30]
A = []
A(:,1) = []
[C,iA,iB] = union(A,B)
More Information
2-11
2 Program Components
Examples
To construct a cell array, enclose all elements of the array in curly braces:
Index to a specific cell array element by enclosing all indices in curly braces:
A = C{4,7,2}
More Information
• “Cell Arrays”
2-12
MATLAB Operators and Special Characters
% Name: Percent
Uses:
• Comment
• Conversion specifier
Two percent signs, %%, serve as a cell delimiter as described in “Code Sections”
on page 18-7.
Examples
More Information
2-13
2 Program Components
Examples
%{
The purpose of this routine is to compute
the value of ...
%}
More Information
2-14
MATLAB Operators and Special Characters
Examples
!rmdir oldtests
More Information
Examples
?inputParser
More Information
• metaclass
2-15
2 Program Components
Description: Use single quotes to create character vectors that have class
char.
Examples
More Information
Description: Use double quotes to create string scalars that have class
string.
Examples
S = "Hello, world"
More Information
2-16
MATLAB Operators and Special Characters
Uses: Separator
Examples
Uses: Separator
Examples
2-17
2 Program Components
~ Name: Tilde
Uses:
• Logical NOT
• Argument placeholder
Examples
A = eye(3);
~A
A = [1 -1; 0 1]
B = [1 -2; 3 2]
A~=B
[~,~,iB] = union(A,B)
More Information
• not
• “Ignore Function Inputs” on page 21-13
• “Ignore Function Outputs” on page 1-4
2-18
MATLAB Operators and Special Characters
Uses: Assignment
Description: Use the equal sign to assign values to a variable. The syntax B =
A stores the elements of A in variable B.
Examples
A = [1 0; -1 0];
B = A;
B(1) = 200;
< & Name: Left angle bracket and ampersand
Examples
More Information:
• “Subclass Syntax”
2-19
2 Program Components
Description:
When using function argument validation, you can define the fields of the
name-value structure as the names of all writeable properties of the class.
Examples
Specify the field names of the propArgs structure as the writeable properties
of the matlab.graphics.primitive.Line class.
function f(propArgs)
arguments
propArgs.?matlab.graphics.primitive.Line
end
% Function code
...
end
More Information:
Use the special characters in this table to specify a folder path using a character vector or
string.
2-20
MATLAB Operators and Special Characters
Examples
dir([matlabroot '\toolbox\matlab\elmat\shiftdim.m'])
dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])
dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])
.. Name: Dot dot
Description: Two dots in succession refers to the parent of the current folder.
Use this character to specify folder paths relative to the current folder.
Examples
To go up two levels in the folder tree and down into the test folder, use:
cd ..\..\test
More Information
• cd
2-21
2 Program Components
* Name: Asterisk
Wildcards are generally used in file operations that act on multiple files or
folders. MATLAB matches all characters in the name exactly except for the
wildcard character *, which can match any one or more characters.
Examples
Locate all files with names that start with january_ and have a .mat file
extension:
dir('january_*.mat')
@ Name: At symbol
Examples
\@myClass\get.m
More Information
2-22
MATLAB Operators and Special Characters
+ Name: Plus
Examples
+mypack
+mypack/pkfcn.m % a package function
+mypack/@myClass % class folder in a package
More Information
There are certain special characters that you cannot enter as ordinary text. Instead, you
must use unique character sequences to represent them. Use the symbols in this table to
format strings and character vectors on their own or in conjunction with formatting
functions like compose, sprintf, and error. For more information, see “Formatting
Text” on page 6-31.
2-23
2 Program Components
See Also
More About
• “Array vs. Matrix Operations” on page 2-25
• “Array Comparison with Relational Operators” on page 2-37
• “Compatible Array Sizes for Basic Operations” on page 2-31
• “Operator Precedence” on page 2-41
• “Find Array Elements That Meet a Condition” on page 5-2
• “Greek Letters and Special Characters in Chart Text”
2-24
Array vs. Matrix Operations
Introduction
MATLAB has two different types of arithmetic operations: array operations and matrix
operations. You can use these arithmetic operations to perform numeric computations, for
example, adding two numbers, raising the elements of an array to a given power, or
multiplying two matrices.
Matrix operations follow the rules of linear algebra. By contrast, array operations execute
element by element operations and support multidimensional arrays. The period
character (.) distinguishes the array operations from the matrix operations. However,
since the matrix and array operations are the same for addition and subtraction, the
character pairs .+ and .- are unnecessary.
Array Operations
Array operations execute element by element operations on corresponding elements of
vectors, matrices, and multidimensional arrays. If the operands have the same size, then
each element in the first operand gets matched up with the element in the same location
in the second operand. If the operands have compatible sizes, then each input is implicitly
expanded as needed to match the size of the other. For more information, see
“Compatible Array Sizes for Basic Operations” on page 2-31.
As a simple example, you can add two vectors with the same size.
A = [1 1 1]
A =
1 1 1
B = [1 2 3]
2-25
2 Program Components
B =
1 2 3
A+B
ans =
2 3 4
If one operand is a scalar and the other is not, then MATLAB implicitly expands the scalar
to be the same size as the other operand. For example, you can compute the element-wise
product of a scalar and a matrix.
A = [1 2 3; 1 2 3]
A =
1 2 3
1 2 3
3.*A
ans =
3 6 9
3 6 9
Implicit expansion also works if you subtract a 1-by-3 vector from a 3-by-3 matrix because
the two sizes are compatible. When you perform the subtraction, the vector is implicitly
expanded to become a 3-by-3 matrix.
A = [1 1 1; 2 2 2; 3 3 3]
A =
1 1 1
2 2 2
3 3 3
m = [2 4 6]
m =
2 4 6
2-26
Array vs. Matrix Operations
A - m
ans =
-1 -3 -5
0 -2 -4
1 -1 -3
A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-
by-1 vector, then each vector implicitly expands into a 2-by-3 matrix before MATLAB
executes the element-wise addition.
x = [1 2 3]
x =
1 2 3
y = [10; 15]
y =
10
15
x + y
ans =
11 12 13
16 17 18
If the sizes of the two operands are incompatible, then you get an error.
A = [8 1 6; 3 5 7; 4 9 2]
A =
8 1 6
3 5 7
4 9 2
m = [2 4]
m =
2 4
2-27
2 Program Components
A - m
The following table provides a summary of arithmetic array operators in MATLAB. For
function-specific information, click the link to the function reference page in the last
column.
Matrix Operations
Matrix operations follow the rules of linear algebra and are not compatible with
multidimensional arrays. The required size and shape of the inputs in relation to one
another depends on the operation. For nonscalar inputs, the matrix operators generally
calculate different answers than their array operator counterparts.
For example, if you use the matrix right division operator, /, to divide two matrices, the
matrices must have the same number of columns. But if you use the matrix multiplication
operator, *, to multiply two matrices, then the matrices must have a common inner
dimension. That is, the number of columns in the first input must be equal to the number
2-28
Array vs. Matrix Operations
of rows in the second input. The matrix multiplication operator calculates the product of
two matrices with the formula,
n
C(i, j) = ∑ A(i, k)B(k, j) .
k=1
A = [1 3;2 4]
A =
1 3
2 4
B = [3 0;1 5]
B =
3 0
1 5
A*B
ans =
6 15
10 20
The previous matrix product is not equal to the following element-wise product.
A.*B
ans =
3 0
2 20
The following table provides a summary of matrix arithmetic operators in MATLAB. For
function-specific information, click the link to the function reference page in the last
column.
2-29
2 Program Components
See Also
More About
• “Compatible Array Sizes for Basic Operations” on page 2-31
• “MATLAB Operators and Special Characters” on page 2-2
• “Operator Precedence” on page 2-41
2-30
Compatible Array Sizes for Basic Operations
These are some combinations of scalars, vectors, and matrices that have compatible sizes:
• One input is a matrix, and the other is a column vector with the same number of rows.
2-31
2 Program Components
Multidimensional Arrays
Every array in MATLAB has trailing dimensions of size 1. For multidimensional arrays,
this means that a 3-by-4 matrix is the same as a matrix of size 3-by-4-by-1-by-1-by-1.
Examples of multidimensional arrays with compatible sizes are:
• One input is a matrix, and the other is a 3-D array with the same number of rows and
columns.
2-32
Compatible Array Sizes for Basic Operations
• One input is a matrix, and the other is a 3-D array. The dimensions are all either the
same or one of them is 1.
2-33
2 Program Components
Empty Arrays
The rules are the same for empty arrays or arrays that have a dimension size of zero. The
size of the dimension that is not equal to 1 determines the size of the output. This means
that dimensions with a size of zero must be paired with a dimension of size 1 or 0 in the
other array, and that the output has a dimension size of 0.
A: 1-by-0
B: 3-by-1
Result: 3-by-0
2-34
Compatible Array Sizes for Basic Operations
A: 3-by-2
B: 4-by-2
• Two nonscalar row vectors with lengths that are not the same.
A: 1-by-3
B: 1-by-4
Examples
Subtract Vector from Matrix
For example, calculate the mean value of each column in a matrix, then subtract the mean
value from each element.
A = magic(3)
A =
8 1 6
3 5 7
4 9 2
C = mean(A)
C =
5 5 5
A - C
ans =
3 -4 1
-2 0 2
-1 4 -3
Row and column vectors have compatible sizes, and when you perform an operation on
them the result is a matrix.
2-35
2 Program Components
For example, add a row and column vector. The result is the same as
bsxfun(@plus,a,b).
a = [1 2 3 4]
ans =
1 2 3 4
b = [5; 6; 7]
ans =
5
6
7
a + b
ans =
6 7 8 9
7 8 9 10
8 9 10 11
See Also
bsxfun
More About
• “Array vs. Matrix Operations” on page 2-25
• “MATLAB Operators and Special Characters” on page 2-2
2-36
Array Comparison with Relational Operators
Relational operators compare operands quantitatively, using operators like “less than”,
“greater than”, and “not equal to.” The result of a relational comparison is a logical array
indicating the locations where the relation is true.
Array Comparison
Numeric Arrays
The relational operators perform element-wise comparisons between two arrays. The
arrays must have compatible sizes to facilitate the operation. Arrays with compatible sizes
are implicitly expanded to be the same size during execution of the calculation. In the
simplest cases, the two operands are arrays of the same size, or one is a scalar. For more
information, see “Compatible Array Sizes for Basic Operations” on page 2-31.
For example, if you compare two matrices of the same size, then the result is a logical
matrix of the same size with elements indicating where the relation is true.
A = [2 4 6; 8 10 12]
A =
2-37
2 Program Components
2 4 6
8 10 12
B = [5 5 5; 9 9 9]
B =
5 5 5
9 9 9
A < B
ans =
1 1 0
1 0 0
ans =
0 0 0
1 1 1
If you compare a 1-by-N row vector to an M-by-1 column vector, then MATLAB expands
each vector into an M-by-N matrix before performing the comparison. The resulting matrix
contains the comparison result for each combination of elements in the vectors.
A = 1:3
A =
1 2 3
B = [2; 3]
B =
2
3
A >= B
ans =
2-38
Array Comparison with Relational Operators
0 1 1
0 0 1
Empty Arrays
The relational operators work with arrays for which any dimension has size zero, as long
as both arrays have compatible sizes. This means that if one array has a dimension size of
zero, then the size of the corresponding dimension in the other array must be 1 or zero,
and the size of that dimension in the output is zero.
A = ones(3,0);
B = ones(3,1);
A == B
ans =
A == []
return an error if A is not 0-by-0 or 1-by-1. This behavior is consistent with that of all
other binary operators, such as +, -, >, <, &, |, and so on.
Complex Numbers
• The operators >, <, >=, and <= use only the real part of the operands in performing
comparisons.
• The operators == and ~= test both real and imaginary parts of the operands.
2-39
2 Program Components
Logic Statements
Use relational operators in conjunction with the logical operators A & B (AND), A | B
(OR), xor(A,B) (XOR), and ~A (NOT), to string together more complex logical
statements.
For example, you can locate where negative elements occur in two arrays.
A = [2 -1; -3 10]
A =
2 -1
-3 10
B = [0 -2; -3 -1]
B =
0 -2
-3 -1
ans =
0 1
1 0
For more examples, see “Find Array Elements That Meet a Condition” on page 5-2.
See Also
eq | ge | gt | le | lt | ne
More About
• “Array vs. Matrix Operations” on page 2-25
• “Compatible Array Sizes for Basic Operations” on page 2-31
• “MATLAB Operators and Special Characters” on page 2-2
2-40
Operator Precedence
Operator Precedence
You can build expressions that use any combination of arithmetic, relational, and logical
operators. Precedence levels determine the order in which MATLAB evaluates an
expression. Within each precedence level, operators have equal precedence and are
evaluated from left to right. The precedence rules for MATLAB operators are shown in
this list, ordered from highest precedence level to lowest precedence level:
1 Parentheses ()
2 Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
3 Power with unary minus (.^-), unary plus (.^+), or logical negation (.^~) as well
as matrix power with unary minus (^-), unary plus (^+), or logical negation (^~).
Note Although most operators work from left to right, the operators (^-), (.^-),
(^+), (.^+), (^~), and (.^~) work from second from the right to left. It is
recommended that you use parentheses to explicitly specify the intended precedence
of statements containing these operator combinations.
4 Unary plus (+), unary minus (-), logical negation (~)
5 Multiplication (.*), right division (./), left division (.\), matrix multiplication
(*), matrix right division (/), matrix left division (\)
6 Addition (+), subtraction (-)
7 Colon operator (:)
8 Less than (<), less than or equal to (<=), greater than (>), greater than or equal to
(>=), equal to (==), not equal to (~=)
9 Element-wise AND (&)
10 Element-wise OR (|)
11 Short-circuit AND (&&)
12 Short-circuit OR (||)
2-41
2 Program Components
The same precedence rule holds true for the && and || operators.
A = [3 9 5];
B = [2 1 5];
C = A./B.^2
C =
0.7500 9.0000 0.2000
C = (A./B).^2
C =
2.2500 81.0000 1.0000
See Also
More About
• “Array vs. Matrix Operations” on page 2-25
• “Compatible Array Sizes for Basic Operations” on page 2-31
• “Array Comparison with Relational Operators” on page 2-37
• “MATLAB Operators and Special Characters” on page 2-2
2-42
Average Similar Data Points Using a Tolerance
Use random points picked from the peaks function in the domain [ − 3, 3] × [ − 3, 3] as
the data set. Add a small amount of noise to the data.
xy = rand(10000,2)*6-3;
z = peaks(xy(:,1),xy(:,2)) + 0.5-rand(10000,1);
A = [xy z];
plot3(A(:,1), A(:,2), A(:,3), '.')
view(-28,32)
2-43
2 Program Components
Find points that have similar x and y coordinates using uniquetol with these options:
• Specify ByRows as true, since the rows of A contain the point coordinates.
• Specify OutputAllIndices as true to return the indices for all points that are
within tolerance of each other.
• Specify DataScale as [1 1 Inf] to use an absolute tolerance for the x and y
coordinates, while ignoring the z-coordinate.
DS = [1 1 Inf];
[C,ia] = uniquetol(A, 0.3, 'ByRows', true, ...
'OutputAllIndices', true, 'DataScale', DS);
Average each group of points that are within tolerance (including the z-coordinates),
producing a reduced data set that still holds the general shape of the original data.
for k = 1:length(ia)
aveA(k,:) = mean(A(ia{k},:),1);
end
hold on
plot3(aveA(:,1), aveA(:,2), aveA(:,3), '.r', 'MarkerSize', 15)
2-44
See Also
See Also
uniquetol
More About
• “Group Scattered Data Using a Tolerance” on page 2-46
2-45
2 Program Components
Create a set of random 2-D points. Then create and plot a grid of equally spaced points on
top of the random data.
x = rand(10000,2);
[a,b] = meshgrid(0:0.1:1);
gridPoints = [a(:), b(:)];
plot(x(:,1), x(:,2), '.')
hold on
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)
2-46
Group Scattered Data Using a Tolerance
Use ismembertol to locate the data points in x that are within tolerance of the grid
points in gridPoints. Use these options with ismembertol:
• Specify ByRows as true, since the point coordinates are in the rows of x.
• Specify OutputAllIndices as true to return all of the indices for rows in x that are
within tolerance of the corresponding row in gridPoints.
[LIA,LocB] = ismembertol(gridPoints, x, 0.05, ...
'ByRows', true, 'OutputAllIndices', true);
For each grid point, plot the points in x that are within tolerance of that grid point.
figure
hold on
2-47
2 Program Components
for k = 1:length(LocB)
plot(x(LocB{k},1), x(LocB{k},2), '.')
end
plot(gridPoints(:,1), gridPoints(:,2), 'xr', 'Markersize', 6)
See Also
ismembertol
More About
• “Average Similar Data Points Using a Tolerance” on page 2-43
2-48
Bit-Wise Operations
Bit-Wise Operations
This topic shows how to use bit-wise operations in MATLAB® to manipulate the bits of
numbers. Operating on bits is directly supported by most modern CPUs. In many cases,
manipulating the bits of a number in this way is quicker than performing arithmetic
operations like division or multiplication.
Number Representations
Any number can be represented with bits (also known as binary digits). The binary, or
base 2, form of a number contains 1s and 0s to indicate which powers of 2 are present in
the number. For example, the 8-bit binary form of 7 is
00000111
A collection of 8 bits is also called 1 byte. In binary representations, the bits are counted
from the right to the left, so the first bit in this representation is a 1. This number
represents 7 because
2 1 0
2 + 2 + 2 = 7.
When you type numbers into MATLAB, it assumes the numbers are double precision (a
64-bit binary representation). However, you can also specify single-precision numbers
(32-bit binary representation) and integers (signed or unsigned, from 8 to 64 bits). For
example, the most memory efficient way to store the number 7 is with an 8-bit unsigned
integer:
a = uint8(7)
a = uint8
7
You can even specify the binary form directly using the prefix 0b followed by the binary
digits (for more information, see “Hexadecimal and Binary Values” on page 6-61).
MATLAB stores the number in an integer format with the fewest number of bits. Instead
of specifying all the bits, you need to specify only the left-most 1 and all the digits to the
right of it. The bits to the left of that bit are trivially zero. So the number 7 is:
b = 0b111
b = uint8
7
2-49
2 Program Components
MATLAB stores negative integers using two's complement. For example, consider the 8-
bit signed integer -8. To find the two's complement bit pattern for this number:
1 Start with the bit pattern of the positive version of the number, 8: 00001000.
2 Next, flip all of the bits: 11110111.
3 Finally, add 1 to the result: 11111000.
n = 0b11111000s8
n = int8
-8
MATLAB does not natively display the binary format of numbers. For that, you can use the
dec2bin function, which returns a character vector of binary digits for positive integers.
Again, this function returns only the digits that are not trivially zero.
dec2bin(b)
ans =
'111'
You can use bin2dec to switch between the two formats. For example, you can convert
the binary digits 10110101 to decimal format with the commands
data = [1 0 1 1 0 1 0 1];
dec = bin2dec(num2str(data))
dec = 181
The cast and typecast functions are also useful to switch among different data types.
These functions are similar, but they differ in how they treat the underlying storage of the
number:
Because MATLAB does not display the digits of a binary number directly, you must pay
attention to data types when you work with bit-wise operations. Some functions return
binary digits as a character vector (dec2bin), some return the decimal number
(bitand), and others return a vector of the bits themselves (bitget).
2-50
Bit-Wise Operations
MATLAB has several functions that enable you to perform logical operations on the bits of
two equal-length binary representations of numbers, known as bit masking:
• bitand — If both digits are 1, then the resulting digit is also a 1. Otherwise, the
resulting digit is 0.
• bitor — If either digit is 1, then the resulting digit is also a 1. Otherwise, the
resulting digit is 0.
• bitxor — If the digits are different, then the resulting digit is a 1. Otherwise, the
resulting digit is 0.
In addition to these functions, the bit-wise complement is available with bitcmp, but this
is a unary operation that flips the bits in only one number at a time.
One use of bit masking is to query the status of a particular bit. For example, if you use a
bit-wise AND operation with the binary number 00001000, you can query the status of
the fourth bit. You can then shift that bit to the first position so that MATLAB returns a 0
or 1 (the next section describes bit shifting in more detail).
n = 0b10111001;
n4 = bitand(n,0b1000);
n4 = bitshift(n4,-3)
n4 = uint8
1
Bit-wise operations can have surprising applications. For example, consider the 8-bit
binary representation of the number n = 8:
00001000
00000111
By subtracting 1, all of the bits starting at the right-most 1 are flipped. As a result, when
n is a power of 2, corresponding digits of n and n − 1 are always different, and the bit-
wise AND returns zero.
n = 0b1000;
bitand(n,n-1)
2-51
2 Program Components
ans = uint8
0
0
However, when n is not a power of 2, then the right-most 1 is for the 2 bit, so n and
0
n − 1 have all the same bits except for the 2 bit. For this case, the bit-wise AND returns
a nonzero number.
n = 0b101;
bitand(n,n-1)
ans = uint8
4
This operation suggests a simple function that operates on the bits of a given input
number to check whether the number is a power of 2:
function tf = isPowerOfTwo(n)
tf = n && ~bitand(n,n-1);
end
The use of the short-circuit AND operator && checks to make sure that n is not zero. If it
is, then the function does not need to calculate bitand(n,n-1) to know that the correct
answer is false.
Shifting Bits
These operations are sometimes written A<<N (left shift) and A>>N (right shift), but
MATLAB does not use << and >> operators for this purpose.
When the bits of a number are shifted, some bits fall off the end of the number, and 0s or
1s are introduced to fill in the newly created space. When you shift bits to the left, the
bits are filled in on the right; when you shift bits to the right, the bits are filled in on the
left.
2-52
Bit-Wise Operations
For example, if you shift the bits of the number 8 (binary: 1000) to the right by one digit,
you get 4 (binary: 100).
n = 0b1000;
bitshift(n,-1)
ans = uint8
4
Similarly, if you shift the number 15 (binary: 1111) to the left by two digits, you get 60
(binary: 111100).
n = 0b1111;
bitshift(15,2)
ans = 60
When you shift the bits of a negative number, bitshift preserves the signed bit. For
example, if you shift the signed integer -1 (binary: 11111101) to the right by 2 digits, you
get -1 (binary: 11111111). In these cases, bitshift fills in on the left with 1s rather
than 0s.
n = 0b11111101s8;
bitshift(n,-2)
ans = int8
-1
Writing Bits
You can use the bitset function to change the bits in a number. For example, change the
first bit of the number 8 to a 1 (which adds 1 to the number):
bitset(8,1)
ans = 9
By default, bitset flips bits to on or 1. You can optionally use the third input argument to
specify the bit value.
bitset does not change multiple bits at once, so you need to use a for loop to change
multiple bits. Therefore, the bits you change can be either consecutive or nonconsecutive.
For example, change the first two bits of the binary number 1000:
bits = [1 2];
c = 0b1000;
2-53
2 Program Components
for k = 1:numel(bits)
c = bitset(c,bits(k));
end
dec2bin(c)
ans =
'1011'
Another common use of bitset is to convert a vector of binary digits into decimal
format. For example, use a loop to set the individual bits of the integer 11001101.
data = [1 1 0 0 1 1 0 1];
n = length(data);
dec = 0b0u8;
for k = 1:n
dec = bitset(dec,n+1-k,data(k));
end
dec
dec = uint8
205
dec2bin(dec)
ans =
'11001101'
Another use of bit shifting is to isolate consecutive sections of bits. For example, read the
last four bits in the 16-bit number 0110000010100000. Recall that the last four bits are
on the left of the binary representation.
n = 0b0110000010100000;
dec2bin(bitshift(n,-12))
ans =
'110'
To isolate consecutive bits in the middle of the number, you can combine the use of bit
shifting with logical masking. For example, to extract the 13th and 14th bits, you can shift
the bits to the right by 12 and then mask the resulting four bits with 0011. Because the
inputs to bitand must be the same integer data type, you can specify 0011 as an
unsigned 16-bit integer with 0b11u16. Without the -u16 suffix, MATLAB stores the
number as an unsigned 8-bit integer.
2-54
See Also
m = 0b11u16;
dec2bin(bitand(bitshift(n,-12),m))
ans =
'10'
Another way to read consecutive bits is with bitget, which reads specified bits from a
number. You can use colon notation to specify several consecutive bits to read. For
example, read the last 8 bits of n.
bitget(n,16:-1:8)
0 1 1 0 0 0 0 0 1
You can also use bitget to read bits from a number when the bits are not next to each
other. For example, read the 5th, 8th, and 14th bits from n.
1 1 0
See Also
bitand | bitcmp | bitget | bitor | bitset | bitshift | bitxor
More About
• “Integers” on page 4-2
• “Perform Cyclic Redundancy Check” on page 2-56
• “Hexadecimal and Binary Values” on page 6-61
2-55
2 Program Components
1101100111011010
To obtain the check value, divide this number by the polynomial x3 + x2 + x + 1. You can
represent this polynomial with its coefficients: 1111.
The division is performed in steps, and after each step the polynomial divisor is aligned
with the left-most 1 in the number. Because the result of dividing by the four term
polynomial has three bits (in general dividing by a polynomial of length n + 1 produces a
check value of length n), append the number with 000 to calculate the remainder. At each
step, the result uses the bit-wise XOR of the four bits being operated on, and all other bits
are unchanged.
1101100111011010 000
1111
----------------
0010100111011010 000
Each successive division operates on the result of the previous step, so the second
division is
0010100111011010 000
1111
----------------
0001010111011010 000
The division is completed once the dividend is all zeros. The complete division, including
the above two steps, is
2-56
Perform Cyclic Redundancy Check
1101100111011010 000
1111
0010100111011010 000
1111
0001010111011010 000
1111
0000101111011010 000
1111
0000010011011010 000
1111
0000001101011010 000
1111
0000000010011010 000
1111
0000000001101010 000
1111
0000000000010010 000
1111
0000000000001100 000
1111
0000000000000011 000
11 11
0000000000000000 110
The remainder bits, 110, are the check value for this message.
In MATLAB®, you can perform this same operation to obtain the check value using bit-
wise operations. First, define variables for the message and polynomial divisor. Use
unsigned 32-bit integers so that extra bits are available for the remainder.
message = 0b1101100111011010u32;
messageLength = 16;
divisor = 0b1111u32;
divisorDegree = 3;
Next, initialize the polynomial divisor. Use dec2bin to display the bits of the result.
divisor = bitshift(divisor,messageLength-divisorDegree-1);
dec2bin(divisor)
ans =
'1111000000000000'
2-57
2 Program Components
Now, shift the divisor and message so that they have the correct number of bits (16 bits
for the message and 3 bits for the remainder).
divisor = bitshift(divisor,divisorDegree);
remainder = bitshift(message,divisorDegree);
dec2bin(divisor)
ans =
'1111000000000000000'
dec2bin(remainder)
ans =
'1101100111011010000'
Perform the division steps of the CRC using a for loop. The for loop always advances a
single bit each step, so include a check to see if the current digit is a 1. If the current
digit is a 1, then the division step is performed; otherwise, the loop advances a bit and
continues.
for k = 1:messageLength
if bitget(remainder,messageLength+divisorDegree)
remainder = bitxor(remainder,divisor);
end
remainder = bitshift(remainder,1);
end
Shift the bits of the remainder to the right to get the check value for the operation.
CRC_check_value = bitshift(remainder,-messageLength);
dec2bin(CRC_check_value)
ans =
'110'
You can use the check value to verify the integrity of a message by repeating the same
division operation. However, instead of using a remainder of 000 to start, use the check
value 110. If the message is error free, then the result of the division will be zero.
Reset the remainder variable, and add the CRC check value to the remainder bits using a
bit-wise OR. Introduce an error into the message by flipping one of the bit values with
bitset.
2-58
See Also
remainder = bitshift(message,divisorDegree);
remainder = bitor(remainder,CRC_check_value);
remainder = bitset(remainder,6);
dec2bin(remainder)
ans =
'1101100111011110110'
Perform the CRC division operation and then check if the result is zero.
for k = 1:messageLength
if bitget(remainder,messageLength+divisorDegree)
remainder = bitxor(remainder,divisor);
end
remainder = bitshift(remainder,1);
end
if remainder == 0
disp('Message is error free.')
else
disp('Message contains errors.')
end
References
[1] Sklar, Bernard. Digital Communications: Fundamentals and Applications. Englewood
Cliffs, NJ: Prentice Hall, 1988.
[2] Wicker, Stephen B. Error Control Systems for Digital Communication and Storage.
Upper Saddle River, NJ: Prentice Hall, 1995.
See Also
bitshift | bitxor
More About
• “Bit-Wise Operations” on page 2-49
• “Hexadecimal and Binary Values” on page 6-61
2-59
2 Program Components
Conditional Statements
Conditional statements enable you to select at run time which block of code to execute.
The simplest conditional statement is an if statement. For example:
% If it is even, divide by 2
if rem(a, 2) == 0
disp('a is even')
b = a/2;
end
if statements can include alternate choices, using the optional keywords elseif or
else. For example:
a = randi(100, 1);
if a < 30
disp('small')
elseif a < 80
disp('medium')
else
disp('large')
end
Alternatively, when you want to test for equality against a set of known values, use a
switch statement. For example:
switch dayString
case 'Monday'
disp('Start of the work week')
case 'Tuesday'
disp('Day 2')
case 'Wednesday'
disp('Day 3')
case 'Thursday'
disp('Day 4')
case 'Friday'
disp('Last day of the work week')
otherwise
2-60
See Also
disp('Weekend!')
end
For both if and switch, MATLAB executes the code corresponding to the first true
condition, and then exits the code block. Each conditional statement requires the end
keyword.
In general, when you have many possible discrete, known values, switch statements are
easier to read than if statements. However, you cannot test for inequality between
switch and case values. For example, you cannot implement this type of condition with
a switch:
if yourNumber < 0
disp('Negative')
elseif yourNumber > 0
disp('Positive')
else
disp('Zero')
end
See Also
end | if | return | switch
2-61
2 Program Components
• for statements loop a specific number of times, and keep track of each iteration with
an incrementing index variable.
x = ones(1,10);
for n = 2:6
x(n) = 2 * x(n - 1);
end
• while statements loop as long as a condition remains true.
For example, find the first integer n for which factorial(n) is a 100-digit number:
n = 1;
nFactorial = 1;
while nFactorial < 1e100
n = n + 1;
nFactorial = nFactorial * n;
end
It is a good idea to indent the loops for readability, especially when they are nested (that
is, when one loop contains another loop):
A = zeros(5,100);
for m = 1:5
for n = 1:100
A(m, n) = 1/(m + n - 1);
end
end
You can programmatically exit a loop using a break statement, or skip to the next
iteration of a loop using a continue statement. For example, count the number of lines in
the help for the magic function (that is, all comment lines until a blank line):
fid = fopen('magic.m','r');
count = 0;
2-62
See Also
while ~feof(fid)
line = fgetl(fid);
if isempty(line)
break
elseif ~strncmp(line,'%',1)
continue
end
count = count + 1;
end
fprintf('%d lines in MAGIC help\n',count);
fclose(fid);
Tip If you inadvertently create an infinite loop (a loop that never ends on its own), stop
execution of the loop by pressing Ctrl+C.
See Also
break | continue | end | for | while
2-63
2 Program Components
Regular Expressions
In this section...
“What Is a Regular Expression?” on page 2-64
“Steps for Building Expressions” on page 2-66
“Operators and Characters” on page 2-69
Regular expressions provide a unique way to search a volume of text for a particular
subset of characters within that text. Instead of looking for an exact character match as
you would do with a function like strfind, regular expressions give you the ability to
look for a particular pattern of characters.
km/h
km/hr
km/hour
kilometers/hour
kilometers per hour
You could locate any of the above terms in your text by issuing five separate search
commands:
strfind(text, 'km/h');
strfind(text, 'km/hour');
% etc.
2-64
Regular Expressions
To be more efficient, however, you can build a single phrase that applies to all of these
search terms:
Translate this phrase into a regular expression (to be explained later in this section) and
you have:
pattern = 'k(ilo)?m(eters)?(/|\sper\s)h(r|our)?';
Now locate one or more of the terms using just a single command:
ans =
There are four MATLAB functions that support searching and replacing characters using
regular expressions. The first three are similar in the input values they accept and the
output values they return. For details, click the links to the function reference pages.
Function Description
regexp Match regular expression.
regexpi Match regular expression, ignoring case.
regexprep Replace part of text using regular expression.
regexptranslate Translate text into regular expression.
2-65
2 Program Components
When calling any of the first three functions, pass the text to be parsed and the regular
expression in the first two input arguments. When calling regexprep, pass an additional
input that is an expression that specifies a pattern for the replacement.
This entails breaking up the text you want to search for into groups of like character
types. These character types could be a series of lowercase letters, a dollar sign
followed by three numbers and then a decimal point, etc.
2 Express each pattern as a regular expression on page 2-67
Pass the text you want to parse to one of the search functions, such as regexp or
regexpi, or to the text replacement function, regexprep.
The example shown in this section searches a record containing contact information
belonging to a group of five friends. This information includes each person's name,
telephone number, place of residence, and email address. The goal is to extract specific
information from the text..
contacts = { ...
'Harry 287-625-7315 Columbus, OH hparker@hmail.com'; ...
'Janice 529-882-1759 Fresno, CA jan_stephens@horizon.net'; ...
'Mike 793-136-0975 Richmond, VA sue_and_mike@hmail.net'; ...
'Nadine 648-427-9947 Tampa, FL nadine_berry@horizon.net'; ...
'Jason 697-336-7728 Montrose, CO jason_blake@mymail.com'};
The first part of the example builds a regular expression that represents the format of a
standard email address. Using that expression, the example then searches the
information for the email address of one of the group of friends. Contact information for
Janice is in row 2 of the contacts cell array:
contacts{2}
2-66
Regular Expressions
ans =
A typical email address is made up of standard components: the user's account name,
followed by an @ sign, the name of the user's internet service provider (ISP), a dot
(period), and the domain to which the ISP belongs. The table below lists these
components in the left column, and generalizes the format of each component in the right
column.
In this step, you translate the general formats derived in Step 1 into segments of a
regular expression. You then add these segments together to form the entire expression.
The table below shows the generalized format descriptions of each character pattern in
the left-most column. (This was carried forward from the right column of the table in Step
1.) The second column shows the operators or metacharacters that represent the
character pattern.
2-67
2 Program Components
Assembling these patterns into one character vector gives you the complete expression:
email = '[a-z_]+@[a-z]+\.(com|net)';
In this step, you use the regular expression derived in Step 2 to match an email address
for one of the friends in the group. Use the regexp function to perform the search.
Here is the list of contact information shown earlier in this section. Each person's record
occupies a row of the contacts cell array:
contacts = { ...
'Harry 287-625-7315 Columbus, OH hparker@hmail.com'; ...
'Janice 529-882-1759 Fresno, CA jan_stephens@horizon.net'; ...
'Mike 793-136-0975 Richmond, VA sue_and_mike@hmail.net'; ...
'Nadine 648-427-9947 Tampa, FL nadine_berry@horizon.net'; ...
'Jason 697-336-7728 Montrose, CO jason_blake@mymail.com'};
This is the regular expression that represents an email address, as derived in Step 2:
email = '[a-z_]+@[a-z]+\.(com|net)';
Call the regexp function, passing row 2 of the contacts cell array and the email
regular expression. This returns the email address for Janice.
ans =
{'jan_stephens@horizon.net'}
MATLAB parses a character vector from left to right, “consuming” the vector as it goes. If
matching characters are found, regexp records the location and resumes parsing the
character vector, starting just after the end of the most recent match.
Make the same call, but this time for the fifth person in the list:
2-68
Regular Expressions
ans =
{'jason_blake@mymail.com'}
You can also search for the email address of everyone in the list by using the entire cell
array for the input argument:
Metacharacters
Metacharacters represent letters, letter ranges, digits, and space characters. Use them to
construct a generalized pattern of characters.
2-69
2 Program Components
2-70
Regular Expressions
Character Representation
Operator Description
\a Alarm (beep)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\char Any character with special meaning in regular expressions that you want to match
literally (for example, use \\ to match a single backslash)
Quantifiers
Quantifiers specify the number of times a pattern must occur in the matching text.
{0,1} is equivalent to ?.
2-71
2 Program Components
Equivalent to {n,n}.
Quantifiers can appear in three modes, described in the following table. q represents any
of the quantifiers in the previous table.
'<tr><td><p>text</p></td>'
exprq? Lazy expression: match as few Given the
characters as necessary. text'<tr><td><p>text</p></td>',
the expression '</?t.*?>' ends each
match at the first occurrence of the
closing angle bracket (>):
Grouping Operators
Grouping operators allow you to capture tokens, apply one operator to multiple elements,
or disable backtracking in a specific group.
2-72
Regular Expressions
Anchors
Anchors in the expression match the beginning or end of a character vector or word.
2-73
2 Program Components
Lookaround Assertions
Lookaround assertions look for patterns that immediately precede or follow the intended
match, but are not part of the match.
The pointer remains at the current location, and characters that correspond to the test
expression are not captured or discarded. Therefore, lookahead assertions can match
overlapping character groups.
2-74
Regular Expressions
For more information, see “Lookahead Assertions in Regular Expressions” on page 2-80.
Logical and conditional operators allow you to test the state of a given condition, and then
use the outcome to determine which pattern, if any, to match next. These operators
support logical OR and if or if/else conditions. (For AND conditions, see “Lookaround
Assertions” on page 2-74.)
Conditions can be tokens on page 2-75, lookaround assertions on page 2-74, or dynamic
expressions on page 2-77 of the form (?@cmd). Dynamic expressions must return a
logical or numeric value.
Token Operators
Tokens are portions of the matched text that you define by enclosing part of the regular
expression in parentheses. You can refer to a token by its sequence in the text (an ordinal
token), or assign names to tokens for easier code maintenance and readable output.
2-75
2 Program Components
Note If an expression has nested parentheses, MATLAB captures tokens that correspond
to the outermost set of parentheses. For example, given the search pattern '(and(y|
rew))', MATLAB creates a token for 'andrew' but not for 'y' or 'rew'.
2-76
Regular Expressions
Dynamic Expressions
The parentheses that enclose dynamic expressions do not create a capturing group.
Within dynamic expressions, use the following operators to define replacement terms.
2-77
2 Program Components
Comments
The comment operator enables you to insert comments into your code to make it more
maintainable. The text of the comment is ignored by MATLAB when matching against the
input text.
Search Flags
Flag Description
(?-i) Match letter case (default for regexp and regexprep).
(?i) Do not match letter case (default for regexpi).
(?s) Match dot (.) in the pattern with any character (default).
(?-s) Match dot in the pattern with any character that is not a newline character.
(?-m) Match the ^ and $ metacharacters at the beginning and end of text
(default).
(?m) Match the ^ and $ metacharacters at the beginning and end of a line.
(?-x) Include space characters and comments when matching (default).
(?x) Ignore space characters and comments when matching. Use '\ ' and
'\#' to match space and # characters.
The expression that the flag modifies can appear either after the parentheses, such as
(?i)\w*
or inside the parentheses and separated from the flag with a colon (:), such as
(?i:\w*)
The latter syntax allows you to change the behavior for part of a larger expression.
2-78
See Also
See Also
regexp | regexpi | regexprep | regexptranslate
More About
• “Lookahead Assertions in Regular Expressions” on page 2-80
• “Tokens in Regular Expressions” on page 2-84
• “Dynamic Regular Expressions” on page 2-92
2-79
2 Program Components
Lookahead Assertions
There are two types of lookaround assertions for regular expressions: lookahead and
lookbehind. In both cases, the assertion is a condition that must be satisfied to return a
match to the expression.
A lookahead assertion has the form (?=test) and can appear anywhere in a regular
expression. MATLAB looks ahead of the current location in the text for the test condition.
If MATLAB matches the test condition, it continues processing the rest of the expression
to find a match.
For example, look ahead in a character vector specifying a path to find the name of the
folder that contains a program file (in this case, fileread.m).
chr = which('fileread')
chr =
'matlabroot\toolbox\matlab\iofun\fileread.m'
regexp(chr,'\w+(?=\\\w+\.[mp])','match')
ans =
{'iofun'}
The match expression, \w+, searches for one or more alphanumeric or underscore
characters. Each time regexp finds a term that matches this condition, it looks ahead for
a backslash (specified with two backslashes, \\), followed by a file name (\w+) with an .m
or .p extension (\.[mp]). The regexp function returns the match that satisfies the
lookahead condition, which is the folder name iofun.
2-80
Lookahead Assertions in Regular Expressions
Overlapping Matches
Lookahead assertions do not consume any characters in the text. As a result, you can use
them to find overlapping character sequences.
For example, use lookahead to find every sequence of six nonwhitespace characters in a
character vector by matching initial characters that precede five additional characters:
startIndex =
1 8 9 16 17 24 25
Without the lookahead operator, MATLAB parses a character vector from left to right,
consuming the vector as it goes. If matching characters are found, regexp records the
location and resumes parsing the character vector from the location of the most recent
match. There is no overlapping of characters in this process.
startIndex =
1 8 16 24
helptext = help('normest');
chr = helptext(1:50)
2-81
2 Program Components
chr =
Merely searching for non-vowels ([^aeiou]) does not return the expected answer, as the
output includes capital letters, space characters, and punctuation:
c = regexp(chr,'[^aeiou]','match')
c =
Columns 1 through 14
{' '} {'N'} {'O'} {'R'} {'M'} {'E'} {'S'} {'T'} {' '} {'
Columns 15 through 28
{' '} {'t'} {'h'} {' '} {'m'} {'t'} {'r'} {'x'} {' '} {'
Columns 29 through 42
{'.'} {'↵'} {' '} {' '} {' '} {' '} {'N'} {'O'} {'R'} {'
Column 43
{'S'}
Try this again, using a lookahead operator to create the following AND condition:
(lowercase letter) AND (not a vowel)
c =
{'s'} {'t'} {'m'} {'t'} {'t'} {'h'} {'m'} {'t'} {'r'} {'
Note that when using a lookahead operator to perform an AND, you need to place the
match expression expr after the test expression test:
2-82
See Also
(?=test)expr or (?!test)expr
See Also
regexp | regexpi | regexprep
More About
• “Regular Expressions” on page 2-64
2-83
2 Program Components
Introduction
Parentheses used in a regular expression not only group elements of that expression
together, but also designate any matches found for that group as tokens. You can use
tokens to match other parts of the same text. One advantage of using tokens is that they
remember what they matched, so you can recall and reuse matched text in the process of
searching or replacing.
Each token in the expression is assigned a number, starting from 1, going from left to
right. To make a reference to a token later in the expression, refer to it using a backslash
followed by the token number. For example, when referencing a token generated by the
third set of parentheses in the expression, use \3.
As a simple example, if you wanted to search for identical sequential letters in a character
array, you could capture the first letter as a token and then search for a matching
character immediately afterwards. In the expression shown below, the (\S) phrase
creates a token whenever regexp matches any nonwhitespace character in the character
array. The second part of the expression, '\1', looks for a second instance of the same
character immediately following the first.
mat =
2-84
Tokens in Regular Expressions
The cell array tok contains cell arrays that each contain a token.
tok{:}
ans =
{'d'}
ans =
{'p'}
ans =
{'d'}
ans =
{'p'}
The cell array ext contains numeric arrays that each contain starting and ending indices
for a token.
ext{:}
ans =
11 11
ans =
2-85
2 Program Components
26 26
ans =
35 35
ans =
57 57
For another example, capture pairs of matching HTML tags (e.g., <a> and </a>) and the
text between them. The expression used for this example is
expr = '<(\w+).*?>.*?</\1>';
The first part of the expression, '<(\w+)', matches an opening angle bracket (<)
followed by one or more alphabetic, numeric, or underscore characters. The enclosing
parentheses capture token characters following the opening angle bracket.
The second part of the expression, '.*?>.*?', matches the remainder of this HTML tag
(characters up to the >), and any characters that may precede the next opening angle
bracket.
The last part, '</\1>', matches all characters in the ending HTML tag. This tag is
composed of the sequence </tag>, where tag is whatever characters were captured as a
token.
ans =
'<a name="752507"></a>'
ans =
'<b>Default</b>'
2-86
Tokens in Regular Expressions
tok{:}
ans =
{'a'}
ans =
{'b'}
Multiple Tokens
Here is an example of how tokens are assigned values. Suppose that you are going to
search the following text:
You choose to search the above text with the following search pattern:
and(y|rew)|(t)e(d)
This pattern has three parenthetical expressions that generate tokens. When you finally
perform the search, the following tokens are generated for each match.
Only the highest level parentheses are used. For example, if the search pattern and(y|
rew) finds the text andrew, token 1 is assigned the value rew. However, if the search
pattern (and(y|rew)) is used, token 1 is assigned the value andrew.
2-87
2 Program Components
Unmatched Tokens
For those tokens specified in the regular expression that have no match in the text being
evaluated, regexp and regexpi return an empty character vector ('') as the token
output, and an extent that marks the position in the string where the token was expected.
The example shown here executes regexp on a character vector specifying the path
returned from the MATLAB tempdir function. The regular expression expr includes six
token specifiers, one for each piece of the path. The third specifier [a-z]+ has no match
in the character vector because this part of the path, Profiles, begins with an
uppercase letter:
chr = tempdir
chr =
'C:\WINNT\Profiles\bpascal\LOCALS~1\Temp\'
When a token is not found in the text, regexp returns an empty character vector ('') as
the token and a numeric array with the token extent. The first number of the extent is the
string index that marks where the token was expected, and the second number of the
extent is equal to one less than the first.
In the case of this example, the empty token is the third specified in the expression, so the
third token returned is empty:
tok{:}
ans =
The third token extent returned in the variable ext has the starting index set to 10, which
is where the nonmatching term, Profiles, begins in the path. The ending extent index is
set to one less than the starting index, or 9:
ext{:}
2-88
Tokens in Regular Expressions
ans =
1 2
4 8
10 9
19 25
27 34
36 39
ans =
Named Capture
If you use a lot of tokens in your expressions, it may be helpful to assign them names
rather than having to keep track of which token number is assigned to which token.
When referencing a named token within the expression, use the syntax \k<name> instead
of the numeric \1, \2, etc.:
poe = ['While I nodded, nearly napping, ' ...
'suddenly there came a tapping,'];
ans =
Named tokens can also be useful in labeling the output from the MATLAB regular
expression functions. This is especially true when you are processing many pieces of text.
2-89
2 Program Components
For example, parse different parts of street addresses from several character vectors. A
short name is assigned to each token in the expression:
p1 = '(?<adrs>\d+\s\S+\s(Road|Street|Avenue|Drive))';
p2 = '(?<city>[A-Z][a-z]+)';
p3 = '(?<state>[A-Z]{2})';
p4 = '(?<zip>\d{5})';
As the following results demonstrate, you can make your output easier to work with by
using named tokens:
loc1 =
loc2 =
loc3 =
2-90
See Also
See Also
regexp | regexpi | regexprep
More About
• “Regular Expressions” on page 2-64
2-91
2 Program Components
Introduction
In a dynamic expression, you can make the pattern that you want regexp to match
dependent on the content of the input text. In this way, you can more closely match
varying input patterns in the text being parsed. You can also use dynamic expressions in
replacement terms for use with the regexprep function. This gives you the ability to
adapt the replacement text to the parsed input.
replace_expr1 = '$118$3';
regexprep('internationalization', match_expr, replace_expr1)
ans =
'i18n'
replace_expr2 = '$111$3';
regexprep('globalization', match_expr, replace_expr2)
2-92
Dynamic Regular Expressions
ans =
'g11n'
ans =
'i18n'
ans =
'g11n'
There are three forms of dynamic expressions that you can use in match expressions, and
one form for replacement expressions, as described in the following sections
Here is an example of the type of expression that you can use with this operator:
chr = {'5XXXXX', '8XXXXXXXX', '1X'};
regexp(chr, '^(\d+)(??X{$1})$', 'match', 'once');
The purpose of this particular command is to locate a series of X characters in each of the
character vectors stored in the input cell array. Note however that the number of Xs
2-93
2 Program Components
varies in each character vector. If the count did not vary, you could use the expression
X{n} to indicate that you want to match n of these characters. But, a constant value of n
does not work in this case.
The solution used here is to capture the leading count number (e.g., the 5 in the first
character vector of the cell array) in a token, and then to use that count in a dynamic
expression. The dynamic expression in this example is (??X{$1}), where $1 is the value
captured by the token \d+. The operator {$1} makes a quantifier of that token value.
Because the expression is dynamic, the same pattern works on all three of the input
vectors in the cell array. With the first input character vector, regexp looks for five X
characters; with the second, it looks for eight, and with the third, it looks for just one:
ans =
First, create the input string. Make sure that all letters are lowercase, and remove all
nonword characters.
chr = lower(...
'Find the palindrome Never Odd or Even in this string');
chr =
'findthepalindromeneveroddoreveninthisstring'
Locate the palindrome within the character vector using the dynamic expression:
2-94
Dynamic Regular Expressions
palindrome =
{'neveroddoreven'}
The dynamic expression reverses the order of the letters that make up the character
vector, and then attempts to match as much of the reversed-order vector as possible. This
requires a dynamic expression because the value for $1 relies on the value of the token
(.{3,}).
Dynamic expressions in MATLAB have access to the currently active workspace. This
means that you can change any of the functions or variables used in a dynamic expression
just by changing variables in the workspace. Repeat the last command of the example
above, but this time define the function to be called within the expression using a function
handle stored in the base workspace:
fun = @fliplr;
palindrome =
{'neveroddoreven'}
The following example parses a word for zero or more characters followed by two
identical characters followed again by zero or more characters:
2-95
2 Program Components
ans =
{'mississippi'}
To track the exact steps that MATLAB takes in determining the match, the example
inserts a short script (?@disp($1)) in the expression to display the characters that
finally constitute the match. Because the example uses greedy quantifiers, MATLAB
attempts to match as much of the character vector as possible. So, even though MATLAB
finds a match toward the beginning of the string, it continues to look for more matches
until it arrives at the very end of the string. From there, it backs up through the letters i
then p and the next p, stopping at that point because the match is finally satisfied:
regexp('mississippi', '\w*(\w)(?@disp($1))\1\w*', 'match')
i
p
p
ans =
{'mississippi'}
Now try the same example again, this time making the first quantifier lazy (*?). Again,
MATLAB makes the same match:
regexp('mississippi', '\w*?(\w)\1\w*', 'match')
ans =
{'mississippi'}
But by inserting a dynamic script, you can see that this time, MATLAB has matched the
text quite differently. In this case, MATLAB uses the very first match it can find, and does
not even consider the rest of the text:
regexp('mississippi', '\w*?(\w)(?@disp($1))\1\w*', 'match')
m
i
2-96
Dynamic Regular Expressions
ans =
{'mississippi'}
To demonstrate how versatile this type of dynamic expression can be, consider the next
example that progressively assembles a cell array as MATLAB iteratively parses the input
text. The (?!) operator found at the end of the expression is actually an empty lookahead
operator, and forces a failure at each iteration. This forced failure is necessary if you want
to trace the steps that MATLAB is taking to resolve the expression.
MATLAB makes a number of passes through the input text, each time trying another
combination of letters to see if a fit better than last match can be found. On any passes in
which no matches are found, the test results in an empty character vector. The dynamic
script (?@if(~isempty($&))) serves to omit the empty character vectors from the
matches cell array:
matches = {};
expr = ['(Euler\s)?(Cauchy\s)?(Boole)?(?@if(~isempty($&)),' ...
'matches{end+1}=$&;end)(?!)'];
matches
matches =
{'Euler Cauchy Bo…'} {'Euler Cauchy '} {'Euler '} {'Cauchy Boole'} {'Ca
The operators $& (or the equivalent $0), $`, and $' refer to that part of the input text
that is currently a match, all characters that precede the current match, and all
characters to follow the current match, respectively. These operators are sometimes
useful when working with dynamic expressions, particularly those that employ the (?
@cmd) operator.
This example parses the input text looking for the letter g. At each iteration through the
text, regexp compares the current character with g, and not finding it, advances to the
next character. The example tracks the progress of scan through the text by marking the
current location being parsed with a ^ character.
2-97
2 Program Components
(The $` and $´ operators capture that part of the text that precedes and follows the
current parsing location. You need two single-quotation marks ($'') to express the
sequence $´ when it appears within text.)
chr = 'abcdefghij';
expr = '(?@disp(sprintf(''starting match: [%s^%s]'',$`,$'')))g';
The dynamic expression tells MATLAB to execute a function named convertMe using the
two tokens (\d+\.?\d*) and (\w+), derived from the text being matched, as input
arguments in the call to convertMe. The replacement pattern requires a dynamic
expression because the values of $1 and $2 are generated at runtime.
The following example defines the file named convertMe that converts measurements
from imperial units to metric.
2-98
Dynamic Regular Expressions
uout = 'centimeters';
case 'miles'
fun = @(mi)mi .* 1.6093;
uout = 'kilometers';
case 'pounds'
fun = @(lb)lb .* 0.4536;
uout = 'kilograms';
case 'pints'
fun = @(pt)pt .* 0.4731;
uout = 'litres';
case 'ounces'
fun = @(oz)oz .* 28.35;
uout = 'grams';
end
val = fun(str2num(valin));
valout = [num2str(val) ' ' uout];
end
At the command line, call the convertMe function from regexprep, passing in values for
the quantity to be converted and name of the imperial unit:
ans =
ans =
ans =
As with the (??@ ) operator discussed in an earlier section, the ${ } operator has
access to variables in the currently active workspace. The following regexprep
command uses the array A defined in the base workspace:
2-99
2 Program Components
A = magic(3)
A =
8 1 6
3 5 7
4 9 2
ans =
See Also
regexp | regexpi | regexprep
More About
• “Regular Expressions” on page 2-64
2-100
Comma-Separated Lists
Comma-Separated Lists
In this section...
“What Is a Comma-Separated List?” on page 2-101
“Generating a Comma-Separated List” on page 2-101
“Assigning Output from a Comma-Separated List” on page 2-103
“Assigning to a Comma-Separated List” on page 2-104
“How to Use the Comma-Separated Lists” on page 2-106
“Fast Fourier Transform Example” on page 2-108
ans =
ans =
ans =
Such a list, by itself, is not very useful. But when used with large and more complex data
structures like MATLAB structures and cell arrays, the comma-separated list can enable
you to simplify your MATLAB code.
2-101
2 Program Components
Extracting multiple elements from a cell array yields a comma-separated list. Given a 4-
by-6 cell array as shown here
C = cell(4,6);
for k = 1:24
C{k} = k*2;
end
C
C =
C{:,5}
ans =
34
ans =
36
ans =
38
ans =
40
C{1,5},C{2,5},C{3,5},C{4,5}
2-102
Comma-Separated Lists
For structures, extracting a field of the structure that exists across one of its dimensions
yields a comma-separated list.
Start by converting the cell array used above into a 4-by-1 MATLAB structure with six
fields: f1 through f6. Read field f5 for all rows and MATLAB returns a comma-separated
list:
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
S.f5
ans =
34
ans =
36
ans =
38
ans =
40
2-103
2 Program Components
end
[c1,c2,c3,c4,c5,c6] = C{1,1:6};
c5
c5 =
34
If you specify fewer output variables than the number of outputs returned by the
expression, MATLAB assigns the first N outputs to those N variables, and then discards
any remaining outputs. In this next example, MATLAB assigns C{1,1:3} to the variables
c1, c2, and c3, and then discards C{1,4:6}:
[c1,c2,c3] = C{1,1:6};
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
[sf1,sf2,sf3] = S.f5;
sf3
sf3 =
38
You also can use the deal function for this purpose.
This example uses deal to overwrite each element in a comma-separated list. First create
a list.
ans =
31 7
2-104
Comma-Separated Lists
ans =
3 78
ans =
10 20
ans =
14 12
This example does the same as the one above, but with a comma-separated list of vectors
in a structure field:
ans =
31 7
ans =
3 78
ans =
10 20
2-105
2 Program Components
ans =
14 12
The following sections provide examples of using comma-separated lists with cell arrays.
Each of these examples applies to MATLAB structures as well.
Constructing Arrays
You can use a comma-separated list to enter a series of elements when constructing a
matrix or array. Note what happens when you insert a list of elements as opposed to
adding the cell itself.
When you specify a list of elements with C{:, 5}, MATLAB inserts the four individual
elements:
A = {'Hello',C{:,5},magic(4)}
A =
When you specify the C cell itself, MATLAB inserts the entire cell array:
A = {'Hello',C,magic(4)}
A =
2-106
Comma-Separated Lists
Displaying Arrays
A{:}
ans =
Hello
ans =
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Concatenation
Putting a comma-separated list inside square brackets extracts the specified elements
from the list and concatenates them:
A = [C{:,5:6}]
A =
34 36 38 40 42 44 46 48
When writing the code for a function call, you enter the input arguments as a list with
each argument separated by a comma. If you have these arguments stored in a structure
or cell array, then you can generate all or part of the argument list from the structure or
cell array instead. This can be especially useful when passing in variable numbers of
arguments.
2-107
2 Program Components
X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));
C = cell(2,3);
C{1,1} = 'LineWidth';
C{2,1} = 2;
C{1,2} = 'MarkerEdgeColor';
C{2,2} = 'k';
C{1,3} = 'MarkerFaceColor';
C{2,3} = 'g';
figure
plot(X,Y,'--rs',C{:})
MATLAB functions can also return more than one value to the caller. These values are
returned in a list with each value separated by a comma. Instead of listing each return
value, you can use a comma-separated list with a structure or cell array. This becomes
more useful for those functions that have variable numbers of return values.
C = cell(1,3);
[C{:}] = fileparts('work/mytests/strArrays.mat')
C =
fftshift uses vectors of indices to perform the swap. For the vector shown above, the
index [1 2 3 4 5 6] is rearranged to form a new index [4 5 6 1 2 3]. The function
then uses this index vector to reposition the elements. For a multidimensional array,
fftshift must construct an index vector for each dimension. A comma-separated list
makes this task much simpler.
2-108
Comma-Separated Lists
function y = fftshift(x)
numDims = ndims(x);
idx = cell(1,numDims);
for k = 1:numDims
m = size(x,k);
p = ceil(m/2);
idx{k} = [p+1:m 1:p];
end
y = x(idx{:});
end
The function stores the index vectors in cell array idx. Building this cell array is
relatively simple. For each of the N dimensions, determine the size of that dimension and
find the integer index nearest the midpoint. Then, construct a vector that swaps the two
halves of that dimension.
By using a cell array to store the index vectors and a comma-separated list for the
indexing operation, fftshift shifts arrays of any dimension using just a single
operation: y = x(idx{:}). If you were to use explicit indexing, you would need to write
one if statement for each dimension you want the function to handle:
if ndims(x) == 1
y = x(index1);
else if ndims(x) == 2
y = x(index1,index2);
end
end
Another way to handle this without a comma-separated list would be to loop over each
dimension, converting one dimension at a time and moving data each time. With a
comma-separated list, you move the data just once. A comma-separated list makes it very
easy to generalize the swapping operation to an arbitrary number of dimensions.
2-109
2 Program Components
• MATLAB compiles code the first time you run it to enhance performance for future
runs. However, because code in an eval statement can change at run time, it is not
compiled.
• Code within an eval statement can unexpectedly create or assign to a variable
already in the current workspace, overwriting existing data.
• Concatenated character vectors within an eval statement are often difficult to read.
Other language constructs can simplify the syntax in your code.
For many common uses of eval, there are preferred alternate approaches, as shown in
the following examples.
For example, create a cell array that contains 10 elements, where each element is a
numeric array:
2-110
Alternatives to the eval Function
numArrays = 10;
A = cell(numArrays,1);
for n = 1:numArrays
A{n} = magic(n);
end
Access the data in the cell array by indexing with curly braces. For example, display the
fifth element of A:
A{5}
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The assignment statement A{n} = magic(n) is more elegant and efficient than this call
to eval:
The best practice is to use function syntax, which allows you to pass variables as inputs.
For example:
currentFile = 'myfile1.mat';
save(currentFile)
2-111
2 Program Components
You can construct file names within a loop using the sprintf function (which is usually
more efficient than int2str), and then call the save function without eval. This code
creates 10 files in the current folder:
numFiles = 10;
for n = 1:numFiles
randomData = rand(n);
currentFile = sprintf('myfile%d.mat',n);
save(currentFile,'randomData')
end
• Create function handles with the @ symbol or with the str2func function. For
example, run a function from a list stored in a cell array:
examples = {@odedemo,@sunspots,@fitdemo};
n = input('Select an example (1, 2, or 3): ');
examples{n}()
• Use the feval function. For example, call a plot function (such as plot, bar, or pie)
with data that you specify at run time:
plotFunction = input('Specify a plotting function: ','s');
data = input('Enter data to plot: ');
feval(plotFunction,data)
2-112
Alternatives to the eval Function
If you enter weight at the input prompt, then you can find the minimum weight value
with the following command.
min(dataToUse)
ans =
90
For an additional example, see “Generate Field Names from Variables” on page 11-15.
Error Handling
The preferred method for error handling in MATLAB is to use a try, catch statement.
For example:
try
B = A;
catch exception
disp('A is undefined')
end
If your workspace does not contain variable A, then this code returns:
A is undefined
Previous versions of the documentation for the eval function include the syntax
eval(expression,catch_expr). If evaluating the expression input returns an error,
then eval evaluates catch_expr. However, an explicit try/catch is significantly
clearer than an implicit catch in an eval statement. Using the implicit catch is not
recommended.
2-113
Classes (Data Types)
115
3
There are 16 fundamental classes in MATLAB. Each of these classes is in the form of a
matrix or array. With the exception of function handles, this matrix or array is a minimum
of 0-by-0 in size and can grow to an n-dimensional array of any size. A function handle is
always scalar (1-by-1).
All of the fundamental MATLAB classes are shown in the diagram below:
Numeric classes in the MATLAB software include signed and unsigned integers, and
single- and double-precision floating-point numbers. By default, MATLAB stores all
numeric values as double-precision floating point. (You cannot change the default type
and precision.) You can choose to store any number, or array of numbers, as integers or as
single-precision. Integer and single-precision arrays offer more memory-efficient storage
than double-precision.
All numeric types support basic array operations, such as subscripting, reshaping, and
mathematical operations.
3-2
Fundamental MATLAB Classes
You can create two-dimensional double and logical matrices using one of two storage
formats: full or sparse. For matrices with mostly zero-valued elements, a sparse matrix
requires a fraction of the storage space required for an equivalent full matrix. Sparse
matrices invoke methods especially tailored to solve sparse problems.
These classes require different amounts of storage, the smallest being a logical value
or 8-bit integer which requires only 1 byte. It is important to keep this minimum size in
mind if you work on data in files that were written using a precision smaller than 8 bits.
3-3
3 Overview of MATLAB Classes
3-4
See Also
See Also
More About
• “Valid Combinations of Unlike Classes” on page 15-2
3-5
4
Numeric Classes
Integers
In this section...
“Integer Classes” on page 4-2
“Creating Integer Data” on page 4-3
“Arithmetic Operations on Integer Classes” on page 4-4
“Largest and Smallest Values for Integer Classes” on page 4-5
Integer Classes
MATLAB has four signed and four unsigned integer classes. Signed types enable you to
work with negative integers as well as positive, but cannot represent as wide a range of
numbers as the unsigned types because one bit is used to designate a positive or negative
sign for the number. Unsigned types give you a wider range of numbers, but these
numbers can only be zero or positive.
MATLAB supports 1-, 2-, 4-, and 8-byte storage for integer data. You can save memory and
execution time for your programs if you use the smallest integer type that accommodates
your data. For example, you do not need a 32-bit integer to store the value 100.
Here are the eight integer classes, the range of values you can store with each type, and
the MATLAB conversion function required to create that type:
4-2
Integers
For example, to store 325 as a 16-bit signed integer assigned to variable x, type
x = int16(325);
If the number being converted to an integer has a fractional part, MATLAB rounds to the
nearest integer. If the fractional part is exactly 0.5, then from the two equally nearby
integers, MATLAB chooses the one for which the absolute value is larger in magnitude:
x = 325.499;
int16(x)
ans =
int16
325
x = x + .001;
int16(x)
ans =
int16
326
If you need to round a number using a rounding scheme other than the default, MATLAB
provides four rounding functions: round, fix, floor, and ceil. The fix function
enables you to override the default and round towards zero when there is a nonzero
fractional part:
x = 325.9;
int16(fix(x))
ans =
int16
325
4-3
4 Numeric Classes
Arithmetic operations that involve both integers and floating-point always result in an
integer data type. MATLAB rounds the result, when necessary, according to the default
rounding algorithm. The example below yields an exact answer of 1426.75 which
MATLAB then rounds to the next highest integer:
int16(325) * 4.39
ans =
int16
1427
The integer conversion functions are also useful when converting other classes, such as
strings, to integers:
int8(str)
ans =
If you convert a NaN value into an integer class, the result is a value of 0 in that integer
class. For example,
int32(NaN)
ans =
int32
• Integers or integer arrays of the same integer data type. This yields a result that has
the same data type as the operands:
4-4
Integers
ans =
uint32
• Integers or integer arrays and scalar double-precision floating-point numbers. This
yields a result that has the same data type as the integer operands:
x = uint32([132 347 528]) .* 75.49;
class(x)
ans =
uint32
For all binary operations in which one operand is an array of integer data type (except 64-
bit integers) and the other is a scalar double, MATLAB computes the operation using
element-wise double-precision arithmetic, and then converts the result back to the
original integer data type. For binary operations involving a 64-bit integer array and a
scalar double, MATLAB computes the operation as if 80-bit extended-precision arithmetic
were used, to prevent loss of precision.
You can also obtain these values with the intmax and intmin functions:
intmax('int8')
ans =
int8
127
intmin('int8')
ans =
int8
-128
If you convert a number that is larger than the maximum value of an integer data type to
that type, MATLAB sets it to the maximum value. Similarly, if you convert a number that is
4-5
4 Numeric Classes
smaller than the minimum value of the integer data type, MATLAB sets it to the minimum
value. For example,
x = int8(300)
x =
int8
127
x = int8(-300)
x =
int8
-128
Also, when the result of an arithmetic operation involving integers exceeds the maximum
(or minimum) value of the data type, MATLAB sets it to the maximum (or minimum) value:
x = int8(100) * 3
x =
int8
127
x = int8(-100) * 3
x =
int8
-128
4-6
Floating-Point Numbers
Floating-Point Numbers
In this section...
“Double-Precision Floating Point” on page 4-7
“Single-Precision Floating Point” on page 4-7
“Creating Floating-Point Data” on page 4-8
“Arithmetic Operations on Floating-Point Numbers” on page 4-9
“Largest and Smallest Values for Floating-Point Classes” on page 4-11
“Accuracy of Floating-Point Data” on page 4-12
“Avoiding Common Problems with Floating-Point Arithmetic” on page 4-14
Bits Usage
63 Sign (0 = positive, 1 = negative)
62 to 52 Exponent, biased by 1023
51 to 0 Fraction f of the number 1.f
Bits Usage
31 Sign (0 = positive, 1 = negative)
4-7
4 Numeric Classes
Bits Usage
30 to 23 Exponent, biased by 127
22 to 0 Fraction f of the number 1.f
Because MATLAB stores numbers of type single using 32 bits, they require less memory
than numbers of type double, which use 64 bits. However, because they are stored with
fewer bits, numbers of type single are represented to less precision than numbers of
type double.
Because the default numeric type for MATLAB is double, you can create a double with a
simple assignment statement:
x = 25.783;
The whos function shows that MATLAB has created a 1-by-1 array of type double for the
value you just stored in x:
whos x
Name Size Bytes Class
x 1x1 8 double
Use isfloat if you just want to verify that x is a floating-point number. This function
returns logical 1 (true) if the input is a floating-point number, and logical 0 (false)
otherwise:
isfloat(x)
ans =
logical
4-8
Floating-Point Numbers
You can convert other numeric data, characters or strings, and logical data to double
precision using the MATLAB function, double. This example converts a signed integer to
double-precision floating point:
Because MATLAB stores numeric data as a double by default, you need to use the
single conversion function to create a single-precision number:
x = single(25.783);
The whos function returns the attributes of variable x in a structure. The bytes field of
this structure shows that when x is stored as a single, it requires just 4 bytes compared
with the 8 bytes to store it as a double:
xAttrib = whos('x');
xAttrib.bytes
ans =
4
You can convert other numeric data, characters or strings, and logical data to single
precision using the single function. This example converts a signed integer to single-
precision floating point:
single
-5.8932e+11
4-9
4 Numeric Classes
Double-Precision Operations
You can perform basic arithmetic operations with double and any of the following other
classes. When one or more operands is an integer (scalar or array), the double operand
must be a scalar. The result is of type double, except where noted otherwise:
This example performs arithmetic on data of types char and double. The result is of type
double:
c = 'uppercase' - 32;
class(c)
ans =
double
char(c)
ans =
UPPERCASE
Single-Precision Operations
You can perform basic arithmetic operations with single and any of the following other
classes. The result is always single:
• single
• double
• char
• logical
In this example, 7.5 defaults to type double, and the result is of type single:
x = single([1.32 3.47 5.28]) .* 7.5;
class(x)
ans =
single
4-10
Floating-Point Numbers
The MATLAB functions realmax and realmin return the maximum and minimum values
that you can represent with the double data type:
ans =
The range for double is:
-1.79769e+308 to -2.22507e-308 and
2.22507e-308 to 1.79769e+308
Numbers larger than realmax or smaller than -realmax are assigned the values of
positive and negative infinity, respectively:
realmax + .0001e+308
ans =
Inf
-realmax - .0001e+308
ans =
-Inf
The MATLAB functions realmax and realmin, when called with the argument
'single', return the maximum and minimum values that you can represent with the
single data type:
ans =
The range for single is:
-3.40282e+38 to -1.17549e-38 and
1.17549e-38 to 3.40282e+38
4-11
4 Numeric Classes
realmax('single') + .0001e+038
ans =
single
Inf
-realmax('single') - .0001e+038
ans =
single
-Inf
Double-Precision Accuracy
Because there are only a finite number of double-precision numbers, you cannot
represent all numbers in double-precision storage. On any computer, there is a small gap
between each double-precision number and the next larger double-precision number. You
can determine the size of this gap, which limits the precision of your results, using the
eps function. For example, to find the distance between 5 and the next larger double-
precision number, enter
format long
eps(5)
ans =
8.881784197001252e-16
This tells you that there are no double-precision numbers between 5 and 5 + eps(5). If
a double-precision computation returns the answer 5, the result is only accurate to within
eps(5).
4-12
Floating-Point Numbers
The value of eps(x) depends on x. This example shows that, as x gets larger, so does
eps(x):
eps(50)
ans =
7.105427357601002e-15
If you enter eps with no input argument, MATLAB returns the value of eps(1), the
distance from 1 to the next larger double-precision number.
Single-Precision Accuracy
Similarly, there are gaps between any two single-precision numbers. If x has type
single, eps(x) returns the distance between x and the next larger single-precision
number. For example,
x = single(5);
eps(x)
returns
ans =
single
4.7684e-07
Note that this result is larger than eps(5). Because there are fewer single-precision
numbers than double-precision numbers, the gaps between the single-precision numbers
are larger than the gaps between double-precision numbers. This means that results in
single-precision arithmetic are less precise than in double-precision arithmetic.
For a number x of type double, eps(single(x)) gives you an upper bound for the
amount that x is rounded when you convert it from double to single. For example,
when you convert the double-precision number 3.14 to single, it is rounded by
double(single(3.14) - 3.14)
ans =
1.0490e-07
eps(single(3.14))
ans =
4-13
4 Numeric Classes
single
2.3842e-07
The decimal number 4/3 is not exactly representable as a binary fraction. For this reason,
the following calculation does not give zero, but rather reveals the quantity eps.
e = 1 - 3*(4/3 - 1)
e =
2.2204e-16
Similarly, 0.1 is not exactly representable as a binary number. Thus, you get the following
nonintuitive behavior:
a = 0.0;
for i = 1:10
a = a + 0.1;
end
a == 1
ans =
logical
b = 1e-16 + 1 - 1e-16;
c = 1e-16 - 1e-16 + 1;
4-14
Floating-Point Numbers
b == c
ans =
logical
There are gaps between floating-point numbers. As the numbers get larger, so do the
gaps, as evidenced by:
(2^53 + 1) - 2^53
ans =
0
Since pi is not really π, it is not surprising that sin(pi) is not exactly zero:
sin(pi)
ans =
1.224646799147353e-16
When subtractions are performed with nearly equal operands, sometimes cancellation can
occur unexpectedly. The following is an example of a cancellation caused by swamping
(loss of precision that makes the addition insignificant).
sqrt(1e-16 + 1) - 1
ans =
0
Some functions in MATLAB, such as expm1 and log1p, may be used to compensate for
the effects of catastrophic cancellation.
4-15
4 Numeric Classes
y = A\b;
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.110223e-16.
These are only a few of the examples showing how IEEE floating-point arithmetic affects
computations in MATLAB. Note that all computations performed in IEEE 754 arithmetic
are affected, this includes applications written in C or FORTRAN, as well as MATLAB.
References
[1] Moler, Cleve. “Floating Points.” MATLAB News and Notes. Fall, 1996.
[2] Moler, Cleve. Numerical Computing with MATLAB. Natick, MA: The MathWorks, Inc.,
2004.
4-16
Create Complex Numbers
The following statement shows one way of creating a complex value in MATLAB. The
variable x is assigned a complex number with a real part of 2 and an imaginary part of 3:
x = 2 + 3i;
Another way to create a complex number is using the complex function. This function
combines two numeric inputs into a complex output, making the first input real and the
second imaginary:
x = rand(3) * 5;
y = rand(3) * -8;
z = complex(x, y)
z =
4.7842 -1.0921i 0.8648 -1.5931i 1.2616 -2.2753i
2.6130 -0.0941i 4.8987 -2.3898i 4.3787 -3.7538i
4.4007 -7.1512i 1.3572 -5.2915i 3.6865 -0.5182i
You can separate a complex number into its real and imaginary parts using the real and
imag functions:
zr = real(z)
zr =
4.7842 0.8648 1.2616
2.6130 4.8987 4.3787
4.4007 1.3572 3.6865
zi = imag(z)
zi =
-1.0921 -1.5931 -2.2753
-0.0941 -2.3898 -3.7538
-7.1512 -5.2915 -0.5182
4-17
4 Numeric Classes
Infinity
MATLAB represents infinity by the special value inf. Infinity results from operations like
division by zero and overflow, which lead to results too large to represent as conventional
floating-point values. MATLAB also provides a function called inf that returns the IEEE
arithmetic representation for positive infinity as a double scalar value.
Several examples of statements that return positive or negative infinity in MATLAB are
shown here.
x = 1/0 x = 1.e1000
x = x =
Inf Inf
x = exp(1000) x = log(0)
x = x =
Inf -Inf
isinf(x)
ans =
1
NaN
MATLAB represents values that are not real or complex numbers with a special value
called NaN, which stands for “Not a Number”. Expressions like 0/0 and inf/inf result in
NaN, as do any arithmetic operations involving a NaN:
x = 0/0
x =
4-18
Infinity and NaN
NaN
x = NaN;
whos x
Name Size Bytes Class
x 1x1 8 double
The NaN function returns one of the IEEE arithmetic representations for NaN as a double
scalar value. The exact bit-wise hexadecimal representation of this NaN value is,
format hex
x = NaN
x =
fff8000000000000
Always use the isnan function to verify that the elements in an array are NaN:
isnan(x)
ans =
MATLAB preserves the “Not a Number” status of alternate NaN representations and
treats all of the different representations of NaN equivalently. However, in some special
cases (perhaps due to hardware limitations), MATLAB does not preserve the exact bit
pattern of alternate NaN representations throughout an entire calculation, and instead
uses the canonical NaN bit pattern defined above.
Because two NaNs are not equal to each other, logical operations involving NaN always
return false, except for a test for inequality, (NaN ~= NaN):
4-19
4 Numeric Classes
NaN ~= NaN
ans =
1
4-20
Identifying Numeric Classes
Command Operation
whos x Display the data type of x.
xType = class(x); Assign the data type of x to a variable.
isnumeric(x) Determine if x is a numeric type.
isa(x, 'integer') Determine if x is the specified numeric type. (Examples for
isa(x, 'uint64') any integer, unsigned 64-bit integer, any floating point,
isa(x, 'float') double precision, and single precision are shown here).
isa(x, 'double')
isa(x, 'single')
isreal(x) Determine if x is real or complex.
isnan(x) Determine if x is Not a Number (NaN).
isinf(x) Determine if x is infinite.
isfinite(x) Determine if x is finite.
4-21
4 Numeric Classes
x = 4/3
x =
1.3333
You can change the display in the Command Window or Editor using the format function.
format long
x
x =
1.333333333333333
Using the format function only sets the format for the current MATLAB session. To set
the format for subsequent sessions, click Preferences on the Home tab in the
Environment section. Select MATLAB > Command Window, and then choose a
Numeric format option.
4-22
See Also
The display format only affects how numbers are displayed, not how they are stored in
MATLAB.
See Also
format
Related Examples
• “Format Output”
4-23
4 Numeric Classes
Integer Arithmetic
This example shows how to perform arithmetic on integer data representing signals and
images.
Load measurement datasets comprising signals from four instruments using 8 and 16-bit
A-to-D's resulting in data saved as int8, int16 and uint16. Time is stored as uint16.
load integersignal
% Look at variables
whos Signal1 Signal2 Signal3 Signal4 Time1
Plot Data
First we will plot two of the signals to see the signal ranges.
4-24
Integer Arithmetic
Here we see the values for int8. It is likely that these values would need to be scaled to
calculate the actual physical value that the signal represents e.g. Volts.
Process Data
Now let's plot the sum signal and see where it saturates.
cla;
plot(Time1, SumSig);
4-25
4 Numeric Classes
hold on
Saturated = (SumSig == intmin('int8')) | (SumSig == intmax('int8')); % Find where it ha
plot(Time1(Saturated),SumSig(Saturated),'rd')
grid
hold off
4-26
Integer Arithmetic
Here we see the images are 24-bit color, stored as three planes of uint8 data.
Display Images
4-27
4 Numeric Classes
Scale an Image
We can scale the image by a double precision constant but keep the image stored as
integers. For example,
duller = 0.5 * street2; % Scale image with a double constant but create an integer
whos duller
4-28
Integer Arithmetic
subplot(1,2,1);
image(street2);
axis off equal tight
title('Original'); % Display image
subplot(1,2,2);
image(duller);
axis off equal tight
title('Duller'); % Display image
4-29
4 Numeric Classes
We can add the two street images together and plot the ghostly result.
4-30
Single Precision Math
Ad = [1 2 0; 2 5 -1; 4 10 -1]
Ad = 3×3
1 2 0
2 5 -1
4 10 -1
A = single(Ad); % or A = cast(Ad,'single');
We can also create single precision zeros and ones with their respective functions.
n = 1000;
Z = zeros(n,1,'single');
O = ones(n,1,'single');
whos A Ad O Z n
A 3x3 36 single
Ad 3x3 72 double
O 1000x1 4000 single
Z 1000x1 4000 single
n 1x1 8 double
4-31
4 Numeric Classes
We can see that some of the variables are of type single and that the variable A (the
single precision version of Ad) takes half the number of bytes of memory to store because
singles require just four bytes (32-bits), whereas doubles require 8 bytes (64-bits).
1 2 4
2 5 10
0 -1 -1
whos B
B 3x3 36 single
5 12 24
12 30 59
24 59 117
C = A .* B % Elementwise arithmetic
1 4 0
4 25 -10
0 -10 1
4-32
Single Precision Math
5 2 -2
-2 -1 1
0 -2 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
E = eig(A) % Eigenvalues
3.7321
0.2679
1.0000
F = fft(A(:,1)) % FFT
7.0000 + 0.0000i
-2.0000 + 1.7321i
-2.0000 - 1.7321i
12.3171
4-33
4 Numeric Classes
0.5149
0.1577
1 -5 5 -1
3.7321
1.0000
0.2679
R = conv(P,Q)
4-34
Single Precision Math
Now let's look at a function to compute enough terms in the Fibonacci sequence so the
ratio is less than the correct machine epsilon (eps) for datatype single or double.
% How many terms needed to get single precision results?
fibodemo('single')
ans = 19
ans = 41
4-35
4 Numeric Classes
fcurrent = ones(dtype);
fnext = fcurrent;
goldenMean = (ones(dtype)+sqrt(5))/2;
tol = eps(goldenMean);
nterms = 2;
while abs(fnext/fcurrent - goldenMean) >= tol
nterms = nterms + 1;
temp = fnext;
fnext = fnext + fcurrent;
fcurrent = temp;
end
Notice that we initialize several of our variables, fcurrent, fnext, and goldenMean,
with values that are dependent on the input datatype, and the tolerance tol depends on
that type as well. Single precision requires that we calculate fewer terms than the
equivalent double precision calculation.
4-36
5
To apply a single condition, start by creating a 5-by-5 matrix that contains random
integers between 1 and 15. Reset the random number generator to the default state for
reproducibility.
rng default
A = randi(15,5)
A = 5×5
13 2 3 3 10
14 5 15 7 1
2 9 15 14 13
14 15 8 12 15
10 15 13 15 11
Use the relational less than operator, <, to determine which elements of A are less than 9.
Store the result in B.
B = A < 9
0 1 1 1 0
0 1 0 1 1
1 0 0 0 0
0 0 1 0 0
0 0 0 0 0
The result is a logical matrix. Each value in B represents a logical 1 (true) or logical 0
(false) state to indicate whether the corresponding element of A fulfills the condition A
5-2
Find Array Elements That Meet a Condition
< 9. For example, A(1,1) is 13, so B(1,1) is logical 0 (false). However, A(1,2) is 2,
so B(1,2) is logical 1 (true).
Although B contains information about which elements in A are less than 9, it doesn’t tell
you what their values are. Rather than comparing the two matrices element by element,
you can use B to index into A.
A(B)
ans = 8×1
2
2
5
3
8
3
7
1
The result is a column vector of the elements in A that are less than 9. Since B is a logical
matrix, this operation is called logical indexing. In this case, the logical array being
used as an index is the same size as the other array, but this is not a requirement. For
more information, see “Array Indexing”.
Some problems require information about the locations of the array elements that meet a
condition rather than their actual values. In this example, you can use the find function
to locate all of the elements in A less than 9.
I = find(A < 9)
I = 8×1
3
6
7
11
14
16
17
22
5-3
5 The Logical Class
The result is a column vector of linear indices. Each index describes the location of an
element in A that is less than 9, so in practice A(I) returns the same result as A(B). The
difference is that A(B) uses logical indexing, whereas A(I) uses linear indexing.
You can use the logical and, or, and not operators to apply any number of conditions to
an array; the number of conditions is not limited to one or two.
First, use the logical and operator, denoted &, to specify two conditions: the elements
must be less than 9 and greater than 2. Specify the conditions as a logical index to
view the elements that satisfy both conditions.
ans = 5×1
5
3
8
3
7
The result is a list of the elements in A that satisfy both conditions. Be sure to specify
each condition with a separate statement connected by a logical operator. For example,
you cannot specify the conditions above by A(2<A<9), since it evaluates to A(2<A |
A<9).
Next, find the elements in A that are less than 9 and even numbered.
ans = 3×1
2
2
8
The result is a list of all even elements in A that are less than 9. The use of the logical
NOT operator, ~, converts the matrix mod(A,2) into a logical matrix, with a value of
logical 1 (true) located where an element is evenly divisible by 2.
5-4
Find Array Elements That Meet a Condition
Finally, find the elements in A that are less than 9 and even numbered and not equal
to 2.
ans = 8
The result, 8, is even, less than 9, and not equal to 2. It is the only element in A that
satisfies all three conditions.
Use the find function to get the index of the element equal to 8 that satisfies the
conditions.
ans = 14
Replace all values in A that are greater than 10 with the number 10.
A(A>10) = 10
A = 5×5
10 2 3 3 10
10 5 10 7 1
2 9 10 10 10
10 10 8 10 10
10 10 10 10 10
Next, replace all values in A that are not equal to 10 with a NaN value.
A(A~=10) = NaN
A = 5×5
5-5
5 The Logical Class
Lastly, replace all of the NaN values in A with zeros and apply the logical NOT operator,
~A.
A(isnan(A)) = 0;
C = ~A
0 1 1 1 0
0 1 0 1 1
1 1 0 0 0
0 0 1 0 0
0 0 0 0 0
The resulting matrix has values of logical 1 (true) in place of the NaN values, and logical
0 (false) in place of the 10s. The logical NOT operation, ~A, converts the numeric array
into a logical array such that A&C returns a matrix of logical 0 (false) values and A|C
returns a matrix of logical 1 (true) values.
See Also
Logical Operators: Short Circuit | and | find | isnan | nan | not | or | xor
5-6
Reduce Logical Arrays to Single Value
The any and all functions are natural extensions of the logical | (OR) and & (AND)
operators, respectively. However, rather than comparing just two elements, the any and
all functions compare all of the elements in a particular dimension of an array. It is as if
all of those elements are connected by & or | operators and the any or all functions
evaluate the resulting long logical expressions. Therefore, unlike the core logical
operators, the any and all functions reduce the size of the array dimension that they
operate on so that it has size 1. This enables the reduction of many logical values into a
single logical condition.
First, create a matrix A that contains random integers between 1 and 25. Reset the
random number generator to the default state for reproducibility.
rng default
A = randi(25,5)
A = 5×5
21 3 4 4 17
23 7 25 11 1
4 14 24 23 22
23 24 13 20 24
16 25 21 24 17
Next, use the mod function along with the logical NOT operator, ~, to determine which
elements in A are even.
A = ~mod(A,2)
0 0 1 1 0
0 0 0 0 0
1 1 1 0 1
0 1 0 1 1
1 0 0 1 0
5-7
5 The Logical Class
The resulting matrices have values of logical 1 (true) where an element is even, and
logical 0 (false) where an element is odd.
Since the any and all functions reduce the dimension that they operate on to size 1, it
normally takes two applications of one of the functions to reduce a 2–D matrix into a
single logical condition, such as any(any(A)). However, if you use the notation A(:) to
regard all of the elements of A as a single column vector, you can use any(A(:)) to get
the same logical information without nesting the function calls.
any(A(:))
ans = logical
1
You can perform logical and relational comparisons within the function call to any or all.
This makes it easy to quickly test an array for a variety of properties.
all(~A(:))
ans = logical
0
Determine whether any main or super diagonal elements in A are even. Since the vectors
returned by diag(A) and diag(A,1) are not the same size, you first need to reduce
each diagonal to a single scalar logical condition before comparing them. You can use the
short-circuit OR operator || to perform the comparison, since if any elements in the first
diagonal are even then the entire expression evaluates to true regardless of what appears
on the right-hand side of the operator.
any(diag(A)) || any(diag(A,1))
ans = logical
1
5-8
See Also
See Also
Logical Operators: Short Circuit | all | and | any | or | xor
5-9
6
You can store any 1-by-n sequence of characters as a string, using the string data type.
Starting in R2017a, enclose text in double quotes to create a string.
str = "Hello, world"
str =
"Hello, world"
Though the text "Hello, world" is 12 characters long, str itself is a 1-by-1 string, or
string scalar. You can use a string scalar to specify a file name, plot label, or any other
piece of textual information.
n = 12
If the text includes double quotes, use two double quotes within the definition.
str = "They said, ""Welcome!"" and waved."
str =
"They said, "Welcome!" and waved."
To add text to the end of a string, use the plus operator, +. If a variable can be converted
to a string, then plus converts it and appends it.
fahrenheit = 71;
celsius = (fahrenheit-32)/1.8;
tempText = "temperature is " + celsius + "C"
tempText =
"temperature is 21.6667C"
Starting in R2019a, you can also concatenate text using the append function.
6-2
Text in String and Character Arrays
tempText2 =
"Today's temperature is 21.6667C"
The string function can convert different types of inputs, such as numeric, datetime,
duration, and categorical values. For example, convert the output of pi to a string.
ps = string(pi)
ps =
"3.1416"
You can store multiple pieces of text in a string array. Each element of the array can
contain a string having a different number of characters, without padding.
str = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","ISS"]
str is a 2-by-3 string array. You can find the lengths of the strings with the strlength
function.
N = strlength(str)
N = 2×3
7 6 6
6 8 3
To store a 1-by-n sequence of characters as a character vector, using the char data type,
enclose it in single quotes.
6-3
6 Characters and Strings
chr =
'Hello, world'
The text 'Hello, world' is 12 characters long, and chr stores it as a 1-by-12 character
vector.
whos chr
If the text includes single quotes, use two single quotes within the definition.
chr =
'They said, 'Welcome!' and waved.'
• To specify single pieces of text, such as file names and plot labels.
• To represent data that is encoded using characters. In such cases, you might need
easy access to individual characters.
seq = 'GCTAGAATCC';
You can access individual characters or subsets of characters by indexing, just as you
would index into a numeric array.
seq(4:6)
ans =
'AGA'
Concatenate character vector with square brackets, just as you concatenate other types
of arrays.
seq2 =
'GCTAGAATCCATTAGAAACC'
6-4
See Also
Starting in R2019a, you also can concatenate text using append. The append function is
recommended because it treats string arrays, character vectors, and cell arrays of
character vectors consistently.
seq2 = append(seq,'ATTAGAAACC')
seq2 =
'GCTAGAATCCATTAGAAACC'
MATLAB functions that accept string arrays as inputs also accept character vectors and
cell arrays of character vectors.
See Also
append | cellstr | char | horzcat | plus | string | strlength
Related Examples
• “Create String Arrays” on page 6-6
• “Analyze Text Data with String Arrays” on page 6-19
• “Frequently Asked Questions About String Arrays” on page 6-66
• “Update Your Code to Accept Strings” on page 6-72
• “Cell Arrays of Character Vectors” on page 6-15
6-5
6 Characters and Strings
MATLAB® provides string arrays to store pieces of text. Each element of a string array
contains a 1-by-n sequence of characters.
str =
"Hello, world"
As an alternative, you can convert a character vector to a string using the string
function. chr is a 1-by-17 character vector. str is a 1-by-1 string that has the same text
as the character vector.
chr =
'Greetings, friend'
str = string(chr)
str =
"Greetings, friend"
Create a string array containing multiple strings using the [] operator. str is a 2-by-3
string array that contains six strings.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"]
6-6
Create String Arrays
Find the length of each string in str with the strlength function. Use strlength, not
length, to determine the number of characters in strings.
L = strlength(str)
L = 2×3
7 6 6
6 8 3
As an alternative, you can convert a cell array of character vectors to a string array using
the string function. MATLAB displays strings in string arrays with double quotes, and
displays characters vectors in cell arrays with single quotes.
C = {'Mercury','Venus','Earth'}
str = string(C)
In addition to character vectors, you can convert numeric, datetime, duration, and
categorical values to strings using the string function.
ans =
"26-Aug-2019 17:34:48"
6-7
6 Characters and Strings
Also, you can read text from files into string arrays using the readtable, textscan, and
fscanf functions.
String arrays can contain both empty and missing values. An empty string contains zero
characters. When you display an empty string, the result is a pair of double quotes with
nothing between them (""). The missing string is the string equivalent to NaN for numeric
arrays. It indicates where a string array has missing values. When you display a missing
string, the result is <missing>, with no quotation marks.
Create an empty string array using the strings function. When you call strings with
no arguments, it returns an empty string. Note that the size of str is 1-by-1, not 0-by-0.
However, str contains zero characters.
str = strings
str =
""
Create an empty character vector using single quotes. Note that the size of chr is 0-by-0.
chr = ''
chr =
Create a string array where every element is an empty string. You can preallocate a string
array with the strings function.
str = strings(2,3)
To create a missing string, convert a missing value using the string function. The
missing string displays as <missing>.
str = string(missing)
str =
<missing>
6-8
Create String Arrays
You can create a string array with both empty and missing strings. Use the ismissing
function to determine which elements are strings with missing values. Note that the
empty string is not a missing string.
str(1) = "";
str(2) = "Gemini";
str(3) = string(missing)
ismissing(str)
0 0 1
Compare a missing string to another string. The result is always 0 (false), even when
you compare a missing string to another missing string.
str = string(missing);
str == "Gemini"
ans = logical
0
str == string(missing)
ans = logical
0
String arrays support array operations such as indexing and reshaping. Use array
indexing to access the first row of str and all the columns.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
str(1,:)
6-9
6 Characters and Strings
ans =
"Skylab B"
Assign a new string outside the bounds of str. MATLAB expands the array and fills
unallocated elements with missing values.
str(3,4) = "Mir"
You can index into a string array using curly braces, {}, to access characters directly. Use
curly braces when you need to access and modify characters within a string element.
Indexing with curly braces provides compatibility for code that could work with either
string arrays or cell arrays of character vectors. But whenever possible, use string
functions to work with the characters in strings.
Access the second element in the second row with curly braces. chr is a character vector,
not a string.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
chr = str{2,2}
chr =
'Skylab B'
Access the character vector and return the first three characters.
str{2,2}(1:3)
ans =
'Sky'
6-10
Create String Arrays
Find the space characters in a string and replace them with dashes. Use the isspace
function to inspect individual characters within the string. isspace returns a logical
vector that contains a true value wherever there is a space character. Finally, display the
modified string element, str(2,2).
TF = isspace(str{2,2})
0 0 0 0 0 0 1 0
str{2,2}(TF) = "-";
str(2,2)
ans =
"Skylab-B"
Note that in this case, you can also replace spaces using the replace function, without
resorting to curly brace indexing.
replace(str(2,2)," ","-")
ans =
"Skylab-B"
Concatenate strings into a string array just as you would concatenate arrays of any other
kind.
str1 = ["Mercury","Gemini","Apollo"];
str2 = ["Skylab","Skylab B","ISS"];
str = [str1 str2]
Transpose str1 and str2. Concatenate them and then vertically concatenate column
headings onto the string array. When you concatenate character vectors into a string
array, the character vectors are automatically converted to strings.
6-11
6 Characters and Strings
str1 = str1';
str2 = str2';
str = [str1 str2];
str = [["Mission:","Station:"] ; str]
To append text to strings, use the plus operator, +. The plus operator appends text to
strings but does not change the size of a string array.
Append a last name to an array of names. If you append a character vector to strings,
then the character vector is automatically converted to a string.
names = ["Mary";"John";"Elizabeth";"Paul";"Ann"];
names = names + ' Smith'
Append different last names. You can append text to a string array from a string array or
from a cell array of character vectors. When you add nonscalar arrays, they must be the
same size.
names = ["Mary";"John";"Elizabeth";"Paul";"Ann"];
lastnames = ["Jones";"Adams";"Young";"Burns";"Spencer"];
names = names + " " + lastnames
6-12
Create String Arrays
Append a missing string. When you append a missing string with the plus operator, the
output is a missing string.
str1 = "Jones";
str2 = string(missing);
str1 + str2
ans =
<missing>
MATLAB provides a rich set of functions to work with string arrays. For example, you can
use the split, join, and sort functions to rearrange the string array names so that the
names are in alphabetical order by last name.
Split names on the space characters. Splitting changes names from a 5-by-1 string array
to a 5-by-2 array.
Switch the columns of names so that the last names are in the first column. Add a comma
after each last name.
6-13
6 Characters and Strings
Join the last and first names. The join function places a space character between the
strings it joins. After the join, names is a 5-by-1 string array.
names = join(names)
names = sort(names)
See Also
ismissing | isspace | join | plus | sort | split | string | strings | strlength
Related Examples
• “Analyze Text Data with String Arrays” on page 6-19
• “Search and Replace Text” on page 6-49
• “Compare Text” on page 6-42
• “Test for Empty Strings and Missing Values” on page 6-26
• “Frequently Asked Questions About String Arrays” on page 6-66
• “Update Your Code to Accept Strings” on page 6-72
6-14
Cell Arrays of Character Vectors
Note
• As of R2018b, the recommended way to store text is to use string arrays. If you create
variables that have the string data type, store them in string arrays, not cell arrays.
For more information, see “Text in String and Character Arrays” on page 6-2 and
“Update Your Code to Accept Strings” on page 6-72.
• While the phrase cell array of strings frequently has been used to describe such cell
arrays, the phrase is no longer accurate because such a cell array holds character
vectors, not strings.
The character vectors in C can have different lengths because a cell array does not
require that its contents have the same size. To determine the lengths of the character
vectors in C, use the strlength function.
L = strlength(C)
L = 1×5
2 7 5 4 6
6-15
6 Characters and Strings
C = {'Li','Sanchez','Jones','Yang','Larson'};
chr = C{1}
chr =
'Li'
C{1} = 'Yang'
To refer to a subset of cells, instead of their contents, index using smooth parentheses.
C(1:3)
While you can access the contents of cells by indexing, most functions that accept cell
arrays as inputs operate on the entire cell array. For example, you can use the strcmp
function to compare the contents of C to a character vector. strcmp returns 1 where
there is a match and 0 otherwise.
TF = strcmp(C,'Yang')
1 0 0 1 0
num = sum(TF)
num = 2
6-16
Cell Arrays of Character Vectors
Use TF as logical indices to return the matches in C. If you index using smooth
parentheses, then the output is a cell array containing only the matches.
M = C(TF)
You can convert cell arrays of character vectors to string arrays. To convert a cell array of
character vectors, use the string function.
C = {'Li','Sanchez','Jones','Yang','Larson'}
str = string(C)
In fact, the string function converts any cell array, so long as all of the contents can be
converted to strings.
C2=1×4 cell
{[5]} {[10]} {'some text'} {[26-Aug-2019]}
str2 = string(C2)
6-17
6 Characters and Strings
See Also
cellstr | char | iscellstr | strcmp | string
More About
• “Text in String and Character Arrays” on page 6-2
• “Access Data in Cell Array” on page 12-5
• “Create String Arrays” on page 6-6
• “Update Your Code to Accept Strings” on page 6-72
• “Frequently Asked Questions About String Arrays” on page 6-66
6-18
Analyze Text Data with String Arrays
Read text from Shakespeare's Sonnets with the fileread function. fileread returns
the text as a 1-by-100266 character vector.
sonnets = fileread('sonnets.txt');
sonnets(1:35)
ans =
'THE SONNETS
by William Shakespeare'
Convert the text to a string using the string function. Then, split it on newline
characters using the splitlines function. sonnets becomes a 2625-by-1 string array,
where each string contains one line from the poems. Display the first five lines of
sonnets.
sonnets = string(sonnets);
sonnets = splitlines(sonnets);
sonnets(1:5)
To calculate the frequency of the words in sonnets, first clean it by removing empty
strings and punctuation marks. Then reshape it into a string array that contains individual
words as elements.
Remove the strings with zero characters ("") from the string array. Compare each
element of sonnets to "", the empty string. Starting in R2017a, you can create strings,
6-19
6 Characters and Strings
including an empty string, using double quotes. TF is a logical vector that contains a true
value wherever sonnets contains a string with zero characters. Index into sonnets with
TF and delete all strings with zero characters.
TF = (sonnets == "");
sonnets(TF) = [];
sonnets(1:10)
Replace some punctuation marks with space characters. For example, replace periods,
commas, and semi-colons. Keep apostrophes because they can be part of some words in
the Sonnets, such as light's.
p = [".","?","!",",",";",":"];
sonnets = replace(sonnets,p," ");
sonnets(1:10)
Strip leading and trailing space characters from each element of sonnets.
6-20
Analyze Text Data with String Arrays
sonnets = strip(sonnets);
sonnets(1:10)
Split sonnets into a string array whose elements are individual words. You can use the
split function to split elements of a string array on whitespace characters, or on
delimiters that you specify. However, split requires that every element of a string array
must be divisible into an equal number of new strings. The elements of sonnets have
different numbers of spaces, and therefore are not divisible into equal numbers of strings.
To use the split function on sonnets, write a for-loop that calls split on one element
at a time.
Create the empty string array sonnetWords using the strings function. Write a for-loop
that splits each element of sonnets using the split function. Concatenate the output
from split onto sonnetWords. Each element of sonnetWords is an individual word
from sonnets.
sonnetWords = strings(0);
for i = 1:length(sonnets)
sonnetWords = [sonnetWords ; split(sonnets(i))];
end
sonnetWords(1:10)
6-21
6 Characters and Strings
"creatures"
"we"
Find the unique words in sonnetWords. Count them and sort them based on their
frequency.
To count words that differ only by case as the same word, convert sonnetWords to
lowercase. For example, The and the count as the same word. Find the unique words
using the unique function. Then, count the number of times each unique word occurs
using the histcounts function.
sonnetWords = lower(sonnetWords);
[words,~,idx] = unique(sonnetWords);
numOccurrences = histcounts(idx,numel(words));
Sort the words in sonnetWords by number of occurrences, from most to least common.
[rankOfOccurrences,rankIndex] = sort(numOccurrences,'descend');
wordsByFrequency = words(rankIndex);
Plot the occurrences of words in the Sonnets from the most to least common words. Zipf's
Law states that the distribution of occurrences of words in a large body text follows a
power-law distribution.
loglog(rankOfOccurrences);
xlabel('Rank of word (most to least common)');
ylabel('Number of Occurrences');
6-22
Analyze Text Data with String Arrays
wordsByFrequency(1:10)
6-23
6 Characters and Strings
"thou"
Calculate the total number of occurrences of each word in sonnetWords. Calculate the
number of occurrences as a percentage of the total number of words, and calculate the
cumulative percentage from most to least common. Write the words and the basic
statistics for them to a table.
numOccurrences = numOccurrences(rankIndex);
numOccurrences = numOccurrences';
numWords = length(sonnetWords);
T = table;
T.Words = wordsByFrequency;
T.NumOccurrences = numOccurrences;
T.PercentOfText = numOccurrences / numWords * 100.0;
T.CumulativePercentOfText = cumsum(numOccurrences) / numWords * 100.0;
T(1:10,:)
ans=10×4 table
Words NumOccurrences PercentOfText CumulativePercentOfText
______ ______________ _____________ _______________________
6-24
See Also
The most common word in the Sonnets, and, occurs 490 times. Together, the ten most
common words account for 20.163% of the text.
See Also
histcounts | join | lower | replace | sort | split | splitlines | string | strip |
table | unique
Related Examples
• “Create String Arrays” on page 6-6
• “Search and Replace Text” on page 6-49
• “Compare Text” on page 6-42
• “Test for Empty Strings and Missing Values” on page 6-26
6-25
6 Characters and Strings
You can test a string array for empty strings using the == operator.
Starting in R2017a, you can create an empty string using double quotes with nothing
between them (""). Note that the size of str is 1-by-1, not 0-by-0. However, str contains
zero characters.
str = ""
str =
""
Create an empty character vector using single quotes. Note that the size of chr is 0-by-0.
The character array chr actually is an empty array, and not just an array with zero
characters.
chr = ''
chr =
Create an array of empty strings using the strings function. Each element of the array
is a string with no characters.
str2 = strings(1,3)
6-26
Test for Empty Strings and Missing Values
if (str == "")
disp 'str has zero characters'
end
Do not use the isempty function to test for empty strings. A string with zero characters
still has a size of 1-by-1. However, you can test if a string array has at least one dimension
with a size of zero using the isempty function.
Create an empty string array using the strings function. To be an empty array, at least
one dimension must have a size of zero.
str = strings(0,3)
str =
isempty(str)
ans = logical
1
Test a string array for empty strings. The == operator returns a logical array that is the
same size as the string array.
str = ["Mercury","","Apollo"]
str == ''
0 1 0
6-27
6 Characters and Strings
Strings always contain the empty string as a substring. In fact, the empty string is always
at both the start and the end of every string. Also, the empty string is always found
between any two consecutive characters in a string.
TF = logical
1
TF = startsWith(str,"")
TF = logical
1
Count the number of characters in str. Then count the number of empty strings in str.
The count function counts empty strings at the beginning and end of str, and between
each pair of characters. Therefore if str has N characters, it also has N+1 empty strings.
str
str =
"Hello, world"
strlength(str)
ans = 12
count(str,"")
ans = 13
Replace a substring with the empty string. When you call replace with an empty string,
it removes the substring and replaces it with a string that has zero characters.
replace(str,"world","")
6-28
Test for Empty Strings and Missing Values
ans =
"Hello, "
Insert a substring after empty strings using the insertAfter function. Because there
are empty strings between each pair of characters, insertAfter inserts substrings
between each pair.
insertAfter(str,"","-")
ans =
"-H-e-l-l-o-,- -w-o-r-l-d-"
In general, string functions that replace, erase, extract, or insert substrings allow you to
specify empty strings as the starts and ends of the substrings to modify. When you do so,
these functions operate on the start and end of the string, and between every pair of
characters.
You can test a string array for missing values using the ismissing function. The missing
string is the string equivalent to NaN for numeric arrays. It indicates where a string array
has missing values. The missing string displays as <missing>.
To create a missing string, convert a missing value using the string function.
str = string(missing)
str =
<missing>
You can create a string array with both empty and missing strings. Use the ismissing
function to determine which elements are strings with missing values. Note that the
empty string is not a missing string.
str(1) = "";
str(2) = "Gemini";
str(3) = string(missing)
ismissing(str)
6-29
6 Characters and Strings
0 0 1
Compare str to a missing string. The comparison is always 0 (false), even when you
compare a missing string to another missing string.
str == string(missing)
0 0 0
To find missing strings, use the ismissing function. Do not use the == operator.
See Also
all | any | contains | endsWith | eq | erase | eraseBetween | extractAfter |
extractBefore | extractBetween | insertAfter | insertBefore | ismissing |
replace | replaceBetween | startsWith | string | strings | strlength
Related Examples
• “Create String Arrays” on page 6-6
• “Analyze Text Data with String Arrays” on page 6-19
• “Search and Replace Text” on page 6-49
• “Compare Text” on page 6-42
6-30
Formatting Text
Formatting Text
To convert data to text and control its format, you can use formatting operators with
common conversion functions, such as num2str and sprintf. These operators control
notation, alignment, significant digits, and so on. They are similar to those used by the
printf function in the C programming language. Typical uses for formatted text include
text for display and output files.
For example, %f converts floating-point values to text using fixed-point notation. Adjust
the format by adding information to the operator, such as %.2f to represent two digits
after the decimal mark, or %12f to represent 12 characters in the output, padding with
spaces as needed.
A = pi*ones(1,3);
txt = sprintf('%f | %.2f | %12f', A)
txt =
'3.141593 | 3.14 | 3.141593'
You can combine operators with ordinary text and special characters in a format specifier.
For instance, \n inserts a newline character.
txt =
'Displaying pi:
3.141593
3.14
3.141593'
Functions that support formatting operators are compose, num2str, sprintf, fprintf,
and the error handling functions assert, error, warning, and MException.
6-31
6 Characters and Strings
% 3$ 0 12 .5 b u
Conversion Character
The conversion character specifies the notation of the output. It consists of a single
character and appears last in the format specifier.
Specifier Description
c Single character.
d Decimal notation (signed).
e Exponential notation (using a lowercase e, as in 3.1415e+00).
E Exponential notation (using an uppercase E, as in 3.1415E+00).
f Fixed-point notation.
g The more compact of %e or %f. (Insignificant zeroes do not print.)
G Same as %g, but using an uppercase E.
o Octal notation (unsigned).
s Character vector or string array.
u Decimal notation (unsigned).
x Hexadecimal notation (unsigned, using lowercase letters a–f).
X Hexadecimal notation (unsigned, using uppercase letters A–F).
For example, format the number 46 using different conversion characters to display the
number in decimal, fixed-point, exponential, and hexadecimal formats.
A = 46*ones(1,4);
txt = sprintf('%d %f %e %X', A)
txt =
'46 46.000000 4.600000e+01 2E'
6-32
Formatting Text
Subtype
The subtype field is a single alphabetic character that immediately precedes the
conversion character. Without the subtype field, the conversion characters %o, %x, %X,
and %u treat input data as integers. To treat input data as floating-point values instead
and convert them to octal, decimal, or hexadecimal representations, use one of following
subtype specifiers.
b The input data are double-precision floating-point values rather than unsigned
integers. For example, to print a double-precision value in hexadecimal, use a
format like %bx.
t The input data are single-precision floating-point values rather than unsigned
integers.
Precision
txt =
'157.08 1.6e+02 157.079633 157.08'
While you can specify the precision in a formatting operator for input text (for example, in
the %s operator), there is usually no reason to do so. If you specify the precision as p, and
p is less than the number of characters in the input text, then the output contains only the
first p characters.
Field Width
The field width in a formatting operator is a nonnegative integer that specifies the
number of digits or characters in the output when formatting input values. For example,
in the operator %7.3f, the field width is 7.
6-33
6 Characters and Strings
Specify different field widths. To show the width for each output, use the | character. By
default, the output text is padded with space characters when the field width is greater
than the number of characters.
txt =
'|1.570796e+02| 1.570796e+02|157.079633| 157.079633|'
When used on text input, the field width can determine whether to pad the output text
with spaces. If the field width is less than or equal to the number of characters in the
input text, then it has no effect.
txt =
' Pad left with spaces'
Flags
Optional flags control additional formatting of the output text. The table describes the
characters you can use as flags.
6-34
Formatting Text
Right- and left-justify the output. The default behavior is to right-justify the output text.
txt =
'right-justify: 12.30
left-justify: 12.30 '
Display a + sign for positive numbers. The default behavior is to omit the leading + sign
for positive numbers.
txt =
'no sign: 12.30
sign: +12.30'
Pad to the left with spaces and zeroes. The default behavior is to pad with spaces.
txt =
'Pad with spaces: 5.20
Pad with zeroes: 000000005.20'
6-35
6 Characters and Strings
Note You can specify more than one flag in a formatting operator.
Value Identifiers
By default, functions such as sprintf insert values from input arguments into the output
text in sequential order. To process the input arguments in a nonsequential order, specify
the order using numeric identifiers in the format specifier. Specify nonsequential
arguments with an integer immediately following the % sign, followed by a $ sign.
ans = ans =
Special Characters
Special characters can be part of the output text. But because they cannot be entered as
ordinary text, they require specific character sequences to represent them. To insert
special characters into output text, use any of the character sequences in the table.
6-36
Formatting Text
The figure illustrates how the field width and precision settings affect the output of the
formatting functions. In this figure, the zero following the % sign in the formatting
operator means to add leading zeroes to the output text rather than space characters.
6-37
6 Characters and Strings
• If the precision p is greater than the number of digits f in the fractional part of the
input, then p digits are shown after the decimal point. The fractional part is extended
to the right with p-f zeroes in the output.
• If the field width is not specified, then it defaults to p+1+n, where n is the number of
digits in the whole part of the input value.
• If the field width w is greater than p+1+n, then the whole part of the output value is
padded to the left with w-(p+1+n) additional characters. The additional characters
are space characters unless the formatting operator includes the 0 flag. In that case,
the additional characters are zeroes.
You can specify the field width and precision using values from a sequential argument list.
Use an asterisk (*) in place of the field width or precision fields of the formatting
operator.
For example, format and display three numbers. In each case, use an asterisk to specify
that the field width or precision come from input arguments that follow the format
specifier.
txt =
' 123.456780 16.428 3.1416'
The table describes the effects of each formatting operator in the example.
You can mix the two styles. For example, get the field width from the following input
argument and the precision from the format specifier.
6-38
Formatting Text
txt =
'123.46'
You also can specify field width and precision as values from a nonsequential argument
list, using an alternate syntax shown in the figure. Within the formatting operator, specify
the field width and precision with asterisks that follow numbered identifiers and $ signs.
Specify the values of the field width and precision with input arguments that follow the
format specifier.
For example, format and display three numbers. In each case, use a numbered identifier
to specify that the field width or precision come from input arguments that follow the
format specifier.
txt =
' 123.456780 16.428 3.1416'
The table describes the effect of each formatting operator in the example.
6-39
6 Characters and Strings
ans = ans =
If your function call provides more input arguments than there are formatting operators
in the format specifier, then the operators are reused. However, only function calls that
use sequential ordering reuse formatting operators. You cannot reuse formatting
operators when you use numbered identifiers.
ans = ans =
'1234' '1'
If you use numbered identifiers when the input data is a vector or array, then the output
does not contain formatted data.
6-40
See Also
ans = ans =
See Also
compose | fprintf | num2str | sprintf
Related Examples
• “Convert from Character Arrays to Numeric Values” on page 6-58
• “Convert from Numeric Values to Character Array” on page 6-56
6-41
6 Characters and Strings
Compare Text
Compare text in character arrays and string arrays in different ways. String arrays were
introduced in R2016b. You can compare string arrays and character vectors with
relational operators and with the strcmp function. You can sort string arrays using the
sort function, just as you would sort arrays of any other type. MATLAB® also provides
functions to inspect characters in pieces of text. For example, you can determine which
characters in a character vector or string array are letters or space characters.
You can compare string arrays for equality with the relational operators == and ~=. When
you compare string arrays, the output is a logical array that has 1 where the relation is
true, and 0 where it is not true.
Create two string scalars. Starting in R2017a, you can create strings using double quotes.
str1 = "Hello";
str2 = "World";
str1,str2
str1 =
"Hello"
str2 =
"World"
str1 == str2
ans = logical
0
str1 = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","International Space Station"];
str2 = "Apollo";
str1 == str2
6-42
Compare Text
0 0 1
0 0 0
Compare a string array to a character vector. As long as one of the variables is a string
array, you can make the comparison.
chr = 'Gemini';
TF = (str1 == chr)
0 1 0
0 0 0
Index into str1 with TF to extract the string elements that matched Gemini. You can use
logical arrays to index into an array.
str1(TF)
ans =
"Gemini"
Compare for inequality using the ~= operator. Index into str1 to extract the elements
that do not match 'Gemini'.
TF = (str1 ~= chr)
1 0 1
1 1 1
str1(TF)
6-43
6 Characters and Strings
Compare two nonscalar string arrays. When you compare two nonscalar arrays, they must
be the same size.
str2 = ["Mercury","Mars","Apollo";...
"Jupiter","Saturn","Neptune"];
TF = (str1 == str2)
1 0 1
0 0 0
You can also compare strings with the relational operators >, >=, <, and <=. Strings that
start with uppercase letters come before strings that start with lowercase letters. For
example, the string "ABC" is less than "abc". Digits and some punctuation marks also
come before letters.
"ABC" < "abc"
ans = logical
1
Compare a string array that contains names to another name with the > operator. The
names Sanchez, de Ponte, and Nash come after Matthews, because S, d, and N all are
greater than M.
str = ["Sanchez","Jones","de Ponte","Crosby","Nash"];
TF = (str > "Matthews")
1 0 1 0 1
6-44
Compare Text
str(TF)
You can sort string arrays. MATLAB® stores characters as Unicode® using the UTF-16
character encoding scheme. Character and string arrays are sorted according to the
UTF-16 code point order. For the characters that are also the ASCII characters, this order
means that uppercase letters come before lowercase letters. Digits and some punctuation
also come before letters.
Sort a 2-by-3 string array. The sort function sorts the elements in each column
separately.
sort(str2)
To sort the elements in each row, sort str2 along the second dimension.
sort(str2,2)
You can compare character vectors and cell arrays of character vectors to each other. Use
the strcmp function to compare two character vectors, or strncmp to compare the first
N characters. You also can use strcmpi and strncmpi for case-insensitive comparisons.
6-45
6 Characters and Strings
Compare two character vectors with the strcmp function. chr1 and chr2 are not equal.
chr1 = 'hello';
chr2 = 'help';
TF = strcmp(chr1,chr2)
TF = logical
0
Note that the MATLAB strcmp differs from the C version of strcmp. The C version of
strcmp returns 0 when two character arrays are the same, not when they are different.
Compare the first two characters with the strncmp function. TF is 1 because both
character vectors start with the characters he.
TF = strncmp(chr1,chr2,2)
TF = logical
1
Compare two cell arrays of character vectors. strcmp returns a logical array that is the
same size as the cell arrays.
1
0
0
You can inspect the characters in string arrays or character arrays with the isstrprop,
isletter, and isspace functions.
6-46
See Also
Determine which characters in a character vector are space characters. isspace returns
a logical vector that is the same size as chr.
0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1
The isstrprop function can query characters for many different traits. isstrprop can
determine whether characters in a string or character vector are letters, alphanumeric
characters, decimal or hexadecimal digits, or punctuation characters.
str =
"A horse! A horse! My kingdom for a horse!"
isstrprop(str,"punct")
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0
isstrprop(chr,"alpha")
1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0
See Also
eq | ge | gt | isletter | isspace | isstrprop | le | lt | ne | sort | strcmp
6-47
6 Characters and Strings
Related Examples
• “Text in String and Character Arrays” on page 6-2
• “Create String Arrays” on page 6-6
• “Analyze Text Data with String Arrays” on page 6-19
• “Search and Replace Text” on page 6-49
• “Test for Empty Strings and Missing Values” on page 6-26
6-48
Search and Replace Text
Identify text in string arrays, character vectors, or cell arrays of character vectors with
the contains, startsWith, and endsWith function.
Create a string. Starting in R2017a, you can create strings using double quotes.
str =
"Rosemary Jones"
Determine whether str contains the substring mary. The contains function returns a
logical 1 if it finds the substring any place within the string.
TF = contains(str,"mary")
TF = logical
1
You can also use the strfind function to find matching text. strfind returns the index
of the start of each match. In this case, strfind returns 5 because the m in mary is the
fifth character of str.
idx = strfind(str,"mary")
idx = 5
Find multiple matches with strfind. When there are multiple matches, strfind returns
the indices as an array.
idx = strfind(str,"s")
6-49
6 Characters and Strings
idx = 1×2
3 14
Create a string array that contains many names. Determine which names contain the
substring Ann. The contains function returns a logical array that has a 1 wherever str
has an element that contains Ann. To create a new string array that includes only the
matches, index into str with TF.
str = ["Rosemary Ann Jones","Peter Michael Smith","Ann Marie Young"]
TF = contains(str,"Ann")
1 0 1
matches = str(TF)
matches =
"Ann Marie Young"
Similarly, the endsWith function find strings that end with a specified piece of text.
You can also use the contains, startsWith, and endsWith functions to determine
whether character vectors contains text.
chr = 'John Paul Jones'
chr =
'John Paul Jones'
6-50
Search and Replace Text
TF = contains(chr,'Paul')
TF = logical
1
TF = endsWith(chr,'Paul')
TF = logical
0
Use the contains function to find text in rows of a string array. census1905 contains a
few rows of simulated census data for the year 1905. Each row contains a name, year of
birth, and number of times that name was given in that year.
census1905 = ["Ann Mary","1905","230";
"John","1905","5400";
"Mary","1905","4600";
"Maryjane","1905","304";
"Paul","1905","1206"];
Find the rows where the name is a variation of Mary with the contains function.
TF = contains(census1905(:,1),"Mary");
census1905(TF,:)
Replace Text
You can replace text in string arrays, character vectors, or cell arrays of character vectors
with the replace function.
6-51
6 Characters and Strings
str =
"Rosemary Jones"
newStr = replace(str,"mary","anne")
newStr =
"Roseanne Jones"
You can also replace text using the strrep function. However, the replace function is
recommended.
newStr = strrep(str,"Jones","Day")
newStr =
"Rosemary Day"
Replace text in a character vector. You can use replace and replaceBetween with
character vectors, as well as with strings.
chr = 'Mercury, Gemini, Apollo'
chr =
'Mercury, Gemini, Apollo'
replace(chr,'Gemini','Mars')
6-52
Search and Replace Text
ans =
'Mercury, Mars, Apollo'
Replace text in a string array of file names. Append the file names to the address of a
website. The file names contain spaces, but spaces cannot be part of web addresses.
Replace the space character, " ", with %20, which is the standard for web addresses.
Extract Text
Extract a substring from string arrays or character vectors with the extractAfter,
extractBefore, and extractBetween functions. Use these functions to extract
different substrings that precede, follow, or occur between specified pieces of text.
Create a string array that contains file names. Extract the portions of the names after
C:\Temp\ with the extractAfter function.
6-53
6 Characters and Strings
str = ["C:\Temp\MyReport.docx";
"C:\Temp\Data\Sample1.csv";
"C:\Temp\Slides.pptx"]
filenames = extractAfter(str,"C:\Temp\")
Extract customer names from a string array that encodes the names within XML tags.
names = extractBetween(str,"<CustomerName>","</CustomerName>")
See Also
contains | endsWith | erase | eraseBetween | extractAfter | extractBefore |
extractBetween | insertAfter | insertBefore | replace | replaceBetween |
startsWith | strfind | strrep
6-54
See Also
Related Examples
• “Text in String and Character Arrays” on page 6-2
• “Create String Arrays” on page 6-6
• “Analyze Text Data with String Arrays” on page 6-19
• “Compare Text” on page 6-42
• “Test for Empty Strings and Missing Values” on page 6-26
• “Regular Expressions” on page 2-64
6-55
6 Characters and Strings
In this section...
“Function Summary” on page 6-56
“Convert Numbers to Character Codes” on page 6-57
“Represent Numbers as Text” on page 6-57
“Convert to Specific Radix” on page 6-57
Function Summary
The functions listed in this table provide a number of ways to convert numeric data to
character arrays.
[3.1416 2.178] →
"3.1416" "2.178"
int2str Convert a positive or negative integer to a character [72 105] → '72 105'
type. (Rounds any fractional parts.)
num2str Convert a numeric type to a character type of the [72 105] →
specified precision and format. '72/105/' (format set
to %1d/)
mat2str Convert a numeric type to a character type of the [72 105] → '[72
specified precision, returning a character vector 105]'
MATLAB can evaluate.
dec2hex Convert a positive integer to a character type of [72 105] → '48 69'
hexadecimal base.
dec2bin Convert a positive integer to a character type of [72 105] → '1001000
binary base. 1101001'
6-56
Convert from Numeric Values to Character Array
x = [77 65 84 76 65 66];
char(x)
ans =
'MATLAB'
function plotlabel(x, y)
plot(x, y)
chr1 = num2str(min(x));
chr2 = num2str(max(x));
out = ['Value of f from ' chr1 ' to ' chr2];
xlabel(out);
6-57
6 Characters and Strings
In this section...
“Function Summary” on page 6-58
“Convert from Character Code” on page 6-59
“Convert Text that Represents Numeric Values” on page 6-59
“Convert from Specific Radix” on page 6-60
Function Summary
The functions listed in this table provide a number of ways to convert character arrays to
numeric data.
6-58
Convert from Character Arrays to Numeric Values
name = double(name)
name =
84 104 111 109 97 115 32 82 46 32 76 101 101
name = char(name)
name =
'Thomas R. Lee'
val = str2num(chr)
val =
3.7294
The str2double function converts a string array or a cell array of character vectors to
the double-precision values they represent:
c = {'37.294e-1'; '-58.375'; '13.796'};
str = string({'3.14159','2.718'});
d = str2double(c)
d =
6-59
6 Characters and Strings
3.7294
-58.3750
13.7960
x = str2double(str)
x =
3.1416 2.7180
whos
Name Size Bytes Class Attributes
The hex2num and hex2dec functions both take hexadecimal (base 16) inputs, but
hex2num returns the IEEE double-precision floating-point number it represents, while
hex2dec converts to a decimal integer.
6-60
Hexadecimal and Binary Values
• As literals. Starting in R2019b, you can write hexadecimal and binary values as literals
using an appropriate prefix as notation. For example, 0x2A is a literal that specifies 42
—and MATLAB stores it as a number, not as text.
• As strings or character vectors. For example, the character vector '2A' represents the
number 42 as a hexadecimal value. When you represent a hexadecimal or binary value
using text, enclose it in quotation marks. MATLAB stores this representation as text,
not a number.
MATLAB provides several functions for converting numbers to and from their
hexadecimal and binary representations.
Hexadecimal literals start with a 0x or 0X prefix, while binary literals start with a 0b or
0B prefix. MATLAB stores the number written with this notation as an integer. For
example, these two literals both represent the integer 42.
A = 0x2A
A = uint8
42
B = 0b101010
B = uint8
42
Do not use quotation marks when you write a number using this notation. Use 0-9, A-F,
and a-f to represent hexadecimal digits. Use 0 and 1 to represent binary digits.
By default, MATLAB stores the number as the smallest unsigned integer type that can
accommodate it. However, you can use an optional suffix to specify the type of integer
that stores the value.
• To specify unsigned 8-, 16-, 32-, and 64-bit integer types, use the suffixes u8, u16,
u32, and u64.
6-61
6 Characters and Strings
• To specify signed 8-, 16-, 32-, and 64-bit integer types, use the suffixes s8, s16, s32,
and s64.
A = 0x2As32
A = int32
42
When you specify signed integer types, you can write literals that represent negative
numbers. Represent negative numbers in two's complement form. For example, specify a
negative number with a literal using the s8 suffix.
A = 0xFFs8
A = int8
-1
Since MATLAB stores these literals as numbers, you can use them in any context or
function where you use numeric arrays.
You can also convert integers to character vectors that represent them as hexadecimal or
binary values using the dec2hex and dec2bin functions. Convert an integer to
hexadecimal.
hexStr = dec2hex(255)
hexStr =
'FF'
binStr = dec2bin(16)
binStr =
'10000'
Since these functions produce text, use them when you need text that represents numeric
values. For example, you can append these values to a title or a plot label, or write them
to a file that stores numbers as their hexadecimal or binary representations.
6-62
Hexadecimal and Binary Values
The recommended way to convert an array of numbers to text is to use the compose
function. This function returns a string array having the same size as the input numeric
array. To produce hexadecimal format, use %X as the format specifier.
A = 1×5
hexStr = compose("%X",A)
The dec2hex and dec2bin functions also convert arrays of numbers to text representing
them as hexadecimal or binary values. However, these functions return character arrays,
where each row represents a number from the input numeric array, padded with zeros as
necessary.
To convert a binary value to hexadecimal, start with a binary literal, and convert it to text
representing its hexadecimal value. Since a literal is interpreted as a number, you can
specify it directly as the input argument to dec2hex.
D = 0b1111;
hexStr = dec2hex(D)
hexStr =
'F'
If you start with a hexadecimal literal, then you can convert it to text representing its
binary value using dec2bin.
D = 0x8F;
binStr = dec2bin(D)
binStr =
'10001111'
6-63
6 Characters and Strings
One typical use of binary numbers is to represent bits. For example, many devices have
registers that provide access to a collection of bits representing data in memory or the
status of the device. When working with such hardware you can use numbers in MATLAB
to represent the value in a register. Use binary values and bitwise operations to represent
and access particular bits.
Create a number that represents an 8-bit register. It is convenient to start with binary
representation, but the number is stored as an integer.
register = 0b10010110
register = uint8
150
To get or set the values of particular bits, use bitwise operations. For example, use the
bitand and bitshift functions to get the value of the fifth bit. (Shift that bit to the first
position so that MATLAB returns a 0 or 1. In this example, the fifth bit is a 1.)
b5 = bitand(register,0b10000);
b5 = bitshift(b5,-4)
b5 = uint8
1
register = bitset(register,5,0)
register = uint8
134
Since register is an integer, use the dec2bin function to display all the bits in binary
format. binStr is a character vector, and represents the binary value without a leading
0b prefix.
binStr = dec2bin(register)
6-64
See Also
binStr =
'10000110'
See Also
bin2dec | bitand | bitset | bitshift | dec2bin | dec2hex | hex2dec | sprintf |
sscanf
More About
• “Convert from Character Arrays to Numeric Values” on page 6-58
• “Convert from Numeric Values to Character Array” on page 6-56
• “Formatting Text” on page 6-31
• “Bit-Wise Operations” on page 2-49
• “Perform Cyclic Redundancy Check” on page 2-56
6-65
6 Characters and Strings
In most respects, strings arrays behave like character vectors and cell arrays of character
vectors. However, there are a few key differences between string arrays and character
arrays that can lead to results you might not expect. For each of these differences, there
is a recommended way to use strings that leads to the expected result.
With command syntax, you separate inputs with spaces rather than commas, and you do
not enclose input arguments in parentheses. For example, you can use the cd function
with command syntax to change folders.
cd C:\Temp
The text C:\Temp is a character vector. In command form, all arguments are always
character vectors. If you have an argument, such as a folder name, that contains spaces,
then specify it as one input argument by enclosing it in single quotes.
cd 'C:\Program Files'
But if you specify the argument using double quotes, then cd throws an error.
cd "C:\Program Files"
Error using cd
Too many input arguments.
The error message can vary depending on the function that you use and the arguments
that you specify. For example, if you use the load function with command syntax and
specify the argument using double quotes, then load throws a different error.
6-66
Frequently Asked Questions About String Arrays
load "myVariables.mat"
In command form, double quotes are treated as part of the literal text rather than as the
string construction operator. If you wrote the equivalent of cd "C:\Program Files" in
functional form, then it would look like a call to cd with two arguments.
cd('"C:\Program','Files"')
When specifying arguments as strings, use function syntax. All functions that support
command syntax also support function syntax. For example, you can use cd with function
syntax and input arguments that are double quoted strings.
cd("C:\Program Files")
Avoid using cell arrays of strings. When you use cell arrays, you give up the performance
advantages that come from using string arrays. And in fact, most functions do not accept
cell arrays of strings as input arguments, options, or values of name-value pairs. For
example, if you specify a cell array of strings as an input argument, then the contains
function throws an error.
C = {"Venus","Earth","Mars"}
TF = contains(C,"Earth")
6-67
6 Characters and Strings
str = ["Venus","Earth","Mars"];
TF = contains(str,"Earth");
Before R2016b, the term "cell array of strings" meant a cell array whose elements all
contain character vectors. But it is more precise to refer to such cell arrays as "cell arrays
of character vectors," to distinguish them from string arrays.
Cell arrays can contain variables having any data types, including strings. It is still
possible to create a cell array whose elements all contain strings. And if you already have
specified cell arrays of character vectors in your code, then replacing single quotes with
double quotes might seem like a simple update. However, it is not recommended that you
create or use cell arrays of strings.
Create a character vector using single quotes. To determine its length, use the length
function. Because C is a vector, its length is equal to the number of characters. C is a 1-
by-11 vector.
C = 'Hello world';
L = length(C)
L = 11
Create a string with the same characters, using double quotes. Though it stores 11
characters, str is a 1-by-1 string array, or string scalar. If you call length on a string
scalar, then the output argument is 1, no matter how many characters it stores.
L = 1
L = strlength(C)
6-68
Frequently Asked Questions About String Arrays
L = 11
L = strlength(str)
L = 11
You also can use strlength on string arrays containing multiple strings and on cell
arrays of character vectors.
The length function returns the size of the longest dimension of an array. For a string
array, length returns the number of strings along the longest dimension of the array. It
does not return the number of characters within strings.
L = 0
However, an empty string is not an empty array. An empty string is a string scalar that
happens to have no characters.
sz = size("")
sz = 1×2
1 1
If you call isempty on an empty string, then it returns 0 (false) because the string is
not an empty array.
tf = isempty("")
tf = logical
0
However, if you call isempty on an empty character array, then it returns 1 (true). A
character array specified as a empty pair of single quotes, '', is a 0-by-0 character array.
tf = isempty('')
tf = logical
1
6-69
6 Characters and Strings
To test whether a piece of text has no characters, the best practice is to use the
strlength function. You can use the same call whether the input is a string scalar or a
character vector.
str = "";
if strlength(str) == 0
disp('String has no text')
end
chr = '';
if strlength(chr) == 0
disp('Character vector has no text')
end
For example, if you concatenate two strings, then the result is a 1-by-2 string array.
str = ["Hello" "World"]
However, if you concatenate two character vectors, then the result is a longer character
vector.
str = ['Hello' 'World']
chr = 'HelloWorld'
To append text to a string (or to the elements of a string array), use the plus operator
instead of square brackets.
str = "Hello" + "World"
str = "HelloWorld"
6-70
See Also
As an alternative, you can use the strcat function. strcat appends text whether the
input arguments are strings or character vectors.
str = strcat("Hello","World")
str = "HelloWorld"
Whether you use square brackets, plus, or strcat, you can specify an arbitrary number
of arguments. Append a space character between Hello and World.
See Also
cd | contains | copyfile | dir | isempty | length | load | plus | size | sprintf |
strcat | string | strlength
Related Examples
• “Create String Arrays” on page 6-6
• “Test for Empty Strings and Missing Values” on page 6-26
• “Compare Text” on page 6-42
• “Update Your Code to Accept Strings” on page 6-72
6-71
6 Characters and Strings
If you write code for other MATLAB users, then it is to your advantage to update your API
to accept string arrays, while maintaining backward compatibility with other text data
types. String adoption makes your code consistent with MathWorks products.
If your code has few dependencies, or if you are developing new code, then consider
using string arrays as your primary text data type for better performance. In that case,
best practice is to write or update your API to accept input arguments that are character
vectors, cell arrays of character vectors, or string arrays.
For the definitions of string array and other terms, see “Terminology for Character and
String Arrays” on page 6-80.
Functions
6-72
Update Your Code to Accept Strings
character vector (using single quotes). Best practice is to update the function so
that the argument can be specified as either a character vector or a string scalar
(using double quotes).
• Accept strings as both names and values in name-value pair arguments.
• A cell array of string arrays has a string array in each cell. For example,
{"hello","world"} is a cell array of string arrays. While you can create such a
cell array, it is not recommended for storing text. The elements of a string array
have the same data type and are stored efficiently. If you store strings in a cell
array, then you lose the advantages of using a string array.
However, if your code accepts heterogeneous cell arrays as inputs, then consider
accepting cell arrays that contain strings. You can convert any strings in such a cell
array to character vectors.
• In general, do not change the output type.
• If your function returns a character vector or cell array of character vectors, then
do not change the output type, even if the function accepts string arrays as inputs.
For example, the fileread function accepts an input file name specified as either
a character vector or a string, but the function returns the file contents as a
character vector. By keeping the output type the same, you can maintain backward
compatibility.
• Return the same data type when the function modifies input text.
• If your function modifies input text and returns the modified text as the output
argument, then the input and output arguments should have the same data type.
For example, the lower function accepts text as the input argument, converts it to
all lowercase letters, and returns it. If the input argument is a character vector,
then lower returns a character vector. If the input is a string array, then lower
returns a string array.
• Consider adding a 'TextType' argument to import functions.
• If your function imports data from files, and at least some of that data can be text,
then consider adding an input argument that specifies whether to return text as a
6-73
6 Characters and Strings
character array or a string array. For example, the readtable function provides
the 'TextType' name-value pair argument. This argument specifies whether
readtable returns a table with text in cell arrays of character vectors or string
arrays.
Classes
• For string adoption, treat methods as though they are functions. Accept string
arrays as input arguments, and in general, do not change the data type of the
output arguments, as described in the previous section.
• Do not change the data types of properties.
As an alternative, you can add a new property that is a string, and make it
dependent on the old property to maintain compatibility.
• Set properties using string arrays.
• If you can set a property using a character vector or cell array of character vectors,
then update your class to set that property using a string array too. However, do
not change the data type of the property. Instead, convert the input string array to
the data type of the property, and then set the property.
• Add a string method.
• If your class already has a char and/or a cellstr method, then add a string
method. If you can represent an object of your class as a character vector or cell
array of character vectors, then represent it as a string array too.
6-74
Update Your Code to Accept Strings
For example, if you have defined a function myFunc that accepts three input arguments,
process all three inputs using convertStringsToChars. Leave the rest of your code
unaltered.
function y = myFunc(a,b,c)
[a,b,c] = convertStringsToChars(a,b,c);
<line 1 of original code>
<line 2 of original code>
...
In this example, the arguments [a,b,c] overwrite the input arguments in place. If any
input argument is not a string array, then it is unaltered.
If myFunc accepts a variable number of input arguments, then process all the arguments
specified by varargin.
function y = myFunc(varargin)
[varargin{:}] = convertStringsToChars(varargin{:});
...
Performance Considerations
6-75
6 Characters and Strings
Functions
• A cell array of string arrays has a string array in each cell. While you can create
such a cell array, it is not recommended for storing text. If your code uses strings
as the primary text data type, store multiple pieces of text in a string array, not a
cell array of string arrays.
However, if your code accepts heterogeneous cell arrays as inputs, then consider
accepting cell arrays that contain strings.
• In general, return strings.
• If your function returns output arguments that are text, then return them as string
arrays.
• Return the same data type when the function modifies input text.
• If your function modifies input text and returns the modified text as the output
argument, then the input and output arguments should have the same data type.
Classes
• Accept character vectors and cell arrays of character vectors as input arguments,
as described in the previous section. In general, return strings as outputs.
• Specify properties as string arrays.
• If a property contains text, then set the property using a string array. When you
access the property, return the value as a string array.
6-76
Update Your Code to Accept Strings
For example, if you have defined a function myFunc that accepts three input arguments,
process all three inputs using convertCharsToStrings.
function y = myFunc(a,b,c)
[a,b,c] = convertCharsToStrings(a,b,c);
<line 1 of original code>
<line 2 of original code>
...
In this example, the arguments [a,b,c] overwrite the input arguments in place. If any
input argument is not a character vector or cell array of character vectors, then it is
unaltered.
If myFunc accepts a variable number of input arguments, then process all the arguments
specified by varargin.
function y = myFunc(varargin)
[varargin{:}] = convertCharsToStrings(varargin{:});
...
Performance Considerations
6-77
6 Characters and Strings
If you must convert input arguments, then use the functions in this table.
Conversion Function
String scalar to character vector char
String array to cell array of character cellstr
vectors
Character vector to string scalar string
Cell array of character vectors to string string
array
6-78
Update Your Code to Accept Strings
An empty string is a string with no characters. MATLAB displays an empty string as a pair
of double quotes with nothing between them (""). However, an empty string is still a 1-
by-1 string array. It is not an empty array.
The recommended way to check whether a string is empty is to use the strlength
function.
str = "";
tf = (strlength(str) ~= 0)
Note Do not use the isempty function to check for an empty string. An empty string has
no characters but is still a 1-by-1 string array.
The strlength function returns the length of each string in a string array. If the string
must be a string scalar, and also not empty, then check for both conditions.
If str could be either a character vector or string scalar, then you still can use
strlength to determine its length. strlength returns 0 if the input argument is an
empty character vector ('').
6-79
6 Characters and Strings
An empty string array is, in fact, an empty array—that is, an array that has at least one
dimension whose length is 0.
The recommended way to create an empty string array is to use the strings function,
specifying 0 as at least one of the input arguments. The isempty function returns 1 when
the input is an empty string array.
str = strings(0);
tf = isempty(str)
The strlength function returns a numeric array that is the same size as the input string
array. If the input is an empty string array, then strlength returns an empty array.
str = strings(0);
L = strlength(str)
String arrays also can contain missing strings. The missing string is the string equivalent
to NaN for numeric arrays. It indicates where a string array has missing values. The
missing string displays as <missing>, with no quotation marks.
You can create missing strings using the missing function. The recommended way to
check for missing strings is to use the ismissing function.
str = string(missing);
tf = ismissing(str)
Note Do not check for missing strings by comparing a string to the missing string.
The missing string is not equal to itself, just as NaN is not equal to itself.
str = string(missing);
f = (str == missing)
6-80
See Also
See Also
cellstr | char | convertCharsToStrings | convertContainedStringsToChars |
convertStringsToChars | isStringScalar | iscellstr | ischar | isstring |
string | strings | strlength | validateattributes
More About
• “Create String Arrays” on page 6-6
• “Test for Empty Strings and Missing Values” on page 6-26
• “Compare Text” on page 6-42
• “Search and Replace Text” on page 6-49
• “Frequently Asked Questions About String Arrays” on page 6-66
6-81
6 Characters and Strings
Function Summary
MATLAB provides these functions for working with character arrays:
Function Description
'chr' Create the character vector specified between quotes.
blanks Create a character vector of blanks.
sprintf Write formatted data as text.
strcat Concatenate character arrays.
char Concatenate character arrays vertically.
Function Description
deblank Remove trailing blanks.
lower Make all letters lowercase.
sort Sort elements in ascending or descending order.
strjust Justify a character array.
strrep Replace text within a character array.
strtrim Remove leading and trailing white space.
upper Make all letters uppercase.
6-82
Function Summary
Function Description
eval Execute a MATLAB expression.
sscanf Read a character array under format control.
Function Description
regexp Match regular expression on page 2-64.
strcmp Compare character arrays.
strcmpi Compare character arrays, ignoring case.
strfind Find a term within a character vector.
strncmp Compare the first N characters of character arrays.
strncmpi Compare the first N characters, ignoring case.
strtok Find a token in a character vector.
textscan Read data from a character array.
Function Description
iscellstr Return true for a cell array of character vectors.
ischar Return true for a character array.
isletter Return true for letters of the alphabet.
isstrprop Determine if a string is of the specified category.
isspace Return true for white-space characters.
6-83
6 Characters and Strings
Function Description
char Convert to a character array.
cellstr Convert a character array to a cell array of character vectors.
double Convert a character array to numeric codes.
int2str Represent an integer as text.
mat2str Convert a matrix to a character array you can run eval on.
num2str Represent a number as text.
str2num Convert a character vector to the number it represents.
str2double Convert a character vector to the double-precision value it
represents.
Function Description
intersect Set the intersection of two vectors.
ismember Detect members of a set.
setdiff Return the set difference of two vectors.
setxor Set the exclusive OR of two vectors.
union Set the union of two vectors.
unique Set the unique elements of a vector.
6-84
7
For example, create a MATLAB datetime array that represents two dates: June 28, 2014
at 6 a.m. and June 28, 2014 at 7 a.m. Specify numeric values for the year, month, day,
hour, minute, and second components for the datetime.
t = datetime(2014,6,28,6:7,0,0)
t =
28-Jun-2014 06:00:00 28-Jun-2014 07:00:00
Change the value of a date or time component by assigning new values to the properties
of the datetime array. For example, change the day number of each datetime by assigning
new values to the Day property.
t.Day = 27:28
t =
Change the display format of the array by changing its Format property. The following
format does not display any time components. However, the values in the datetime array
do not change.
t.Format = 'MMM dd, yyyy'
t =
Jun 27, 2014 Jun 28, 2014
If you subtract one datetime array from another, the result is a duration array in units
of fixed length.
t2 = datetime(2014,6,29,6,30,45)
t2 =
29-Jun-2014 06:30:45
d = t2 - t
7-2
Represent Dates and Times in MATLAB
d =
48:30:45 23:30:45
d =
You can create a duration in a single unit using the seconds, minutes, hours, days, or
years functions. For example, create a duration of 2 days, where each day is exactly 24
hours.
d = days(2)
d =
2 days
You can create a calendar duration in a single unit of variable length. For example, one
month can be 28, 29, 30, or 31 days long. Specify a calendar duration of 2 months.
L = calmonths(2)
L =
2mo
Add a number of calendar months and calendar days. The number of days remains
separate from the number of months because the number of days in a month is not fixed,
and cannot be determined until you add the calendar duration to a specific datetime.
L = calmonths(2) + caldays(35)
L =
2mo 35d
7-3
7 Dates and Time
t2 =
whos t2
In summary, there are several ways to represent dates and times, and MATLAB has a data
type for each approach:
7-4
See Also
See Also
calendarDuration | datetime | duration
7-5
7 Dates and Time
You can specify a time zone when you create a datetime, using the 'TimeZone' name-
value pair argument. The time zone value 'local' specifies the system time zone. To
display the time zone offset for each datetime, include a time zone offset specifier such as
'Z' in the value for the 'Format' argument.
t = datetime(2014,3,8:9,6,0,0,'TimeZone','local',...
'Format','d-MMM-y HH:mm:ss Z')
t =
A different time zone offset is displayed depending on whether the datetime occurs during
daylight saving time.
You can modify the time zone of an existing datetime. For example, change the TimeZone
property of t using dot notation. You can specify the time zone value as the name of a
time zone region in the IANA Time Zone Database. A time zone region accounts for the
current and historical rules for standard and daylight offsets from UTC that are observed
in that geographic region.
t.TimeZone = 'Asia/Shanghai'
t =
You also can specify the time zone value as a character vector of the form +HH:mm or -
HH:mm, which represents a time zone with a fixed offset from UTC that does not observe
daylight saving time.
t.TimeZone = '+08:00'
7-6
See Also
t =
Operations on datetime arrays with time zones automatically account for time zone
differences. For example, create a datetime in a different time zone.
u = datetime(2014,3,9,6,0,0,'TimeZone','Europe/London',...
'Format','d-MMM-y HH:mm:ss Z')
u =
dt = t - u
dt =
-19:00:00 04:00:00
When you perform operations involving datetime arrays, the arrays either must all have a
time zone associated with them, or they must all have no time zone.
See Also
datetime | timezones
Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Convert Date and Time to Julian Date or POSIX Time” on page 7-8
7-7
7 Dates and Time
While datetime arrays are not required to have a time zone, converting "unzoned"
datetime values to Julian dates or POSIX times can lead to unexpected results. To
ensure the expected result, specify the time zone before conversion.
You can specify a time zone for a datetime array, but you are not required to do so. In
fact, by default the datetime function creates an "unzoned" datetime array.
d = datetime('now')
d = datetime
27-Aug-2019 00:13:25
d is constructed from the local time on your machine and has no time zone associated
with it. In many contexts, you might assume that you can treat the times in an unzoned
datetime array as local times. However, the juliandate and posixtime functions
treat the times in unzoned datetime arrays as UTC times, not local times. To avoid any
ambiguity, it is recommended that you avoid using juliandate and posixtime on
unzoned datetime arrays. For example, avoid using posixtime(datetime('now')) in
your code.
If your datetime array has values that do not represent UTC times, specify the time zone
using the TimeZone name-value pair argument so that juliandate and posixtime
interpret the datetime values correctly.
d = datetime('now','TimeZone','America/New_York')
7-8
Convert Date and Time to Julian Date or POSIX Time
d = datetime
27-Aug-2019 00:13:25
As an alternative, you can specify the TimeZone property after you create the array.
d.TimeZone = 'America/Los_Angeles'
d = datetime
26-Aug-2019 21:13:25
A Julian date is the number of days (including fractional days) since noon on November
24, 4714 BCE, in the proleptic Gregorian calendar, or January 1, 4713 BCE, in the
proleptic Julian calendar. To convert datetime arrays to Julian dates, use the
juliandate function.
JDZ = 1×3
Create an unzoned copy of DZ. Convert D to the equivalent Julian dates. As D has no time
zone, juliandate treats the times as UTC times.
D = DZ;
D.TimeZone = '';
JD = juliandate(D)
7-9
7 Dates and Time
JD = 1×3
Compare JDZ and JD. The differences are equal to the time zone offset between UTC and
the America/New_York time zone in fractional days.
JDZ - JD
ans = 1×3
The POSIX time is the number of seconds (including fractional seconds) elapsed since
00:00:00 1-Jan-1970 UTC (Universal Coordinated Time), ignoring leap seconds. To
convert datetime arrays to POSIX times, use the posixtime function.
PTZ = posixtime(DZ)
PTZ = 1×3
Create an unzoned copy of DZ. Convert D to the equivalent POSIX times. As D has no time
zone, posixtime treats the times as UTC times.
D = DZ;
D.TimeZone = '';
PT = posixtime(D)
7-10
See Also
PT = 1×3
Compare PTZ and PT. The differences are equal to the time zone offset between UTC and
the America/New_York time zone in seconds.
PTZ - PT
ans = 1×3
See Also
datetime | juliandate | posixtime | timezones
Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Specify Time Zones” on page 7-6
7-11
7 Dates and Time
In this section...
“Formats for Individual Date and Duration Arrays” on page 7-12
“datetime Display Format” on page 7-12
“duration Display Format” on page 7-13
“calendarDuration Display Format” on page 7-14
“Default datetime Format” on page 7-15
t.Format = 'default'
Changing the Format property does not change the values in the array, only their display.
For example, the following can be representations of the same datetime value (the latter
two do not display any time components):
The Format property of the datetime, duration, and calendarDuration data types
accepts different formats as inputs.
7-12
Set Date and Time Display Format
To change the default formats, see “Default datetime Format” on page 7-15.
Alternatively, you can use the letters A-Z and a-z to specify a custom date format. You can
include nonletter characters such as a hyphen, space, or colon to separate the fields. This
table shows several common display formats and examples of the formatted output for the
date, Saturday, April 19, 2014 at 9:41:06 PM in New York City.
For a complete list of valid symbolic identifiers, see the Format property for datetime
arrays.
Note The letter identifiers that datetime accepts are different from those used by the
datestr, datenum, and datevec functions.
7-13
7 Dates and Time
To specify the number of fractional digits displayed, use the format function.
To display a duration in the form of a digital timer, specify one of the following character
vectors.
• 'dd:hh:mm:ss'
• 'hh:mm:ss'
• 'mm:ss'
• 'hh:mm'
You also can display up to nine fractional second digits by appending up to nine S
characters. For example, 'hh:mm:ss.SSS' displays the milliseconds of a duration value
to 3 digits.
Changing the Format property does not change the values in the array, only their display.
This table describes the date and time components that the characters represent.
7-14
Set Date and Time Display Format
To specify the number of digits displayed for fractional seconds, use the format function.
Changing the Format property does not change the values in the array, only their display.
where fmt is a character vector composed of the letters A-Z and a-z described for the
Format property of datetime arrays, above. For example,
datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss')
sets the default datetime format to include a 4-digit year, 2-digit month number, 2-digit
day number, and hour, minute, and second values.
In addition, you can specify a default format for datetimes created without time
components. For example,
datetime.setDefaultFormats('defaultdate','yyyy-MM-dd')
7-15
7 Dates and Time
sets the default date format to include a 4-digit year, 2-digit month number, and 2-digit
day number.
To reset the both the default format and the default date-only formats to the factory
defaults, type
datetime.setDefaultFormats('reset')
You also can set the default formats in the Preferences dialog box. For more information,
see “Set Command Window Preferences”.
See Also
calendarDuration | datetime | duration | format
7-16
Generate Sequence of Dates and Time
Create a sequence of datetime values starting from November 1, 2013 and ending on
November 5, 2013. The default step size is one calendar day.
t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,5,8,0,0);
t = t1:t2
Columns 4 through 5
t = t1:caldays(2):t2
7-17
7 Dates and Time
Specify a step size in units other than days. Create a sequence of datetime values spaced
18 hours apart.
t = t1:hours(18):t2
Columns 4 through 6
Use the years, days, minutes, and seconds functions to create datetime and duration
sequences using other fixed-length date and time units. Create a sequence of duration
values between 0 and 3 minutes, incremented by 30 seconds.
d = 0:seconds(30):minutes(3)
Assign a time zone to t1 and t2. In the America/New_York time zone, t1 now occurs
just before a daylight saving time change.
t1.TimeZone = 'America/New_York';
t2.TimeZone = 'America/New_York';
If you create the sequence using a step size of one calendar day, then the difference
between successive datetime values is not always 24 hours.
t = t1:t2;
dt = diff(t)
7-18
Generate Sequence of Dates and Time
t = t1:days(1):t2
Columns 4 through 5
dt = diff(t)
t = t1:1:t2
Columns 4 through 5
7-19
7 Dates and Time
Columns 4 through 5
Add a number of calendar months to the date, January 31, 2014, to create a sequence of
dates that fall on the last day of each month.
7-20
Generate Sequence of Dates and Time
t = datetime(2014,1,31) + calmonths(0:11)
Columns 6 through 10
Columns 11 through 12
30-Nov-2014 31-Dec-2014
Create a sequence of five equally spaced dates between April 14, 2014 and August 4,
2014. First, define the endpoints.
A = datetime(2014,04,14);
B = datetime(2014,08,04);
The third input to linspace specifies the number of linearly spaced points to generate
between the endpoints.
C = linspace(A,B,5)
Create a sequence of six equally spaced durations between 1 and 5.5 hours.
A = duration(1,0,0);
B = duration(5,30,0);
C = linspace(A,B,6)
7-21
7 Dates and Time
Generate a sequence of dates consisting of the next three occurrences of Monday. First,
define today's date.
t1 = datetime('today','Format','dd-MMM-yyyy eee')
t1 = datetime
27-Aug-2019 Tue
The first input to dateshift is always the datetime array from which you want to
generate a sequence. Specify 'dayofweek' as the second input to indicate that the
datetime values in the output sequence must fall on a specific day of the week. You can
specify the day of the week either by number or by name. For example, you can specify
Monday either as 2 or 'Monday'.
t = dateshift(t1,'dayofweek',2,1:3)
7-22
Generate Sequence of Dates and Time
corresponds to the start of the current month, and 4 corresponds to the start of the fourth
month from t1.
t1 = datetime(2014,04,01);
t = dateshift(t1,'start','month',0:4)
t1 = datetime(2014,04,01);
t = dateshift(t1,'end','month',0:2)
dt = caldiff(t,'days')
You can specify other units of time such as week, day, and hour.
t1 = datetime('now')
t1 = datetime
27-Aug-2019 02:57:00
t = dateshift(t1,'start','hour',0:4)
7-23
7 Dates and Time
Columns 4 through 5
Generate a sequence of datetime values beginning with the previous hour. Negative
integers in the last input to dateshift correspond to datetime values earlier than t1.
t = dateshift(t1,'start','hour',-1:1)
See Also
dateshift | linspace
7-24
Share Code and Data Across Locales
Create language-independent datetime values. That is, create datetime values that use
month numbers rather than month names, such as 01 instead of January. Avoid using day
of week names.
t = datetime('today','Format','yyyy-MM-dd')
t = datetime
2019-08-26
instead of this:
t = datetime('today','Format','eeee, dd-MMM-yyyy')
t = datetime
Monday, 26-Aug-2019
Display the hour using 24-hour clock notation rather than 12-hour clock notation. Use the
'HH' identifiers when specifying the display format for datetime values.
t = datetime('now','Format','HH:mm')
7-25
7 Dates and Time
t = datetime
18:03
instead of this:
t = datetime('now','Format','hh:mm a')
t = datetime
06:03 PM
When specifying the display format for time zone information, use the Z or X identifiers
instead of the lowercase z to avoid the creation of time zone names that might not be
recognized in other languages or regions.
t = datetime
26-08-2019 -0400
If you share files but not code, you do not need to write locale-independent code while
you work in MATLAB. However, when you write to a file, ensure that any text
representing dates and times is language-independent. Then, other MATLAB users can
read the files easily without having to specify a locale in which to interpret date and time
data.
7-26
See Also
27-Aug-2019
S is a cell array of character vectors representing dates in German. You can export S to a
text file to use with systems in the de_DE locale.
• When reading text files using the textscan function, specify the file encoding when
opening the file with fopen. The encoding is the fourth input argument to fopen.
• When reading text files using the readtable function, use the FileEncoding name-
value pair argument to specify the character encoding associated with the file.
See Also
cellstr | char | datetime | readtable | textscan
7-27
7 Dates and Time
Get the year values of each datetime in the array. Use dot notation to access the Year
property of t.
t_years = t.Year
t_years = 1×3
Get the month values of each datetime in t by accessing the Month property.
t_months = t.Month
t_months = 1×3
8 9 10
You can retrieve the day, hour, minute, and second components of each datetime in t by
accessing the Hour, Minute, and Second properties, respectively.
7-28
Extract or Assign Date and Time Components of Datetime Array
Use the month function to get the month number for each datetime in t. Using functions
is an alternate way to retrieve specific date or time components of t.
m = month(t)
m = 1×3
8 9 10
Use the month function rather than the Month property to get the full month names of
each datetime in t.
m = month(t,'name')
You can retrieve the year, quarter, week, day, hour, minute, and second components of
each datetime in t using the year, quarter, week, hour, minute, and second
functions, respectively.
w = week(t)
w = 1×3
35 40 44
Use the ymd function to get the year, month, and day values of t as three separate
numeric arrays.
[y,m,d] = ymd(t)
y = 1×3
7-29
7 Dates and Time
m = 1×3
8 9 10
d = 1×3
27 28 29
Use the hms function to get the hour, minute, and second values of t as three separate
numeric arrays.
[h,m,s] = hms(t)
h = 1×3
22 18 14
m = 1×3
59 59 59
s = 1×3
Change the year number of all datetime values in t to 2014. Use dot notation to modify
the Year property.
t.Year = 2014
Change the months of the three datetime values in t to January, February, and March,
respectively. You must specify the new value as a numeric array.
7-30
See Also
t.Month = [1,2,3]
t.TimeZone = 'Europe/Berlin';
Change the display format of t to display only the date, and not the time information.
t.Format = 'dd-MMM-yyyy'
If you assign values to a datetime component that are outside the conventional range,
MATLAB® normalizes the components. The conventional range for day of month numbers
is from 1 to 31. Assign day values that exceed this range.
The month and year numbers adjust so that all values remain within the conventional
range for each date component. In this case, January -1, 2014 converts to December 30,
2013.
See Also
datetime | hms | week | ymd
7-31
7 Dates and Time
Create a space-delimited text file named schedule.txt that contains the following (to
create the file, use any text editor, and copy and paste):
Read the file using the readtable function. Use the %D conversion specifier to read the
first and third columns of data as datetime values.
T = readtable('schedule.txt','Format','%{dd.MM.uuuu}D %s %{HH:mm}D','Delimiter',' ')
T =
Date Name Time
__________ _____ _____
10.03.2015 'Joe' 14:31
10.03.2015 'Bob' 15:33
11.03.2015 'Bob' 11:29
12.03.2015 'Kim' 12:09
12.03.2015 'Joe' 13:05
Change the display format for the T.Date and T.Time variables to view both date and
time information. Since the data in the first column of the file ("Date") have no time
information, the time of the resulting datetime values in T.Date default to midnight.
Since the data in the third column of the file ("Time") have no associated date, the date of
the datetime values in T.Time defaults to the current date.
T =
Date Name Time
7-32
See Also
Combine the date and time information from two different table variables by adding
T.Date and the time values in T.Time. Extract the time information from T.Time using
the timeofday function.
myDatetime =
10.03.2015 14:31
10.03.2015 15:33
11.03.2015 11:29
12.03.2015 12:09
12.03.2015 13:05
See Also
readtable | timeofday
7-33
7 Dates and Time
Create a datetime scalar. By default, datetime arrays are not associated with a time zone.
t1 = datetime('now')
t1 = datetime
27-Aug-2019 00:16:35
t2 = t1 + hours(1:3)
Verify that the difference between each pair of datetime values in t2 is 1 hour.
dt = diff(t2)
diff returns durations in terms of exact numbers of hours, minutes, and seconds.
t2 = t1 - minutes(20:10:40)
7-34
Date and Time Arithmetic
Add a numeric array to a datetime array. MATLAB® treats each value in the numeric
array as a number of exact, 24-hour days.
t2 = t1 + [1:3]
If you work with datetime values in different time zones, or if you want to account for
daylight saving time changes, work with datetime arrays that are associated with time
zones. Create a datetime scalar representing March 8, 2014 in New York.
t1 = datetime(2014,3,8,0,0,0,'TimeZone','America/New_York')
t1 = datetime
08-Mar-2014 00:00:00
t2 = t1 + days(0:2)
Because a daylight saving time shift occurred on March 9, 2014, the third datetime in t2
does not occur at midnight.
Verify that the difference between each pair of datetime values in t2 is 24 hours.
dt = diff(t2)
You can add fixed-length durations in other units such as years, hours, minutes, and
seconds by adding the outputs of the years, hours, minutes, and seconds functions,
respectively.
7-35
7 Dates and Time
To account for daylight saving time changes, you should work with calendar durations
instead of durations. Calendar durations account for daylight saving time shifts when they
are added to or subtracted from datetime values.
t3 = t1 + caldays(0:2)
View that the difference between each pair of datetime values in t3 is not always 24
hours due to the daylight saving time shift that occurred on March 9.
dt = diff(t3)
t1 = datetime(2014,1,31)
t1 = datetime
31-Jan-2014
t2 = t1 + calmonths(1:4)
Calculate the difference between each pair of datetime values in t2 in terms of a number
of calendar days using the caldiff function.
dt = caldiff(t2,'days')
7-36
Date and Time Arithmetic
The number of days between successive pairs of datetime values in dt is not always the
same because different months consist of a different number of days.
t2 = t1 + calyears(0:4)
Calculate the difference between each pair of datetime values in t2 in terms of a number
of calendar days using the caldiff function.
dt = caldiff(t2,'days')
The number of days between successive pairs of datetime values in dt is not always the
same because 2016 is a leap year and has 366 days.
You can use the calquarters, calweeks, and caldays functions to create arrays of
calendar quarters, calendar weeks, or calendar days that you add to or subtract from
datetime arrays.
Adding calendar durations is not commutative. When you add more than one
calendarDuration array to a datetime, MATLAB® adds them in the order in which they
appear in the command.
t2 = datetime
30-May-2014
7-37
7 Dates and Time
First add 30 calendar days to the same date, and then add 3 calendar months. The result
is not the same because when you add a calendar duration to a datetime, the number of
days added depends on the original date.
t2 = datetime
02-Jun-2014
d1 = calendarDuration
1y 2mo 20d
d2 = calmonths(11) + caldays(23)
d2 = calendarDuration
11mo 23d
d = d1 + d2
d = calendarDuration
2y 1mo 43d
When you sum two or more calendar durations, a number of months greater than 12 roll
over to a number of years. However, a large number of days does not roll over to a
number of months, because different months consist of different numbers of days.
2*d
ans = calendarDuration
4y 2mo 86d
7-38
Date and Time Arithmetic
Subtract one datetime array from another to calculate elapsed time in terms of an exact
number of hours, minutes, and seconds.
Find the exact length of time between a sequence of datetime values and the start of the
previous day.
t2 = datetime('now') + caldays(1:3)
t1 = datetime('yesterday')
t1 = datetime
26-Aug-2019
dt = t2 - t1
whos dt
dt 1x3 40 duration
View the elapsed durations in units of days by changing the Format property of dt.
dt.Format = 'd'
Scale the duration values by multiplying dt by a factor of 1.2. Because durations have an
exact length, you can multiply and divide them by fractional values.
dt2 = 1.2*dt
7-39
7 Dates and Time
Use the between function to find the number of calendar years, months, and days
elapsed between two dates.
t1 = datetime('today')
t1 = datetime
27-Aug-2019
t2 = t1 + calmonths(0:2) + caldays(4)
dt = between(t1,t2)
See Also
between | caldiff | diff
7-40
Compare Dates and Time
Compare two datetime arrays. The arrays must be the same size or one can be a scalar.
A = datetime(2013,07,26) + calyears(0:2:6)
B = datetime(2014,06,01)
B = datetime
01-Jun-2014
A < B
1 0 0 0
The < operator returns logical 1 (true) where a datetime in A occurs before a datetime in
B.
0 1 1 1
Comparisons of datetime arrays account for the time zone information of each array.
Compare September 1, 2014 at 4:00 p.m. in Los Angeles with 5:00 p.m. on the same day
in New York.
7-41
7 Dates and Time
A = datetime(2014,09,01,16,0,0,'TimeZone','America/Los_Angeles',...
'Format','dd-MMM-yyyy HH:mm:ss Z')
A = datetime
01-Sep-2014 16:00:00 -0700
B = datetime(2014,09,01,17,0,0,'TimeZone','America/New_York',...
'Format','dd-MMM-yyyy HH:mm:ss Z')
B = datetime
01-Sep-2014 17:00:00 -0400
A < B
ans = logical
0
4:00 p.m. in Los Angeles occurs after 5:00 p.m. on the same day in New York.
Compare Durations
B = duration([2,40,0;2,50,0])
A >= B
0
1
7-42
Compare Dates and Time
Compare a duration array to a numeric array. Elements in the numeric array are treated
as a number of fixed-length (24-hour) days.
1
0
Use the isbetween function to determine whether values in a datetime array lie within
a closed interval.
tlower = datetime(2014,08,01)
tlower = datetime
01-Aug-2014
tupper = datetime(2014,09,01)
tupper = datetime
01-Sep-2014
Create a datetime array and determine whether the values lie within the interval
bounded by t1 and t2.
A = datetime(2014,08,21) + calweeks(0:2)
tf = isbetween(A,tlower,tupper)
7-43
7 Dates and Time
1 1 0
See Also
isbetween
More About
• “Array Comparison with Relational Operators” on page 2-37
7-44
Plot Dates and Durations
Create t as a sequence of dates and create y as random data. Plot the vectors using the
plot function.
t = datetime(2014,6,28) + calweeks(0:9);
y = rand(1,10);
plot(t,y);
7-45
7 Dates and Time
By default, plot chooses tick mark locations based on the range of data. When you zoom
in and out of a plot, the tick labels automatically adjust to the new axis limits.
Change the x-axis limits. Also, change the format for the tick labels along the x-axis. For a
list of formatting options, see the xtickformat function.
7-46
Plot Dates and Durations
Create t as seven linearly spaced duration values between 0 and 3 minutes. Create y as a
vector of random data. Plot the data.
t = 0:seconds(30):minutes(3);
y = rand(1,7);
plot(t,y);
7-47
7 Dates and Time
View the x-axis limits. Since the duration tick labels are in terms of a single unit
(minutes), the limits are stored in terms of that unit.
xl = xlim
Change the format for the duration tick labels to display in the form of a digital timer that
includes more than one unit. For a list of formatting options, see the xtickformat
function.
xtickformat('mm:ss')
7-48
Plot Dates and Durations
View the x-axis limits again. Since the duration tick labels are now in terms of multiple
units, the limits are stored in units of 24-hour days.
xl = xlim
7-49
7 Dates and Time
t = datetime('today') + caldays(1:100);
y = linspace(10,40,100) + 10*rand(1,100);
scatter(t,y)
bar barh
plot plot3
7-50
See Also
See Also
datetime | plot | xtickformat
7-51
7 Dates and Time
This table lists notable MATLAB functions that operate on datetime, duration, and
calendarDuration arrays in addition to other arrays.
7-52
Convert Between Datetime Arrays, Numbers, and Text
Overview
datetime is the best data type for representing points in time. datetime values have
flexible display formats and up to nanosecond precision, and can account for time zones,
daylight saving time, and leap seconds. However, if you work with code authored in
MATLAB R2014a or earlier, or if you share code with others who use such a version, you
might need to work with dates and time stored in one of these three formats:
Example: 7.3510e+005
Date strings, vectors, and numbers can be stored as arrays of values. Store multiple date
strings in a cell array of character vectors, multiple date vectors in an m-by-6 matrix, and
multiple serial date numbers in a matrix.
You can convert any of these formats to a datetime array using the datetime function.
If your existing MATLAB code expects a serial date number or date vector, use the
7-53
7 Dates and Time
Starting in R2016b, you also can convert a datetime array to a string array with the
string function.
A date string includes characters that separate the fields, such as the hyphen, space, and
colon used here:
d = '23-Aug-2010 16:35:42'
Convert one or more date strings to a datetime array using the datetime function. For
best performance, specify the format of the input date strings as an input to datetime.
Note The specifiers that datetime uses to describe date and time formats differ from
the specifiers that the datestr, datevec, and datenum functions accept.
For a complete list of date and time format specifiers, see the Format property of the
datetime data type.
t = datetime(d,'InputFormat','dd-MMM-yyyy HH:mm:ss')
t =
datetime
23-Aug-2010 16:35:42
7-54
Convert Between Datetime Arrays, Numbers, and Text
Although the date string, d, and the datetime scalar, t, look similar, they are not equal.
View the size and data type of each variable.
whos d t
d 1x20 40 char
t 1x1 17 datetime
Convert a datetime array to a character vector using char or cellstr. For example,
convert the current date and time to a timestamp to append to a file name.
t = datetime('now','Format','yyyy-MM-dd''T''HHmmss')
t =
datetime
2017-01-03T151105
S = char(t);
filename = ['myTest_',S]
filename =
'myTest_2017-01-03T151105'
Convert a string array. MATLAB displays strings in double quotes. For best performance,
specify the format of the input date strings as an input to datetime.
str =
7-55
7 Dates and Time
"24-Oct-2016 11:58:17"
"19-Nov-2016 09:36:29"
"12-Dec-2016 10:09:06"
t = datetime(str,'InputFormat','dd-MMM-yyyy HH:mm:ss')
t =
24-Oct-2016 11:58:17
19-Nov-2016 09:36:29
12-Dec-2016 10:09:06
str =
"25-Dec-2016 06:12:34"
Convert one or more date vectors to a datetime array using the datetime function:
t = datetime([2012 10 24 10 45 07])
t =
7-56
Convert Between Datetime Arrays, Numbers, and Text
datetime
24-Oct-2012 10:45:07
Instead of using datevec to extract components of datetime values, use functions such as
year, month, and day instead:
y = year(t)
y =
2012
Alternatively, access the corresponding property, such as t.Year for year values:
y = t.Year
y =
2012
Serial time can represent fractions of days beginning at midnight; for example, 6 p.m.
equals 0.75 serial days. So the character vector '31-Oct-2003, 6:00 PM' in MATLAB
is date number 731885.75.
Convert one or more serial date numbers to a datetime array using the datetime
function. Specify the type of date number that is being converted:
t = datetime(731885.75,'ConvertFrom','datenum')
t =
datetime
31-Oct-2003 18:00:00
7-57
7 Dates and Time
numeric values. Then, call the function. For example, the log function accepts double
inputs, but not datetime inputs. Suppose that you have a datetime array of dates
spanning the course of a research study or experiment.
t = datetime(2014,6,18) + calmonths(1:4)
t =
Subtract the origin value. For example, the origin value might be the starting day of an
experiment.
dt = t - datetime(2014,7,1)
dt =
x = hours(dt)
x =
y = log(x)
y =
See Also
cellstr | char | datenum | datetime | datevec | duration | string
7-58
See Also
More About
• “Represent Dates and Times in MATLAB” on page 7-2
• “Extract or Assign Date and Time Components of Datetime Array” on page 7-28
• Proleptic Gregorian Calendar
7-59
7 Dates and Time
In the following example, the month element has a value of 22. MATLAB increments the
year value to 2010 and sets the month to October.
datestr([2009 22 03 00 00 00])
ans =
03-Oct-2010
The carrying forward of values also applies to time and day values in text representing
dates and times. For example, October 3, 2010 and September 33, 2010 are interpreted
to be the same date, and correspond to the same serial date number.
datenum('03-Oct-2010')
ans =
734414
datenum('33-Sep-2010')
ans =
734414
The following example takes the input month (07, or July), finds the last day of the
previous month (June 30), and subtracts the number of days in the field specifier (5 days)
from that date to yield a return date of June 25, 2010.
ans =
25-Jun-2010
7-60
Converting Date Vector Returns Unexpected Output
Consider a date vector that includes the year 3000. This year is outside the range of years
that datestr interprets as elements of date vectors. Therefore, the input is interpreted
as a 1-by-6 vector of serial date numbers:
datestr([3000 11 05 10 32 56])
ans =
18-Mar-0008
11-Jan-0000
05-Jan-0000
10-Jan-0000
01-Feb-0000
25-Feb-0000
Here datestr interprets 3000 as a serial date number, and converts it to the date string
'18-Mar-0008'. Also, datestr converts the next five elements to date strings.
When converting such a date vector to a character vector, first convert it to a serial date
number using datenum. Then, convert the date number to a character vector using
datestr:
dn = datenum([3000 11 05 10 32 56]);
ds = datestr(dn)
ds =
05-Nov-3000 10:32:56
When converting dates to character vectors, datestr interprets input as either date
vectors or serial date numbers using a heuristic rule. Consider an m-by-6 matrix. datestr
interprets the matrix as m date vectors when:
7-61
7 Dates and Time
If either condition is false, for any row, then datestr interprets the m-by-6 matrix as m-
by-6 serial date numbers.
Usually, dates with years in the range 1700–2300 are interpreted as date vectors.
However, datestr might interpret rows with month, day, hour, minute, or second values
outside their normal ranges as serial date numbers. For example, datestr correctly
interprets the following date vector for the year 2014:
datestr([2014 06 21 10 51 00])
ans =
21-Jun-2014 10:51:00
But given a day value outside the typical range (1–31), datestr returns a date for each
element of the vector:
ans =
06-Jul-0005
06-Jan-0000
10-Oct-0005
10-Jan-0000
20-Feb-0000
00-Jan-0000
When you have a matrix of date vectors that datestr might interpret incorrectly as
serial date numbers, first convert the matrix to serial date numbers using datenum. Then,
use datestr to convert the date numbers.
When you have a matrix of serial date numbers that datestr might interpret as date
vectors, first convert the matrix to a column vector. Then, use datestr to convert the
column vector.
7-62
8
Categorical Arrays
By default, categorical arrays contain categories that have no mathematical ordering. For
example, the discrete set of pet categories {'dog' 'cat' 'bird'} has no meaningful
mathematical ordering, so MATLAB® uses the alphabetical ordering {'bird' 'cat'
'dog'}. Ordinal categorical arrays contain categories that have a meaningful
mathematical ordering. For example, the discrete set of size categories {'small',
'medium', 'large'} has the mathematical ordering small < medium < large.
When you create categorical arrays from cell arrays of character vectors or string arrays,
leading and trailing spaces are removed. For example, if you specify the text {' cat' 'dog '}
as categories, then when you convert them to categories they become {'cat' 'dog'}.
You can use the categorical function to create a categorical array from a numeric
array, logical array, string array, cell array of character vectors, or an existing categorical
array.
Create a 1-by-11 cell array of character vectors containing state names from New
England.
state = {'MA','ME','CT','VT','ME','NH','VT','MA','NH','CT','RI'};
Convert the cell array, state, to a categorical array that has no mathematical order.
state = categorical(state)
MA ME CT VT ME NH VT MA NH
Columns 10 through 11
CT RI
8-2
Create Categorical Arrays
class(state)
ans =
'categorical'
categories(state)
Create a 1-by-8 cell array of character vectors containing the sizes of eight objects.
AllSizes = {'medium','large','small','small','medium',...
'large','medium','small'};
The cell array, AllSizes, has three distinct values: 'large', 'medium', and 'small'.
With the cell array of character vectors, there is no convenient way to indicate that small
< medium < large.
Convert the cell array, AllSizes, to an ordinal categorical array. Use valueset to
specify the values small, medium, and large, which define the categories. For an
ordinal categorical array, the first category specified is the smallest and the last category
is the largest.
valueset = {'small','medium','large'};
sizeOrd = categorical(AllSizes,valueset,'Ordinal',true)
8-3
8 Categorical Arrays
Columns 7 through 8
medium small
class(sizeOrd)
ans =
'categorical'
The order of the values in the categorical array, sizeOrd, remains unchanged.
The categories are listed in the specified order to match the mathematical ordering
small < medium < large.
Use the discretize function to create a categorical array by binning the values of x.
Put all values between zero and 15 in the first bin, all the values between 15 and 35 in the
second bin, and all the values between 35 and 50 in the third bin. Each bin includes the
left endpoint, but does not include the right endpoint.
catnames = {'small','medium','large'};
binnedData = discretize(x,[0 15 35 50],'categorical',catnames);
binnedData is a 100-by-1 ordinal categorical array with three categories, such that
small < medium < large.
Use the summary function to print the number of elements in each category.
summary(binnedData)
8-4
Create Categorical Arrays
small 30
medium 35
large 35
Starting in R2016b, you can create string arrays with the string function and convert
them to categorical array.
Add missing elements to str and convert it to a categorical array. Where str has missing
values, planets has undefined values.
str(8) = 'Mars'
Columns 7 through 8
<missing> "Mars"
planets = categorical(str)
8-5
8 Categorical Arrays
Columns 7 through 8
<undefined> Mars
See Also
categorical | categories | discretize | summary
Related Examples
• “Convert Text in Table Variables to Categorical” on page 8-7
• “Access Data Using Categorical Arrays” on page 8-30
• “Compare Categorical Array Elements” on page 8-20
More About
• “Advantages of Using Categorical Arrays” on page 8-43
• “Ordinal Categorical Arrays” on page 8-46
8-6
Convert Text in Table Variables to Categorical
load patients
whos
T = table(Age,Gender,Height,Weight,...
SelfAssessedHealthStatus,Location,...
'RowNames',LastName);
The cell arrays of character vectors, Gender and Location, contain discrete sets of
unique values.
T.Gender = categorical(T.Gender);
T.Location = categorical(T.Location);
8-7
8 Categorical Arrays
Print a Summary
View the data type, description, units, and other descriptive statistics for each variable by
using summary to summarize the table.
format compact
summary(T)
Variables:
Age: 100x1 double
Values:
Min 25
Median 39
Max 50
Gender: 100x1 categorical
Values:
Female 53
Male 47
Height: 100x1 double
Values:
Min 60
Median 67
Max 72
Weight: 100x1 double
Values:
Min 111
Median 142.5
Max 202
SelfAssessedHealthStatus: 100x1 ordinal categorical
Values:
Poor 11
Fair 15
Good 40
Excellent 34
Location: 100x1 categorical
8-8
Convert Text in Table Variables to Categorical
Values:
County General Hospital 39
St. Mary s Medical Center 24
VA Hospital 37
Create a subtable, T1, containing the age, height, and weight of all female patients who
were observed at County General Hospital. You can easily create a logical vector based on
the values in the categorical arrays Gender and Location.
rows = T.Location=='County General Hospital' & T.Gender=='Female';
rows is a 100-by-1 logical vector with logical true (1) for the table rows where the
gender is female and the location is County General Hospital.
T1=19×3 table
Age Height Weight
___ ______ ______
Brown 49 64 119
Taylor 31 66 132
Anderson 45 68 128
Lee 44 66 146
Walker 28 65 123
Young 25 63 114
Campbell 37 65 135
Evans 39 62 121
Morris 43 64 135
Rivera 29 63 130
Richardson 30 67 141
Cox 28 66 111
Torres 45 70 137
8-9
8 Categorical Arrays
Peterson 32 60 136
Ramirez 48 64 137
Bennett 35 64 131
⋮
A is a 19-by-3 table.
Since ordinal categorical arrays have a mathematical ordering for their categories, you
can perform element-wise comparisons of them with relational operations, such as
greater than and less than.
Create a subtable, T2, of the gender, age, height, and weight of all patients who assessed
their health status as poor or fair.
T2=26×4 table
Gender Age Height Weight
______ ___ ______ ______
Johnson Male 43 69 163
Jones Female 40 67 133
Thomas Female 42 66 137
Jackson Male 25 71 174
Garcia Female 27 69 131
Rodriguez Female 39 64 117
Lewis Female 41 62 137
Lee Female 44 66 146
Hall Male 25 70 189
Hernandez Male 36 68 166
Lopez Female 40 66 137
Gonzalez Female 35 66 118
Mitchell Male 39 71 164
Campbell Female 37 65 135
Parker Male 30 68 182
8-10
See Also
T2 is a 26-by-4 table.
See Also
Related Examples
• “Create and Work with Tables” on page 9-2
• “Create Categorical Arrays” on page 8-2
• “Access Data in Tables” on page 9-44
• “Access Data Using Categorical Arrays” on page 8-30
More About
• “Advantages of Using Categorical Arrays” on page 8-43
• “Ordinal Categorical Arrays” on page 8-46
8-11
8 Categorical Arrays
load patients
whos
The workspace variable, Location, is a cell array of character vectors that contains the
three unique medical facilities where patients were observed.
To access and compare data more easily, convert Location to a categorical array.
Location = categorical(Location);
summary(Location)
39 patients were observed at County General Hospital, 24 at St. Mary's Medical Center,
and 37 at the VA Hospital.
8-12
Plot Categorical Data
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus,...
{'Poor' 'Fair' 'Good' 'Excellent'},'Ordinal',true);
summary(SelfAssessedHealthStatus)
Poor 11
Fair 15
Good 40
Excellent 34
Plot Histogram
figure
histogram(SelfAssessedHealthStatus)
title('Self Assessed Health Status From 100 Patients')
8-13
8 Categorical Arrays
Create a histogram of the hospital location for only the patients who assessed their health
as Fair or Poor.
figure
histogram(Location(SelfAssessedHealthStatus<='Fair'))
title('Location of Patients in Fair or Poor Health')
8-14
Plot Categorical Data
figure
pie(SelfAssessedHealthStatus);
title('Self Assessed Health Status From 100 Patients')
8-15
8 Categorical Arrays
The function pie accepts the categorical array, SelfAssessedHealthStatus, and plots
a pie chart of the four categories.
Create a Pareto chart from the category counts for each of the four categories of
SelfAssessedHealthStatus.
figure
A = countcats(SelfAssessedHealthStatus);
C = categories(SelfAssessedHealthStatus);
pareto(A,C);
title('Self Assessed Health Status From 100 Patients')
8-16
Plot Categorical Data
The first input argument to pareto must be a vector. If a categorical array is a matrix or
multidimensional array, reshape it into a vector before calling countcats and pareto.
Female 53
Male 47
8-17
8 Categorical Arrays
Gender is a 100-by-1 categorical array with two categories, Female and Male.
Use the categorical array, Gender, to access Weight and Height data for each gender
separately.
X1 = Weight(Gender=='Female');
Y1 = Height(Gender=='Female');
X2 = Weight(Gender=='Male');
Y2 = Height(Gender=='Male');
X1 and Y1 are 53-by-1 numeric arrays containing data from the female patients.
X2 and Y2 are 47-by-1 numeric arrays containing data from the male patients.
Create a scatter plot of height vs. weight. Indicate data from the female patients with a
circle and data from the male patients with a cross.
figure
h1 = scatter(X1,Y1,'o');
hold on
h2 = scatter(X2,Y2,'x');
8-18
See Also
See Also
bar | categorical | countcats | histogram | pie | rose | scatter | summary
Related Examples
• “Access Data Using Categorical Arrays” on page 8-30
8-19
8 Categorical Arrays
colors = categorical(C)
categories(colors)
Use the relational operator, eq (==), to compare the first and second rows of colors.
colors(1,:) == colors(2,:)
1 0 1 1
Only the values in the second column differ between the rows.
8-20
Compare Categorical Array Elements
Compare the entire categorical array, colors, to the character vector 'blue' to find the
location of all blue values.
colors == 'blue'
1 0 0 1
1 0 0 1
There are four blue entries in colors, one in each corner of the array.
Add a mathematical ordering to the categories in colors. Specify the category order that
represents the ordering of color spectrum, red < green < blue.
categories(colors)
Determine if elements in the first column of colors are greater than the elements in the
second column.
8-21
8 Categorical Arrays
1
1
Both values in the first column, blue, are greater than the corresponding values in the
second column, red and green.
Find all the elements in colors that are less than 'blue'.
0 1 1 0
0 1 1 0
The function lt (<) indicates the location of all green and red values with 1.
See Also
categorical | categories
Related Examples
• “Access Data Using Categorical Arrays” on page 8-30
More About
• “Relational Operations”
• “Advantages of Using Categorical Arrays” on page 8-43
• “Ordinal Categorical Arrays” on page 8-46
8-22
Combine Categorical Arrays
A is a 25-by-1 categorical array with three distinct categories: milk, water, and juice.
milk 8
water 8
juice 9
Eight students in classroom A prefer milk, eight prefer water, and nine prefer juice.
milk 12
water 10
juice 6
Twelve students in classroom B prefer milk, ten prefer water, and six prefer juice.
Concatenate the data from classrooms A and B into a single categorical array, Group1.
8-23
8 Categorical Arrays
Group1 = [A;B];
milk 20
water 18
juice 15
Group1 is a 53-by-1 categorical array with three categories: milk, water, and juice.
Create a categorical array, Group2, containing data from 50 students who were given the
additional beverage option of soda.
Group2 = gallery('integerdata',4,[50,1],2);
Group2 = categorical(Group2,1:4,{'juice' 'milk' 'soda' 'water'});
juice 18
milk 10
soda 13
water 9
Group2 is a 50-by-1 categorical array with four categories: juice, milk, soda, and
water.
milk 30
water 27
juice 33
soda 13
8-24
See Also
Concatenation appends the categories exclusive to the second input, soda, to the end of
the list of categories from the first input, milk, water, juice, soda.
Use reordercats to change the order of the categories in the categorical array,
students.
students = reordercats(students,{'juice','milk','water','soda'});
categories(students)
Use the function union to find the unique responses from Group1 and Group2.
C = union(Group1,Group2)
union returns the combined values from Group1 and Group2 with no repetitions. In this
case, C is equivalent to the categories of the concatenation, students.
All of the categorical arrays in this example were nonordinal. To combine ordinal
categorical arrays, they must have the same sets of categories including their order.
See Also
cat | categorical | categories | horzcat | summary | union | vertcat
Related Examples
• “Create Categorical Arrays” on page 8-2
8-25
8 Categorical Arrays
More About
• “Ordinal Categorical Arrays” on page 8-46
8-26
Combine Categorical Arrays Using Multiplication
Combine two categorical arrays using times. The input arrays must have the same
number of elements, but can have different numbers of categories.
A = categorical({'blue','red','green'});
B = categorical({'+','-','+'});
C = A.*B
Show the categories of C. The categories are all the ordered pairs that can be created
from the categories of A and B, also known as the Cartesian product.
categories(C)
D = B.*A
8-27
8 Categorical Arrays
categories(D)
A = categorical({'blue','red','green','black'});
B = categorical({'+','-','+','-'});
A = removecats(A,{'black'});
C = A.*B
Combine two ordinal categorical arrays. C is an ordinal categorical array only if A and B
are both ordinal. The ordering of the categories of C follows from the orderings of the
input categorical arrays.
A = categorical({'blue','red','green'},{'green','red','blue'},'Ordinal',true);
B = categorical({'+','-','+'},'Ordinal',true);
C = A.*B;
categories(C)
8-28
See Also
{'blue +' }
{'blue -' }
See Also
categorical | categories | summary | times
Related Examples
• “Create Categorical Arrays” on page 8-2
• “Combine Categorical Arrays” on page 8-23
• “Access Data Using Categorical Arrays” on page 8-30
More About
• “Ordinal Categorical Arrays” on page 8-46
8-29
8 Categorical Arrays
• Select elements from particular categories. For categorical arrays, use the logical
operators == or ~= to select data that is in, or not in, a particular category. To select
data in a particular group of categories, use the ismember function.
For ordinal categorical arrays, use inequalities >, >=, <, or <= to find data in
categories above or below a particular category.
• Delete data that is in a particular category. Use logical operators to include or
exclude data from particular categories.
• Find elements that are not in a defined category. Categorical arrays indicate
which elements do not belong to a defined category by <undefined>. Use the
isundefined function to find observations without a defined value.
load patients
whos
8-30
Access Data Using Categorical Arrays
Gender and Location contain data that belong in categories. Each cell array contains
character vectors taken from a small set of unique values (indicating two genders and
three locations respectively). Convert Gender and Location to categorical arrays.
Gender = categorical(Gender);
Location = categorical(Location);
For categorical arrays, you can use the logical operators == and ~= to find the data that is
in, or not in, a particular category.
Determine if there are any patients observed at the location, 'Rampart General
Hospital'.
any(Location=='Rampart General Hospital')
ans = logical
0
You can use ismember to find data in a particular group of categories. Create a logical
vector for the patients observed at County General Hospital or VA Hospital.
VA_CountyGenIndex = ...
ismember(Location,{'County General Hospital','VA Hospital'});
8-31
8 Categorical Arrays
VA_CountyGenIndex is a 100-by-1 logical array containing logical true (1) for each
element in the categorical array Location that is a member of the category County
General Hospital or VA Hospital. The output, VA_CountyGenIndex contains 76
nonzero elements.
Use the logical vector, VA_CountyGenIndex to select the LastName of the patients
observed at either County General Hospital or VA Hospital.
VA_CountyGenPatients = LastName(VA_CountyGenIndex);
Use the summary function to print a summary containing the category names and the
number of elements in each category.
summary(Location)
summary(Gender)
Female 53
Male 47
Use logical operator == to access the age of only the female patients. Then plot a
histogram of this data.
figure()
histogram(Age(Gender=='Female'))
title('Age of Female Patients')
8-32
Access Data Using Categorical Arrays
You can use logical operators to include or exclude data from particular categories.
Delete all patients observed at VA Hospital from the workspace variables, Age and
Location.
Now, Age is a 63-by-1 numeric array, and Location is a 63-by-1 categorical array.
List the categories of Location, as well as the number of elements in each category.
8-33
8 Categorical Arrays
summary(Location)
The patients observed at VA Hospital are deleted from Location, but VA Hospital is
still a category.
categories(Location)
Delete Element
You can delete elements by indexing. For example, you can remove the first element of
Location by selecting the rest of the elements with Location(2:end). However, an
easier way to delete elements is to use [].
Location(1) = [];
summary(Location)
Location is a 62-by-1 categorical array that has two categories. Deleting the first
element has no effect on other elements from the same category and does not delete the
category itself.
8-34
Access Data Using Categorical Arrays
Location(1:8)
After removing the category, County General Hospital, elements that previously
belonged to that category no longer belong to any category defined for Location.
Categorical arrays denote these elements as undefined.
Use the function isundefined to find observations that do not belong to any category.
undefinedIndex = isundefined(Location);
undefinedIndex is a 62-by-1 categorical array containing logical true (1) for all
undefined elements in Location.
Use the summary function to print the number of undefined elements in Location.
summary(Location)
The first element of Location belongs to the category, St. Mary's Medical Center.
Set the first element to be undefined so that it no longer belongs to any category.
Location(1) = '<undefined>';
summary(Location)
8-35
8 Categorical Arrays
You can make selected elements undefined without removing a category or changing
the categories of other elements. Set elements to be undefined to indicate elements
with values that are unknown.
You can use undefined elements to preallocate the size of a categorical array for better
performance. Create a categorical array that has elements with known locations only.
definedIndex = ~isundefined(Location);
newLocation = Location(definedIndex);
summary(newLocation)
Expand the size of newLocation so that it is a 200-by-1 categorical array. Set the last
new element to be undefined. All of the other new elements also are set to be
undefined. The 23 original elements keep the values they had.
newLocation(200) = '<undefined>';
summary(newLocation)
newLocation has room for values you plan to store in the array later.
See Also
any | categorical | categories | histogram | isundefined | removecats |
summary
Related Examples
• “Create Categorical Arrays” on page 8-2
• “Convert Text in Table Variables to Categorical” on page 8-7
• “Plot Categorical Data” on page 8-12
• “Compare Categorical Array Elements” on page 8-20
• “Work with Protected Categorical Arrays” on page 8-38
8-36
See Also
More About
• “Advantages of Using Categorical Arrays” on page 8-43
• “Ordinal Categorical Arrays” on page 8-46
8-37
8 Categorical Arrays
When you create a categorical array with the categorical function, you have the option
of specifying whether or not the categories are protected. Ordinal categorical arrays
always have protected categories, but you also can create a nonordinal categorical array
that is protected using the 'Protected',true name-value pair argument.
When you assign values that are not in the array's list of categories, the array updates
automatically so that its list of categories includes the new values. Similarly, you can
combine (nonordinal) categorical arrays that have different categories. The categories in
the result include the categories from both arrays.
When you assign new values to a protected categorical array, the values must belong to
one of the existing categories. Similarly, you can only combine protected arrays that have
the same categories.
• If you want to combine two nonordinal categorical arrays that have protected
categories, they must have the same categories, but the order does not matter. The
resulting categorical array uses the category order from the first array.
• If you want to combine two ordinal categorical array (that always have protected
categories), they must have the same categories, including their order.
To add new categories to the array, you must use the function addcats.
Create a categorical array containing the sizes of 10 objects. Use the names small,
medium, and large for the values 'S', 'M', and 'L'.
A = categorical({'M';'L';'S';'S';'M';'L';'M';'L';'M';'S'},...
{'S','M','L'},{'small','medium','large'},'Ordinal',true)
8-38
Work with Protected Categorical Arrays
large
medium
small
When you create an ordinal categorical array, the categories are always protected.
Use the isprotected function to verify that the categories of A are protected.
tf = isprotected(A)
tf = logical
1
If you try to assign a new value that does not belong to one of the existing categories,
then MATLAB® returns an error. For example, you cannot assign the value 'xlarge' to
the categorical array, as in the expression A(2) = 'xlarge', because xlarge is not a
category of A. Instead, MATLAB® returns the error:
8-39
8 Categorical Arrays
To add a new category for xlarge, use the addcats function. Since A is ordinal you must
specify the order for the new category.
A = addcats(A,'xlarge','After','large');
A(2) = 'xlarge'
A is now a 10-by-1 categorical array with four categories, such that small < medium <
large < xlarge.
Create another ordinal categorical array, B, containing the sizes of five items.
B = categorical([2;1;1;2;2],1:2,{'xsmall','small'},'Ordinal',true)
B is a 5-by-1 categorical array with two categories such that xsmall < small.
To combine two ordinal categorical arrays (which always have protected categories), they
must have the same categories and the categories must be in the same order.
8-40
Work with Protected Categorical Arrays
A = addcats(A,'xsmall','Before','small');
categories(A)
B = addcats(B,{'medium','large','xlarge'},'After','small');
categories(B)
The categories of A and B are now the same including their order.
C = [A;B]
8-41
8 Categorical Arrays
xsmall
small
small
categories(C)
C is a 16-by-1 ordinal categorical array with five categories, such that xsmall < small
< medium < large < xlarge.
See Also
addcats | categorical | categories | isordinal | isprotected | summary
Related Examples
• “Create Categorical Arrays” on page 8-2
• “Convert Text in Table Variables to Categorical” on page 8-7
• “Access Data Using Categorical Arrays” on page 8-30
• “Combine Categorical Arrays” on page 8-23
• “Combine Categorical Arrays Using Multiplication” on page 8-27
More About
• “Ordinal Categorical Arrays” on page 8-46
8-42
Advantages of Using Categorical Arrays
An ordering other than alphabetical order is not possible with character arrays or cell
arrays of character vectors. Thus, inequality comparisons, such as greater and less than,
are not possible. With categorical arrays, you can use relational operations to test for
equality and perform element-wise comparisons that have a meaningful mathematical
ordering.
8-43
8 Categorical Arrays
defined as character vectors, which can be costly to store and manipulate in a cell array
of character vectors or char array. Categorical arrays store only one copy of each
category name, often reducing the amount of memory required to store the array.
state = [repmat({'MA'},25,1);repmat({'NY'},25,1);...
repmat({'CA'},50,1);...
repmat({'MA'},25,1);repmat({'NY'},25,1)];
whos state
The variable state is a cell array of character vectors requiring 17,400 bytes of memory.
state = categorical(state);
categories(state)
whos state
8-44
See Also
See Also
categorical | categories
Related Examples
• “Create Categorical Arrays” on page 8-2
• “Convert Text in Table Variables to Categorical” on page 8-7
• “Compare Categorical Array Elements” on page 8-20
• “Access Data Using Categorical Arrays” on page 8-30
More About
• “Ordinal Categorical Arrays” on page 8-46
8-45
8 Categorical Arrays
In this section...
“Order of Categories” on page 8-46
“How to Create Ordinal Categorical Arrays” on page 8-46
“Working with Ordinal Categorical Arrays” on page 8-48
Order of Categories
categorical is a data type to store data with values from a finite set of discrete
categories, which can have a natural order. You can specify and rearrange the order of
categories in all categorical arrays. However, you only can treat ordinal categorical arrays
as having a mathematical ordering to their categories. Use an ordinal categorical array if
you want to use the functions min, max, or relational operations, such as greater than and
less than.
The discrete set of pet categories {'dog' 'cat' 'bird'} has no meaningful
mathematical ordering. You are free to use any category order and the meaning of the
associated data does not change. For example, pets =
categorical({'bird','cat','dog','dog','cat'}) creates a categorical array
and the categories are listed in alphabetical order, {'bird' 'cat' 'dog'}. You can
choose to specify or change the order of the categories to {'dog' 'cat' 'bird'} and
the meaning of the data does not change.
8-46
Ordinal Categorical Arrays
Create an ordinal categorical array, sizes, from a cell array of character vectors, A. Use
valueset, specified as a vector of unique values, to define the categories for sizes.
sizes = categorical(A,valueset,'Ordinal',true)
sizes is 3-by-2 ordinal categorical array with three categories such that small <
medium < large. The order of the values in valueset becomes the order of the
categories of sizes.
Create an equivalent categorical array from an array of integers. Use the values 1, 2, and
3 to define the categories small, medium, and large, respectively.
A2 = [2 3; 1 2; 3 1];
valueset = 1:3;
catnames = {'small','medium','large'};
sizes2 = categorical(A2,valueset,catnames,'Ordinal',true)
isequal(sizes,sizes2)
ans = logical
1
8-47
8 Categorical Arrays
sizes and sizes2 are equivalent categorical arrays with the same ordering of
categories.
Create a nonordinal categorical array from the cell array of character vectors, A.
sizes3 = categorical(A)
isordinal(sizes3)
ans = logical
0
Convert sizes3 to an ordinal categorical array, such that small < medium < large.
sizes3 = categorical(sizes3,{'small','medium','large'},'Ordinal',true);
sizes3 is now a 3-by-2 ordinal categorical array equivalent to sizes and sizes2.
8-48
See Also
See Also
categorical | categories | isequal | isordinal
Related Examples
• “Create Categorical Arrays” on page 8-2
• “Convert Text in Table Variables to Categorical” on page 8-7
• “Compare Categorical Array Elements” on page 8-20
• “Access Data Using Categorical Arrays” on page 8-30
More About
• “Advantages of Using Categorical Arrays” on page 8-43
8-49
8 Categorical Arrays
The following table lists notable MATLAB functions that operate on categorical arrays in
addition to other arrays.
8-50
9
Tables
Tables are suitable for column-oriented or tabular data that are often stored as columns in
a text file or in a spreadsheet. Each variable in a table can have a different data type, but
must have the same number of rows. However, variables in a table are not restricted to
column vectors. For example, a table variable can contain a matrix with multiple columns
as long as it has the same number of rows as the other table variables. A typical use for a
table is to store experimental data, where rows represent different observations and
columns represent different measured variables.
Tables are convenient containers for collecting and organizing related data variables and
for viewing and summarizing data. For example, you can extract variables to perform
calculations and conveniently add the results as new table variables. When you finish your
calculations, write the table to a file to save your results.
Create a table from workspace variables and view it. Alternatively, use the Import Tool or
the readtable function to create a table from a spreadsheet or a text file. When you
import data from a file using these functions, each column becomes a table variable.
Load sample data for 100 patients from the patients MAT-file to workspace variables.
load patients
whos
9-2
Create and Work with Tables
Populate a table with column-oriented variables that contain patient data. You can access
and assign table variables by name. When you assign a table variable from a workspace
variable, you can assign the table variable a different name.
Create a table and populate it with the Gender, Smoker, Height, and Weight workspace
variables. Display the first five rows.
T = table(Gender,Smoker,Height,Weight);
T(1:5,:)
ans=5×4 table
Gender Smoker Height Weight
__________ ______ ______ ______
As an alternative, use the readtable function to read the patient data from a comma-
delimited file. readtable reads all the columns that are in a file.
ans=5×10 table
LastName Gender Age Location Height Wei
____________ __________ ___ _____________________________ ______ ___
You can assign more column-oriented table variables using dot notation, T.varname,
where T is the table and varname is the desired variable name. Create identifiers that are
random numbers. Then assign them to a table variable, and name the table variable ID.
All the variables you assign to a table must have the same number of rows. Display the
first five rows of T.
9-3
9 Tables
T.ID = randi(1e4,100,1);
T(1:5,:)
ans=5×5 table
Gender Smoker Height Weight ID
__________ ______ ______ ______ ____
All the variables you assign to a table must have the same number of rows.
View the data type, description, units, and other descriptive statistics for each variable by
creating a table summary using the summary function.
summary(T)
Variables:
Values:
True 34
False 66
Values:
Min 60
Median 67
Max 72
Values:
Min 111
9-4
Create and Work with Tables
Median 142.5
Max 202
Values:
Min 120
Median 5485.5
Max 9706
size(T)
ans = 1×2
100 5
Create a new, smaller table containing the first five rows of T and display it. You can use
numeric indexing within parentheses to specify rows and variables. This method is similar
to indexing into numeric arrays to create subarrays. Tnew is a 5-by-5 table.
Tnew = T(1:5,:)
Tnew=5×5 table
Gender Smoker Height Weight ID
__________ ______ ______ ______ ____
Create a smaller table containing all rows of Tnew and the variables from the second to
the last. Use the end keyword to indicate the last variable or the last row of a table. Tnew
is a 5-by-4 table.
Tnew = Tnew(:,2:end)
9-5
9 Tables
Tnew=5×4 table
Smoker Height Weight ID
______ ______ ______ ____
Add row names to T and index into the table using row and variable names instead of
numeric indices. Add row names by assigning the LastName workspace variable to the
RowNames property of T.
T.Properties.RowNames = LastName;
T(1:5,:)
ans=5×5 table
Gender Smoker Height Weight ID
__________ ______ ______ ______ ____
Return the size of T. The size does not change because row and variable names are not
included when calculating the size of a table.
size(T)
ans = 1×2
100 5
9-6
Create and Work with Tables
Select all the data for the patients with the last names 'Smith' and 'Johnson'. In this
case, it is simpler to use the row names than to use numeric indices. Tnew is a 2-by-5
table.
Tnew = T({'Smith','Johnson'},:)
Tnew=2×5 table
Gender Smoker Height Weight ID
________ ______ ______ ______ ____
Select the height and weight of the patient named 'Johnson' by indexing on variable
names. Tnew is a 1-by-2 table.
Tnew = T('Johnson',{'Height','Weight'})
Tnew=1×2 table
Height Weight
______ ______
Johnson 69 163
You can access table variables either with dot syntax, as in T.Height, or by named
indexing, as in T(:,'Height').
You can access the contents of table variables, and then perform calculations on them
using MATLAB® functions. Calculate body-mass-index (BMI) based on data in the existing
table variables and add it as a new variable. Plot the relationship of BMI to a patient's
status as a smoker or a nonsmoker. Add blood-pressure readings to the table, and plot the
relationship of blood pressure to BMI.
Calculate BMI using the table variables, Weight and Height. You can extract Weight
and Height for the calculation while conveniently keeping Weight, Height, and BMI in
the table with the rest of the patient data. Display the first five rows of T.
T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;
T(1:5,:)
9-7
9 Tables
ans=5×6 table
Gender Smoker Height Weight ID BMI
__________ ______ ______ ______ ____ ______
Populate the variable units and variable descriptions properties for BMI. You can add
metadata to any table variable to describe further the data contained in the variable.
T.Properties.VariableUnits{'BMI'} = 'kg/m^2';
T.Properties.VariableDescriptions{'BMI'} = 'Body Mass Index';
Create a histogram to explore whether there is a relationship between smoking and body-
mass-index in this group of patients. You can index into BMI with the logical values from
the Smoker table variable, because each row contains BMI and Smoker values for the
same patient.
tf = (T.Smoker == false);
h1 = histogram(T.BMI(tf),'BinMethod','integers');
hold on
tf = (T.Smoker == true);
h2 = histogram(T.BMI(tf),'BinMethod','integers');
xlabel('BMI (kg/m^2)');
ylabel('Number of Patients');
legend('Nonsmokers','Smokers');
title('BMI Distributions for Smokers and Nonsmokers');
hold off
9-8
Create and Work with Tables
Add blood pressure readings for the patients from the workspace variables Systolic and
Diastolic. Each row contains Systolic, Diastolic, and BMI values for the same
patient.
T.Systolic = Systolic;
T.Diastolic = Diastolic;
9-9
9 Tables
h2 = histogram(T.Diastolic(tf),'BinMethod','integers');
xlabel('Diastolic Reading (mm Hg)');
ylabel('Number of Patients');
legend('BMI <= 25','BMI > 25');
title('Diastolic Readings for Low and High BMI');
hold off
To prepare the table for output, reorder the table rows by name, and table variables by
position or name. Display the final arrangement of the table.
Sort the table by row names so that patients are listed in alphabetical order.
9-10
Create and Work with Tables
T = sortrows(T,'RowNames');
T(1:5,:)
ans=5×8 table
Gender Smoker Height Weight ID BMI Systolic
__________ ______ ______ ______ ____ ______ ________
Delete Systolic and Diastolic from the table since they are redundant.
T.Systolic = [];
T.Diastolic = [];
T(1:5,:)
ans=5×7 table
Gender Smoker Height Weight ID BMI BloodPre
__________ ______ ______ ______ ____ ______ ________
T(1:5,:)
ans=5×7 table
ID Gender Smoker Height Weight BMI BloodPre
9-11
9 Tables
You also can reorder table variables by name. To reorder the table variables so that
Gender is last:
varnames = T.Properties.VariableNames;
others = ~strcmp('Gender',varnames);
varnames = [varnames(others) 'Gender'];
T = T(:,varnames);
T(1:5,:)
ans=5×7 table
ID Smoker Height Weight BMI BloodPressure Gen
____ ______ ______ ______ ______ _____________ _____
You can write the entire table to a file, or create a subtable to write a selected portion of
the original table to a separate file.
writetable(T,'allPatientsBMI.txt');
9-12
See Also
You can use the readtable function to read the data in allPatientsBMI.txt into a
new table.
Create a subtable and write the subtable to a separate file. Delete the rows that contain
data on patients who are smokers. Then remove the Smoker variable. nonsmokers
contains data only for the patients who are not smokers.
nonsmokers = T;
toDelete = (nonsmokers.Smoker == true);
nonsmokers(toDelete,:) = [];
nonsmokers.Smoker = [];
writetable(nonsmokers,'nonsmokersBMI.txt');
See Also
Import Tool | array2table | cell2table | readtable | sortrows | struct2table |
summary | table | writetable
Related Examples
• “Clean Messy and Missing Data in Tables” on page 9-27
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
• “Access Data in Tables” on page 9-44
More About
• “Advantages of Using Tables” on page 9-74
9-13
9 Tables
load patients
T = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
size(T)
ans = 1×2
100 8
T2 = readtable('morePatients.csv');
Tnew = [T;T2];
size(Tnew)
ans = 1×2
104 8
The table Tnew has 104 rows. In order to vertically concatenate two tables, both tables
must have the same number of variables, with the same variable names. If the variable
names are different, you can directly assign new rows in a table to rows from another
table. For example, T(end+1:end+4,:) = T2.
To append new rows stored in a cell array, vertically concatenate the cell array onto the
end of the table. You can concatenate directly from a cell array when it has the right
9-14
Add and Delete Table Rows
number of columns and the contents of its cells can be concatenated onto the
corresponding table variables.
cellPatients = {'Edwards','Male',42,70,158,0,116,83;
'Falk','Female',28,62,125,1,120,71};
Tnew = [Tnew;cellPatients];
size(Tnew)
ans = 1×2
106 8
You also can convert a cell array to a table using the cell2table function.
You also can append new rows stored in a structure. Convert the structure to a table, and
then concatenate the tables.
structPatients(1,1).LastName = 'George';
structPatients(1,1).Gender = 'Male';
structPatients(1,1).Age = 45;
structPatients(1,1).Height = 76;
structPatients(1,1).Weight = 182;
structPatients(1,1).Smoker = 1;
structPatients(1,1).Systolic = 132;
structPatients(1,1).Diastolic = 85;
structPatients(2,1).LastName = 'Hadley';
structPatients(2,1).Gender = 'Female';
structPatients(2,1).Age = 29;
structPatients(2,1).Height = 58;
structPatients(2,1).Weight = 120;
structPatients(2,1).Smoker = 0;
structPatients(2,1).Systolic = 112;
structPatients(2,1).Diastolic = 70;
Tnew = [Tnew;struct2table(structPatients)];
size(Tnew)
ans = 1×2
108 8
9-15
9 Tables
To omit any rows in a table that are duplicated, use the unique function.
Tnew = unique(Tnew);
size(Tnew)
ans = 1×2
106 8
Tnew([18,20,21],:) = [];
size(Tnew)
ans = 1×2
103 8
First, specify the variable of identifiers, LastName, as row names. Then, delete the
variable, LastName, from Tnew. Finally, use the row name to index and delete rows.
Tnew.Properties.RowNames = Tnew.LastName;
Tnew.LastName = [];
Tnew('Smith',:) = [];
size(Tnew)
ans = 1×2
102 7
The table now has one less row and one less variable.
9-16
See Also
You also can search for observations in the table. For example, delete rows for any
patients under the age of 30.
ans = 1×2
85 7
See Also
array2table | cell2table | readtable | struct2table | table
Related Examples
• “Add, Delete, and Rearrange Table Variables” on page 9-18
• “Clean Messy and Missing Data in Tables” on page 9-27
9-17
9 Tables
You also can modify table variables using the Variables Editor.
Load arrays of sample data from the patients MAT-file. Display the names and sizes of
the variables loaded into the workspace.
load patients
whos -file patients
Create two tables. Create one table, T, with information collected from a patient
questionnaire and create another table, T2, with data measured from patients. Each table
has 100 rows.
T = table(Age,Gender,Smoker);
T2 = table(Height,Weight,Systolic,Diastolic);
head(T,5)
9-18
Add, Delete, and Rearrange Table Variables
ans=5×3 table
Age Gender Smoker
___ __________ ______
38 {'Male' } true
43 {'Male' } false
38 {'Female'} false
40 {'Female'} false
49 {'Female'} false
head(T2,5)
ans=5×4 table
Height Weight Systolic Diastolic
______ ______ ________ _________
71 176 124 93
69 163 109 77
64 131 125 83
67 133 117 75
64 119 122 80
ans=5×7 table
Age Gender Smoker Height Weight Systolic Diastolic
___ __________ ______ ______ ______ ________ _________
9-19
9 Tables
If the tables that you are horizontally concatenating have row names, horzcat
concatenates the tables by matching the row names. Therefore, the tables must use the
same row names, but the row order does not matter.
Add the names of patients from the workspace variable LastName before the first table
variable in T. You can specify any location in the table using the name of a variable near
the new location. Use quotation marks to refer to the names of table variables. However,
do not use quotation marks for input arguments that are workspace variables.
T = addvars(T,LastName,'Before','Age');
head(T,5)
ans=5×8 table
LastName Age Gender Smoker Height Weight Systolic Dias
____________ ___ __________ ______ ______ ______ ________ ____
You also can specify locations in a table using numbers. For example, the equivalent
syntax using a number to specify location is T = addvars(T,LastName,'Before',1).
An alternative way to add new table variables is to use dot syntax. When you use dot
syntax, you always add the new variable as the last table variable. You can add a variable
that has any data type, as long as it has the same number of rows as the table.
Create a new variable for blood pressure as a horizontal concatenation of the two
variables Systolic and Diastolic. Add it to T.
ans=5×9 table
LastName Age Gender Smoker Height Weight Systolic Dias
____________ ___ __________ ______ ______ ______ ________ ____
9-20
Add, Delete, and Rearrange Table Variables
T now has 9 variables and 100 rows. A table variable can have multiple columns. So
although BloodPressure has two columns, it is one table variable.
Add a new variable, BMI, in the table T, that contains the body mass index for each
patient. BMI is a function of height and weight. When you calculate BMI, you can refer to
the Weight and Height variables that are in T.
T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;
The operators ./ and .^ in the calculation of BMI indicate element-wise division and
exponentiation, respectively.
ans=5×10 table
LastName Age Gender Smoker Height Weight Systolic Dias
____________ ___ __________ ______ ______ ______ ________ ____
Move the table variable BMI using the movevars function, so that it is after the variable
Weight. When you specify table variables by name, use quotation marks.
T = movevars(T,'BMI','After','Weight');
head(T,5)
ans=5×10 table
LastName Age Gender Smoker Height Weight BMI Systol
____________ ___ __________ ______ ______ ______ ______ ______
9-21
9 Tables
You also can specify locations in a table using numbers. For example, the equivalent
syntax using a number to specify location is T = movevars(T,'BMI,'After',6). It is
often more convenient to refer to variables by name.
As an alternative, you can move table variables by indexing. You can index into a table
using the same syntax you use for indexing into a matrix.
ans=5×10 table
LastName Age Gender Smoker Height Weight BMI BloodP
____________ ___ __________ ______ ______ ______ ______ ______
In a table with many variables, it is often more convenient to use the movevars function.
Delete Variables
To delete table variables, use the removevars function. Delete the Systolic and
Diastolic table variables.
T = removevars(T,{'Systolic','Diastolic'});
head(T,5)
ans=5×8 table
LastName Age Gender Smoker Height Weight BMI BloodP
9-22
Add, Delete, and Rearrange Table Variables
As an alternative, you can delete variables using dot syntax and the empty matrix, [].
Remove the Age variable from the table.
T.Age = [];
head(T,5)
ans=5×7 table
LastName Gender Smoker Height Weight BMI BloodPressure
____________ __________ ______ ______ ______ ______ _____________
You also can delete variables using indexing and the empty matrix, []. Remove the
Gender variable from the table.
T(:,'Gender') = [];
head(T,5)
ans=5×6 table
LastName Smoker Height Weight BMI BloodPressure
____________ ______ ______ ______ ______ _____________
9-23
9 Tables
To split multicolumn table variables into variables that each have one column, use the
splitvars functions. Split the variable BloodPressure into two variables.
T = splitvars(T,'BloodPressure','NewVariableNames',{'Systolic','Diastolic'});
head(T,5)
ans=5×7 table
LastName Smoker Height Weight BMI Systolic Diastolic
____________ ______ ______ ______ ______ ________ _________
Similarly, you can group related table variables together in one variable, using the
mergevars function. Combine Systolic and Diastolic back into one variable, and
name it BP.
T = mergevars(T,{'Systolic','Diastolic'},'NewVariableName','BP');
head(T,5)
ans=5×6 table
LastName Smoker Height Weight BMI BP
____________ ______ ______ ______ ______ __________
You can reorient the rows of a table or timetable, so that they become the variables the
output table, using the rows2vars function. However, if the table has multicolumn
variables, then you must split them before you can call rows2vars.
9-24
See Also
Reorient the rows of T. Specify that the names of the patients in T are the names of table
variables in the output table. The first variable of T3 contains the names of the variables
of T. Each remaining variable of T3 contains the data from the corresponding row of T.
T = splitvars(T,'BP','NewVariableNames',{'Systolic','Diastolic'});
T3 = rows2vars(T,'VariableNamesSource','LastName');
T3(:,1:5)
ans=6×5 table
OriginalVariableNames Smith Johnson Williams Jones
_____________________ ______ _______ ________ ______
{'Smoker' } 1 0 0 0
{'Height' } 71 69 64 67
{'Weight' } 176 163 131 133
{'BMI' } 24.547 24.071 22.486 20.831
{'Systolic' } 124 109 125 117
{'Diastolic'} 93 77 83 75
You can use dot syntax with T3 to access patient data as an array. However, if the row
values of an input table cannot be concatenated, then the variables of the output table are
cell arrays.
T3.Smith
ans = 6×1
1.0000
71.0000
176.0000
24.5467
124.0000
93.0000
See Also
addvars | inner2outer | mergevars | movevars | removevars | rows2vars |
splitvars | table
9-25
9 Tables
Related Examples
• “Add and Delete Table Rows” on page 9-14
• “Clean Messy and Missing Data in Tables” on page 9-27
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
9-26
Clean Messy and Missing Data in Tables
Load sample data from a comma-separated text file, messy.csv. The file contains many
different missing data indicators:
To specify the character vectors to treat as empty values, use the 'TreatAsEmpty'
name-value pair argument with the readtable function. (Use the disp function to
display all 21 rows, even when running this example as a live script.)
T = readtable('messy.csv','TreatAsEmpty',{'.','NA'});
disp(T)
A B C D E
________ ____ __________ ____ ____
{'afe1'} 3 {'yes' } 3 3
{'egh3'} NaN {'no' } 7 7
{'wth4'} 3 {'yes' } 3 3
{'atn2'} 23 {'no' } 23 23
{'arg1'} 5 {'yes' } 5 5
{'jre3'} 34.6 {'yes' } 34.6 34.6
{'wen9'} 234 {'yes' } 234 234
{'ple2'} 2 {'no' } 2 2
{'dbo8'} 5 {'no' } 5 5
{'oii4'} 5 {'yes' } 5 5
{'wnk3'} 245 {'yes' } 245 245
{'abk6'} 563 {0x0 char} 563 563
{'pnj5'} 463 {'no' } 463 463
{'wnn3'} 6 {'no' } 6 6
{'oks9'} 23 {'yes' } 23 23
{'wba3'} NaN {'yes' } NaN 14
{'pkn4'} 2 {'no' } 2 2
9-27
9 Tables
{'adw3'} 22 {'no' } 22 22
{'poj2'} -99 {'yes' } -99 -99
{'bas8'} 23 {'no' } 23 23
{'gry5'} NaN {'yes' } NaN 21
T is a table with 21 rows and five variables. 'TreatAsEmpty' only applies to numeric
columns in the file and cannot handle numeric values specified as text, such as '-99'.
Summarize Table
View the data type, description, units, and other descriptive statistics for each variable by
creating a table summary using the summary function.
summary(T)
Variables:
B: 21x1 double
Values:
Min -99
Median 14
Max 563
NumMissing 3
D: 21x1 double
Values:
Min -99
Median 7
Max 563
NumMissing 2
E: 21x1 double
Values:
Min -99
9-28
Clean Messy and Missing Data in Tables
Median 14
Max 563
When you import data from a file, the default is for readtable to read any variables with
nonnumeric elements as a cell array of character vectors.
Display the subset of rows from the table, T, that have at least one missing value.
A B C D E
________ ___ __________ ___ ___
readtable replaced '.' and 'NA' with NaN in the numeric variables, B, D, and E.
Clean the data so that the missing values indicated by code -99 have the standard
MATLAB® numeric missing value indicator, NaN.
T = standardizeMissing(T,-99);
disp(T)
A B C D E
________ ____ __________ ____ ____
{'afe1'} 3 {'yes' } 3 3
{'egh3'} NaN {'no' } 7 7
{'wth4'} 3 {'yes' } 3 3
{'atn2'} 23 {'no' } 23 23
{'arg1'} 5 {'yes' } 5 5
{'jre3'} 34.6 {'yes' } 34.6 34.6
{'wen9'} 234 {'yes' } 234 234
{'ple2'} 2 {'no' } 2 2
{'dbo8'} 5 {'no' } 5 5
9-29
9 Tables
{'oii4'} 5 {'yes' } 5 5
{'wnk3'} 245 {'yes' } 245 245
{'abk6'} 563 {0x0 char} 563 563
{'pnj5'} 463 {'no' } 463 463
{'wnn3'} 6 {'no' } 6 6
{'oks9'} 23 {'yes' } 23 23
{'wba3'} NaN {'yes' } NaN 14
{'pkn4'} 2 {'no' } 2 2
{'adw3'} 22 {'no' } 22 22
{'poj2'} NaN {'yes' } NaN NaN
{'bas8'} 23 {'no' } 23 23
{'gry5'} NaN {'yes' } NaN 21
Create a new table, T2, and replace missing values with values from previous rows of the
table. fillmissing provides a number of ways to fill in missing values.
T2 = fillmissing(T,'previous');
disp(T2)
A B C D E
________ ____ _______ ____ ____
{'afe1'} 3 {'yes'} 3 3
{'egh3'} 3 {'no' } 7 7
{'wth4'} 3 {'yes'} 3 3
{'atn2'} 23 {'no' } 23 23
{'arg1'} 5 {'yes'} 5 5
{'jre3'} 34.6 {'yes'} 34.6 34.6
{'wen9'} 234 {'yes'} 234 234
{'ple2'} 2 {'no' } 2 2
{'dbo8'} 5 {'no' } 5 5
{'oii4'} 5 {'yes'} 5 5
{'wnk3'} 245 {'yes'} 245 245
{'abk6'} 563 {'yes'} 563 563
{'pnj5'} 463 {'no' } 463 463
{'wnn3'} 6 {'no' } 6 6
{'oks9'} 23 {'yes'} 23 23
{'wba3'} 23 {'yes'} 23 14
{'pkn4'} 2 {'no' } 2 2
{'adw3'} 22 {'no' } 22 22
{'poj2'} 22 {'yes'} 22 22
{'bas8'} 23 {'no' } 23 23
{'gry5'} 23 {'yes'} 23 21
9-30
Clean Messy and Missing Data in Tables
Create a new table, T3, that contains only the rows from T without missing values. T3 has
only 16 rows.
T3 = rmmissing(T);
disp(T3)
A B C D E
________ ____ _______ ____ ____
{'afe1'} 3 {'yes'} 3 3
{'wth4'} 3 {'yes'} 3 3
{'atn2'} 23 {'no' } 23 23
{'arg1'} 5 {'yes'} 5 5
{'jre3'} 34.6 {'yes'} 34.6 34.6
{'wen9'} 234 {'yes'} 234 234
{'ple2'} 2 {'no' } 2 2
{'dbo8'} 5 {'no' } 5 5
{'oii4'} 5 {'yes'} 5 5
{'wnk3'} 245 {'yes'} 245 245
{'pnj5'} 463 {'no' } 463 463
{'wnn3'} 6 {'no' } 6 6
{'oks9'} 23 {'yes'} 23 23
{'pkn4'} 2 {'no' } 2 2
{'adw3'} 22 {'no' } 22 22
{'bas8'} 23 {'no' } 23 23
Organize Data
Sort the rows of T3 in descending order by C, and then sort in ascending order by A.
T3 = sortrows(T2,{'C','A'},{'descend','ascend'});
disp(T3)
A B C D E
________ ____ _______ ____ ____
9-31
9 Tables
{'oii4'} 5 {'yes'} 5 5
{'oks9'} 23 {'yes'} 23 23
{'poj2'} 22 {'yes'} 22 22
{'wba3'} 23 {'yes'} 23 14
{'wen9'} 234 {'yes'} 234 234
{'wnk3'} 245 {'yes'} 245 245
{'wth4'} 3 {'yes'} 3 3
{'adw3'} 22 {'no' } 22 22
{'atn2'} 23 {'no' } 23 23
{'bas8'} 23 {'no' } 23 23
{'dbo8'} 5 {'no' } 5 5
{'egh3'} 3 {'no' } 7 7
{'pkn4'} 2 {'no' } 2 2
{'ple2'} 2 {'no' } 2 2
{'pnj5'} 463 {'no' } 463 463
{'wnn3'} 6 {'no' } 6 6
In C, the rows are grouped first by 'yes', followed by 'no'. Then in A, the rows are
listed alphabetically.
A C B D E
________ _______ ____ ____ ____
9-32
See Also
{'pkn4'} {'no' } 2 2 2
{'ple2'} {'no' } 2 2 2
{'pnj5'} {'no' } 463 463 463
{'wnn3'} {'no' } 6 6 6
See Also
fillmissing | ismissing | readtable | rmmissing | sortrows |
standardizeMissing | summary
Related Examples
• “Add and Delete Table Rows” on page 9-14
• “Add, Delete, and Rearrange Table Variables” on page 9-18
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
• “Access Data in Tables” on page 9-44
• “Missing Data in MATLAB”
9-33
9 Tables
load patients
BloodPressure = [Systolic Diastolic];
T = table(Gender,Age,Height,Weight,Smoker,BloodPressure);
T(1:5,:)
ans=5×6 table
Gender Age Height Weight Smoker BloodPressure
__________ ___ ______ ______ ______ _____________
Specify units for each variable in the table by modifying the table property,
VariableUnits. Specify the variable units as a cell array of character vectors.
An individual empty character vector within the cell array indicates that the
corresponding variable does not have units.
9-34
Modify Units, Descriptions, and Table Variable Names
Add a variable description for the variable, BloodPressure. Assign a single character
vector to the element of the cell array containing the description for BloodPressure.
T.Properties.VariableDescriptions{'BloodPressure'} = 'Systolic/Diastolic';
You can use the variable name, 'BloodPressure', or the numeric index of the variable,
6, to index into the cell array of character vectors containing the variable descriptions.
View the data type, description, units, and other descriptive statistics for each variable by
using summary to summarize the table.
summary(T)
Variables:
Properties:
Units: Yrs
Values:
Min 25
Median 39
Max 50
Properties:
Units: In
Values:
Min 60
Median 67
Max 72
Properties:
Units: Lbs
9-35
9 Tables
Values:
Min 111
Median 142.5
Max 202
Values:
True 34
False 66
Properties:
Description: Systolic/Diastolic
Values:
BloodPressure_1 BloodPressure_2
_______________ _______________
Min 109 68
Median 122 81.5
Max 138 99
The BloodPressure variable has a description and the Age, Height, Weight, and
BloodPressure variables have units.
Change the variable name for the first variable from Gender to Sex.
T.Properties.VariableNames{'Gender'} = 'Sex';
T(1:5,:)
ans=5×6 table
Sex Age Height Weight Smoker BloodPressure
__________ ___ ______ ______ ______ _____________
9-36
See Also
In addition to properties for variable units, descriptions and names, there are table
properties for row and dimension names, a table description, and user data.
See Also
array2table | cell2table | readtable | struct2table | summary | table
Related Examples
• “Add, Delete, and Rearrange Table Variables” on page 9-18
• “Access Data in Tables” on page 9-44
9-37
9 Tables
All tables and timetables have properties that contain metadata about them or their
variables. You can access these properties through the T.Properties object, where T is
the name of the table or timetable. For example, T.Properties.VariableNames
returns a cell array containing the names of the variables of T.
The properties you access through T.Properties are part of the definitions of the
table and timetable data types. You cannot add or remove these predefined
properties. But starting in R2018b, you can add and remove your own custom properties,
by modifying the T.Properties.CustomProperties object of a table or timetable.
Add Properties
Read power outage data into a table. Sort it using the first variable that contains dates
and times, OutageTime. Then display the first three rows.
T = readtable('outages.csv');
T = sortrows(T,'OutageTime');
head(T,3)
ans=3×6 table
Region OutageTime Loss Customers RestorationTime
_____________ ________________ ______ __________ ________________ __
Display its properties. These are the properties that all tables have in common. Note that
there is also a CustomProperties object, but that by default it has no properties.
T.Properties
ans =
TableProperties with properties:
Description: ''
UserData: []
9-38
Add Custom Properties to Tables and Timetables
To add custom properties, use the addprop function. Specify the names of the properties.
For each property, also specify whether it has metadata for the whole table (similar to the
Description property) or for its variables (similar to the VariableNames property). If
the property has variable metadata, then its value must be a vector whose length is equal
to the number of variables.
Add custom properties that contain an output file name, file type, and indicators of which
variables to plot. Best practice is to assign the input table as the output argument of
addprop, so that the custom properties are part of the same table. Specify that the
output file name and file type are table metadata using the 'table' option. Specify that
the plot indicators are variable metadata using the 'variable' option.
T = addprop(T,{'OutputFileName','OutputFileType','ToPlot'}, ...
{'table','table','variable'});
T.Properties
ans =
TableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {1x6 cell}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
9-39
9 Tables
When you add custom properties using addprop, their values are empty arrays by
default. You can set and access the values of the custom properties using dot syntax.
Set the output file name and type. These properties contain metadata for the table. Then
assign a logical array to the ToPlot property. This property contains metadata for the
variables. In this example, the elements of the value of the ToPlot property are true for
each variable to be included in a plot, and false for each variable to be excluded.
T.Properties.CustomProperties.OutputFileName = 'outageResults';
T.Properties.CustomProperties.OutputFileType = '.mat';
T.Properties.CustomProperties.ToPlot = [false false true true true false];
T.Properties
ans =
TableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {1x6 cell}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
Plot variables from T in a stacked plot using the stackedplot function. To plot only the
Loss, Customers, and RestorationTime values, use the ToPlot custom property as
the second input argument.
stackedplot(T,T.Properties.CustomProperties.ToPlot);
9-40
Add Custom Properties to Tables and Timetables
When you move or delete table variables, both the predefined and custom properties are
reordered so that their values correspond to the same variables. In this example, the
values of the ToPlot custom property stay aligned with the variables marked for plotting,
just as the values of the VariableNames predefined property stay aligned.
T.Customers = [];
T.Properties
ans =
TableProperties with properties:
Description: ''
9-41
9 Tables
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {1x5 cell}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
Convert the table to a timetable, using the outage times as row times. Move Region to
the end of the table, and RestorationTime before the first variable, using the
movevars function. Note that the properties are reordered appropriately. The
RestorationTime and Loss variables still have indicators for inclusion in a plot.
T = table2timetable(T);
T = movevars(T,'Region','After','Cause');
T = movevars(T,'RestorationTime','Before',1);
T.Properties
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'OutageTime' 'Variables'}
VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowTimes: [1468x1 datetime]
StartTime: 2002-02-01 12:18
SampleRate: NaN
TimeStep: NaN
9-42
See Also
Remove Properties
You can remove any or all of the custom properties of a table using the rmprop function.
However, you cannot use it to remove predefined properties from T.Properties,
because those properties are part of the definition of the table data type.
T = rmprop(T,{'OutputFileName','OutputFileType'});
T.Properties
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'OutageTime' 'Variables'}
VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowTimes: [1468x1 datetime]
StartTime: 2002-02-01 12:18
SampleRate: NaN
TimeStep: NaN
See Also
addprop | head | movevars | readtable | rmprop | sortrows | stackedplot | table
| table2timetable
Related Examples
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
• “Access Data in Tables” on page 9-44
• “Add, Delete, and Rearrange Table Variables” on page 9-18
9-43
9 Tables
A table is a container that stores column-oriented data in variables. Table variables can
have different data types and sizes as long as all variables have the same number of rows.
Table variables have names, just as the fields of a structure have names. The rows of a
table can have names, but row names are not required. To access table data, index into
the rows and variables using either their names or numeric indices.
• Smooth parentheses, (), returns a table that has selected rows and variables.
• Dot notation returns the contents of a variable as an array.
• Curly braces, {}, returns an array concatenated from the contents of selected rows
and variables.
You can specify rows and variables by name, numeric index, or data type. Starting in
R2019b, variable names and row names can include any characters, including spaces and
non-ASCII characters. Also, they can start with any characters, not just letters. Variable
and row names do not have to be valid MATLAB identifiers (as determined by the
isvarname function).
9-44
Access Data in Tables
9-45
9 Tables
Array
extracted
from the first
table variable
9-46
Access Data in Tables
First five
rows of array
extracted
from the first
table variable
9-47
9 Tables
9-48
Access Data in Tables
Create a table and populate it with the Age, Gender, Height, Weight, and Smoker
workspace variables. Use the unique identifiers in LastName as row names. T is a 100-
by-5 table. (When you specify row names, they do not count as a table variable).
T = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName)
T=100×5 table
Age Gender Height Weight Smoker
___ __________ ______ ______ ______
9-49
9 Tables
Create a subtable containing the first five rows and all the variables from T. To specify the
desired rows and variables, use numeric indices within parentheses. This type of indexing
is similar to indexing into numeric arrays.
T1 = T(1:5,:)
T1=5×5 table
Age Gender Height Weight Smoker
___ __________ ______ ______ ______
T1 is a 5-by-5 table.
In addition to numeric indices, you can use row or variable names inside the parentheses.
(In this case, using row indices and a colon is more compact than using row or variable
names.)
Select all the data for the patients with the last names 'Williams' and 'Brown'. Since
T has row names that are the last names of patients, index into T using row names.
9-50
Access Data in Tables
T2 = T({'Williams','Brown'},:)
T2=2×5 table
Age Gender Height Weight Smoker
___ __________ ______ ______ ______
T2 is a 2-by-5 table.
You also can select variables by name. Create a table that has only the first five rows of T
and the Height and Weight variables. Display it.
T3 = T(1:5,{'Height','Weight'})
T3=5×2 table
Height Weight
______ ______
Smith 71 176
Johnson 69 163
Williams 64 131
Jones 67 133
Brown 64 119
Table variable names do not have to be valid MATLAB identifiers. They can include spaces
and non-ASCII characters, and can start with any character.
Add a variable name with spaces and a dash to T. Then index into T using variable names.
T = addvars(T,SelfAssessedHealthStatus,'NewVariableNames','Self-Assessed Health Status'
T(1:5,{'Age','Smoker','Self-Assessed Health Status'})
ans=5×3 table
Age Smoker Self-Assessed Health Status
___ ______ ___________________________
9-51
9 Tables
Instead of specifying variables using names or numbers, you can create a data type
subscript that matches all variables having the same data type.
S = vartype('numeric')
S =
table vartype subscript:
Create a table that has only the numeric variables, and only the first five rows, from T.
T4 = T(1:5,S)
T4=5×3 table
Age Height Weight
___ ______ ______
Smith 38 71 176
Johnson 43 69 163
Williams 38 64 131
Jones 40 67 133
Brown 49 64 119
load patients
T = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName);
9-52
Access Data in Tables
To extract data from one variable, use dot notation. Extract numeric values from the
variable Weight. Then plot a histogram of those values.
histogram(T.Weight)
title('Patient Weight')
9-53
9 Tables
You can index into an array or a table using an array of logical indices. Typically, you use
a logical expression that determines which values in a table variable meet a condition.
The result of the expression is an array of logical indices.
For example, create logical indices matching patients whose age is less than 40.
1
0
1
0
0
0
1
0
1
1
⋮
To extract heights for patients whose age is less than 40, index into the Height variable
using rows. There are 56 patients younger than 40.
T.Height(rows)
ans = 56×1
71
64
64
68
66
71
72
65
69
69
⋮
9-54
Access Data in Tables
You can index into a table with logical indices. Display the rows of T for the patients who
are younger than 40.
T(rows,:)
ans=56×5 table
Age Gender Height Weight Smoker
___ __________ ______ ______ ______
You can match multiple conditions with one logical expression. Display the rows for
smoking patients younger than 40.
ans=18×5 table
Age Gender Height Weight Smoker
___ __________ ______ ______ ______
9-55
9 Tables
• By name, without quotation marks. For example, T.Date specifies a variable named
'Date'.
• By an expression, where the expression is enclosed by parentheses after the dot. For
example, T.('Start Date') specifies a variable named 'Start Date'.
Use the first syntax when a table variable name also happens to be a valid MATLAB®
identifier. (A valid identifier starts with a letter and includes only letters, digits, and
underscores.)
For example, create a table from the patients MAT-file. Then use dot notation to access
the contents of table variables.
load patients
T = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName);
To specify a variable by position in the table, use a number. Age is the first variable in T,
so use the number 1 to specify its position.
9-56
Access Data in Tables
T.(1)
ans = 100×1
38
43
38
40
49
46
33
40
28
31
⋮
To specify a variable by name, you can enclose it in quotation marks. Since 'Age' is a
valid identifier, you can specify it using either T.Age or T.('Age').
T.('Age')
ans = 100×1
38
43
38
40
49
46
33
40
28
31
⋮
You can specify table variable names that are not valid MATLAB identifiers. Variable
names can include spaces and non-ASCII characters, and can start with any character.
However, when you use dot notation to access a table variable with such a name, you
must specify it using parentheses.
9-57
9 Tables
ans=5×6 table
Age Gender Height Weight Smoker Self-Assessed Health
___ __________ ______ ______ ______ ____________________
Access the new table variable using dot notation. Display the first five elements.
You also can use the output of a function as a variable name. Delete the T.('Self-
Assessed Health Status') variable. Then replace it with a variable whose name
includes today's date.
ans=5×6 table
Age Gender Height Weight Smoker 26-Aug-2019 Self Rep
___ __________ ______ ______ ______ ____________________
9-58
Access Data in Tables
Create a table from numeric and logical arrays from the patients file.
load patients
T = table(Age,Height,Weight,Smoker,...
'RowNames',LastName);
Extract data from multiple variables in T. Unlike dot notation, indexing with curly braces
can extract values from multiple table variables and concatenate them into one array.
Extract the height and weight for the first five patients. Use numeric indices to select the
first five rows, and variable names to select the variables Height and Weight.
A = T{1:5,{'Height','Weight'}}
A = 5×2
71 176
69 163
64 131
67 133
64 119
If you specify one variable name, then curly brace indexing results in the same array you
can get with dot notation. However, you must specify both rows and variables when you
use curly brace indexing. For example, this syntaxes T.Height and T{:,'Height'}
return the same array.
9-59
9 Tables
If all the table variables have data types that allow them to be concatenated together,
then you can use the T.Variables syntax to put all the table data into an array. This
syntax is equivalent to T{:,:} where the colons indicate all rows and all variables.
A2 = T.Variables
A2 = 100×4
38 71 176 1
43 69 163 0
38 64 131 0
40 67 133 0
49 64 119 0
46 68 142 0
33 64 142 1
40 68 180 0
28 68 183 0
31 66 132 0
⋮
See Also
addvars | histogram | table | vartype
Related Examples
• “Advantages of Using Tables” on page 9-74
• “Create and Work with Tables” on page 9-2
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
• “Calculations on Tables” on page 9-61
• “Find Array Elements That Meet a Condition” on page 5-2
9-60
Calculations on Tables
Calculations on Tables
This example shows how to perform calculations on tables.
The functions rowfun and varfun each apply a specified function to a table, yet many
other functions require numeric or homogeneous arrays as input arguments. You can
extract data from individual variables using dot indexing or from one or more variables
using curly braces. The extracted data is then an array that you can use as input to other
functions.
Read data from a comma-separated text file, testScores.csv, into a table using the
readtable function. testScores.csv contains test scores for several students. Use the
student names in the first column of the text file as row names in the table.
T = readtable('testScores.csv','ReadRowNames',true)
T=10×4 table
Gender Test1 Test2 Test3
__________ _____ _____ _____
HOWARD {'male' } 90 87 93
WARD {'male' } 87 85 83
TORRES {'male' } 86 85 88
PETERSON {'female'} 75 80 72
GRAY {'female'} 89 86 87
RAMIREZ {'female'} 96 92 98
JAMES {'male' } 78 75 77
WATSON {'female'} 91 94 92
BROOKS {'female'} 86 83 85
KELLY {'male' } 79 76 82
View the data type, description, units, and other descriptive statistics for each variable by
using the summary function to summarize the table.
summary(T)
Variables:
9-61
9 Tables
Values:
Min 75
Median 86.5
Max 96
Values:
Min 75
Median 85
Max 94
Values:
Min 72
Median 86
Max 98
The summary contains the minimum, median, and maximum score for each test.
Extract the data from the second, third, and fourth variables using curly braces, {}, find
the average of each row, and store it in a new variable, TestAvg.
T.TestAvg = mean(T{:,2:end},2)
T=10×5 table
Gender Test1 Test2 Test3 TestAvg
__________ _____ _____ _____ _______
HOWARD {'male' } 90 87 93 90
WARD {'male' } 87 85 83 85
TORRES {'male' } 86 85 88 86.333
PETERSON {'female'} 75 80 72 75.667
GRAY {'female'} 89 86 87 87.333
RAMIREZ {'female'} 96 92 98 95.333
9-62
Calculations on Tables
ans=2×3 table
Gender GroupCount mean_TestAvg
__________ __________ ____________
{'female'} 5 87.067
{'male' } 5 83.4
The maximum score for each test is 100. Use curly braces to extract the data from the
table and convert the test scores to a 25 point scale.
T{:,2:end} = T{:,2:end}*25/100
T=10×5 table
Gender Test1 Test2 Test3 TestAvg
__________ _____ _____ _____ _______
9-63
9 Tables
T.Properties.VariableNames{end} = 'Final'
T=10×5 table
Gender Test1 Test2 Test3 Final
__________ _____ _____ _____ ______
See Also
findgroups | rowfun | splitapply | summary | table | varfun
Related Examples
• “Access Data in Tables” on page 9-44
• “Split Table Data Variables and Apply Functions” on page 9-69
9-64
Split Data into Groups and Calculate Statistics
Split the patients into nonsmokers and smokers using the Smoker variable. Calculate the
mean weight for each group.
[G,smoker] = findgroups(Smoker);
meanWeight = splitapply(@mean,Weight,G)
meanWeight = 2×1
149.9091
161.9412
9-65
9 Tables
The findgroups function returns G, a vector of group numbers created from Smoker.
The splitapply function uses G to split Weight into two groups. splitapply applies
the mean function to each group and concatenates the mean weights into a vector.
findgroups returns a vector of group identifiers as the second output argument. The
group identifiers are logical values because Smoker contains logical values. The patients
in the first group are nonsmokers, and the patients in the second group are smokers.
smoker
0
1
Split the patient weights by both gender and status as a smoker and calculate the mean
weights.
G = findgroups(Gender,Smoker);
meanWeight = splitapply(@mean,Weight,G)
meanWeight = 4×1
130.3250
130.9231
180.0385
181.1429
The unique combinations across Gender and Smoker identify four groups of patients:
female nonsmokers, female smokers, male nonsmokers, and male smokers. Summarize
the four groups and their mean weights in a table.
[G,gender,smoker] = findgroups(Gender,Smoker);
T = table(gender,smoker,meanWeight)
T=4×3 table
gender smoker meanWeight
______ ______ __________
9-66
Split Data into Groups and Calculate Statistics
T.gender contains categorical values, and T.smoker contains logical values. The data
types of these table variables match the data types of Gender and Smoker respectively.
Calculate body mass index (BMI) for the four groups of patients. Define a function that
takes Height and Weight as its two input arguments, and that calculates BMI.
meanBMIfcn = @(h,w)mean((w ./ (h.^2)) * 703);
BMI = splitapply(meanBMIfcn,Height,Weight,G)
BMI = 4×1
21.6721
21.6686
26.5775
26.4584
Calculate the fraction of patients who report their health as either Poor or Fair. First,
use splitapply to count the number of patients in each group: female nonsmokers,
female smokers, male nonsmokers, and male smokers. Then, count only those patients
who report their health as either Poor or Fair, using logical indexing on S and G. From
these two sets of counts, calculate the fraction for each group.
[G,gender,smoker] = findgroups(Gender,Smoker);
S = SelfAssessedHealthStatus;
I = ismember(S,{'Poor','Fair'});
numPatients = splitapply(@numel,S,G);
numPF = splitapply(@numel,S(I),G(I));
numPF./numPatients
ans = 4×1
0.2500
0.3846
0.3077
0.1429
Compare the standard deviation in Diastolic readings of those patients who report
Poor or Fair health, and those patients who report Good or Excellent health.
9-67
9 Tables
stdDiastolicPF = splitapply(@std,Diastolic(I),G(I));
stdDiastolicGE = splitapply(@std,Diastolic(~I),G(~I));
Collect results in a table. For these patients, the female nonsmokers who report Poor or
Fair health show the widest variation in blood pressure readings.
T = table(gender,smoker,numPatients,numPF,stdDiastolicPF,stdDiastolicGE,BMI)
T=4×7 table
gender smoker numPatients numPF stdDiastolicPF stdDiastolicGE BM
______ ______ ___________ _____ ______________ ______________ ___
See Also
findgroups | splitapply
Related Examples
• “Split Table Data Variables and Apply Functions” on page 9-69
More About
• “Grouping Variables To Split Data” on page 9-81
9-68
Split Table Data Variables and Apply Functions
The sample file, outages.csv, contains data representing electric utility outages in the
United States. The file contains six columns: Region, OutageTime, Loss, Customers,
RestorationTime, and Cause. Read outages.csv into a table.
T = readtable('outages.csv');
ans=5×6 table
Region OutageTime Loss Customers RestorationTime C
_________ ________________ ______ __________ ________________ ______
Determine the greatest power loss due to a power outage in each region. The
findgroups function returns G, a vector of group numbers created from T.Region. The
splitapply function uses G to split T.Loss into five groups, corresponding to the five
regions. splitapply applies the max function to each group and concatenates the
maximum power losses into a vector.
G = findgroups(T.Region);
maxLoss = splitapply(@max,T.Loss,G)
9-69
9 Tables
maxLoss = 5×1
104 ×
2.3141
2.3418
0.8767
0.2796
1.6659
Calculate the maximum power loss due to a power outage by cause. To specify that Cause
is the grouping variable, use table indexing. Create a table that contains the maximum
power losses and their causes.
T1 = T(:,'Cause');
[G,powerLosses] = findgroups(T1);
powerLosses.maxLoss = splitapply(@max,T.Loss,G)
powerLosses=10×2 table
Cause maxLoss
________________ _______
attack 582.63
earthquake 258.18
energy emergency 11638
equipment fault 16659
fire 872.96
severe storm 8767.3
thunder storm 23418
unknown 23141
wind 2796
winter storm 2883.7
powerLosses is a table because T1 is a table. You can append the maximum losses as
another table variable.
Calculate the maximum power loss by cause in each region. To specify that Region and
Cause are the grouping variables, use table indexing. Create a table that contains the
maximum power losses and display the first 15 rows.
T1 = T(:,{'Region','Cause'});
[G,powerLosses] = findgroups(T1);
powerLosses.maxLoss = splitapply(@max,T.Loss,G);
powerLosses(1:15,:)
9-70
Split Table Data Variables and Apply Functions
ans=15×3 table
Region Cause maxLoss
_________ ________________ _______
MidWest attack 0
MidWest energy emergency 2378.7
MidWest equipment fault 903.28
MidWest severe storm 6808.7
MidWest thunder storm 15128
MidWest unknown 23141
MidWest wind 2053.8
MidWest winter storm 669.25
NorthEast attack 405.62
NorthEast earthquake 0
NorthEast energy emergency 11638
NorthEast equipment fault 794.36
NorthEast fire 872.96
NorthEast severe storm 6002.4
NorthEast thunder storm 23418
ans=15×4 table
Region Cause maxLoss totalCustomers
_________ ________________ _______ ______________
MidWest attack 0 0
MidWest energy emergency 2378.7 6.3363e+05
MidWest equipment fault 903.28 1.7822e+05
MidWest severe storm 6808.7 1.3511e+07
MidWest thunder storm 15128 4.2563e+06
MidWest unknown 23141 3.9505e+06
MidWest wind 2053.8 1.8796e+06
MidWest winter storm 669.25 4.8887e+06
NorthEast attack 405.62 2181.8
NorthEast earthquake 0 0
9-71
9 Tables
Determine the mean durations of all U.S. power outages in hours. Add the mean durations
of power outages to powerLosses. Because T.RestorationTime has NaT values, omit
the resulting NaN values when calculating the mean durations.
D = T.RestorationTime - T.OutageTime;
H = hours(D);
omeanFcn = @(x)(mean(x,'omitnan'));
powerLosses.meanOutage = splitapply(omeanFcn,H,G);
powerLosses(1:15,:)
ans=15×5 table
Region Cause maxLoss totalCustomers meanOutage
_________ ________________ _______ ______________ __________
See Also
findgroups | rowfun | splitapply | varfun
9-72
See Also
Related Examples
• “Access Data in Tables” on page 9-44
• “Calculations on Tables” on page 9-61
• “Split Data into Groups and Calculate Statistics” on page 9-65
More About
• “Grouping Variables To Split Data” on page 9-81
9-73
9 Tables
You can use the table data type to collect mixed-type data and metadata properties, such
as variable name, row names, descriptions, and variable units, in a single container.
Tables are suitable for column-oriented or tabular data that is often stored as columns in
a text file or in a spreadsheet. For example, you can use a table to store experimental
data, with rows representing different observations and columns representing different
measured variables.
Tables consist of rows and column-oriented variables. Each variable in a table can have a
different data type and a different size, but each variable must have the same number of
rows.
load patients
Then, combine the workspace variables, Systolic and Diastolic into a single
BloodPressure variable and convert the workspace variable, Gender, from a cell array
of character vectors to a categorical array.
whos('Gender','Age','Smoker','BloodPressure')
The variables Age, BloodPressure, Gender, and Smoker have varying data types and
are candidates to store in a table since they all have the same number of rows, 100.
Now, create a table from the variables and display the first five rows.
T = table(Gender,Age,Smoker,BloodPressure);
T(1:5,:)
9-74
Advantages of Using Tables
ans=5×4 table
Gender Age Smoker BloodPressure
______ ___ ______ _____________
The table displays in a tabular format with the variable names at the top.
Each variable in a table is a single data type. If you add a new row to the table,
MATLAB® forces consistency of the data type between the new data and the
corresponding table variables. For example, if you try to add information for a new
patient where the first column contains the patient's age instead of gender, as in the
expression T(end+1,:) = {37,{'Female'},true,[130 84]}, then you receive the
error:
The error occurs because MATLAB® cannot assign numeric data, 37, to the categorical
array, Gender.
For comparison of tables with structures, consider the structure array, StructArray,
that is equivalent to the table, T.
StructArray = table2struct(T)
StructArray=100×4 struct
Gender
Age
Smoker
BloodPressure
Structure arrays organize records using named fields. Each field's value can have a
different data type or size. Now, display the named fields for the first element of
StructArray.
StructArray(1)
9-75
9 Tables
Age: 38
Smoker: 1
BloodPressure: [124 93]
Fields in a structure array are analogous to variables in a table. However, unlike with
tables, you cannot enforce homogeneity within a field. For example, you can have some
values of S.Gender that are categorical array elements, Male or Female, others that are
character vectors, 'Male' or 'Female', and others that are integers, 0 or 1.
Now consider the same data stored in a scalar structure, with four fields each containing
one variable from the table.
ScalarStruct = struct(...
'Gender',{Gender},...
'Age',Age,...
'Smoker',Smoker,...
'BloodPressure',BloodPressure)
Unlike with tables, you cannot enforce that the data is rectangular. For example, the field
ScalarStruct.Age can be a different length than the other fields.
A table allows you to maintain the rectangular structure (like a structure array) and
enforce homogeneity of variables (like fields in a scalar structure). Although cell arrays do
not have named fields, they have many of the same disadvantages as structure arrays and
scalar structures. If you have rectangular data that is homogeneous in each variable,
consider using a table. Then you can use numeric or named indexing, and you can use
table properties to store metadata.
You can index into a table using parentheses, curly braces, or dot indexing. Parentheses
allow you to select a subset of the data in a table and preserve the table container. Curly
braces and dot indexing allow you to extract data from a table. Within each table indexing
method, you can specify the rows or variables to access by name or by numeric index.
9-76
Advantages of Using Tables
Consider the sample table from above. Each row in the table, T, represents a different
patient. The workspace variable, LastName, contains unique identifiers for the 100 rows.
Add row names to the table by setting the RowNames property to LastName and display
the first five rows of the updated table.
T.Properties.RowNames = LastName;
T(1:5,:)
ans=5×4 table
Gender Age Smoker BloodPressure
______ ___ ______ _____________
In addition to labeling the data, you can use row and variable names to access data in the
table. For example, use named indexing to display the age and blood pressure of the
patients Williams and Brown.
T({'Williams','Brown'},{'Age','BloodPressure'})
ans=2×2 table
Age BloodPressure
___ _____________
Williams 38 125 83
Brown 49 122 80
Now, use numeric indexing to return an equivalent subtable. Return the third and fifth
row from the second and fourth variables.
T(3:2:5,2:2:4)
ans=2×2 table
Age BloodPressure
___ _____________
Williams 38 125 83
Brown 49 122 80
9-77
9 Tables
With cell arrays or structures, you do not have the same flexibility to use named or
numeric indexing.
• With a cell array, you must use strcmp to find desired named data, and then you can
index into the array.
• With a scalar structure or structure array, it is not possible to refer to a field by
number. Furthermore, with a scalar structure, you cannot easily select a subset of
variables or a subset of observations. With a structure array, you can select a subset of
observations, but you cannot select a subset of variables.
• With a table, you can access data by named index or by numeric index. Furthermore,
you can easily select a subset of variables and a subset of rows.
For more information on table indexing, see “Access Data in Tables” on page 9-44.
In addition to storing data, tables have properties to store metadata, such as variable
names, row names, descriptions, and variable units. You can access a property using
T.Properties.PropName, where T is the name of the table and PropName is one of the
table properties.
For example, add a table description, variable descriptions, and variable units for Age.
T.Properties.Description = 'Simulated Patient Data';
T.Properties.VariableDescriptions = ...
{'Male or Female' ...
'' ...
'true or false' ...
'Systolic/Diastolic'};
T.Properties.VariableUnits{'Age'} = 'Yrs';
Individual empty character vectors within the cell array for VariableDescriptions
indicate that the corresponding variable does not have a description. For more
information, see the Properties section of table.
9-78
Advantages of Using Tables
Variables:
Properties:
Description: Male or Female
Values:
Female 53
Male 47
Properties:
Units: Yrs
Values:
Min 25
Median 39
Max 50
Properties:
Description: true or false
Values:
True 34
False 66
Properties:
Description: Systolic/Diastolic
Values:
BloodPressure_1 BloodPressure_2
_______________ _______________
Min 109 68
Median 122 81.5
Max 138 99
9-79
9 Tables
Structures and cell arrays do not have properties for storing metadata.
See Also
summary | table
Related Examples
• “Create and Work with Tables” on page 9-2
• “Modify Units, Descriptions, and Table Variable Names” on page 9-34
• “Access Data in Tables” on page 9-44
9-80
Grouping Variables To Split Data
Grouping Variables
Grouping variables are variables used to group, or categorize, observations—that is, data
values in other variables. A grouping variable can be any of these data types:
Data variables are the variables that contain observations. A grouping variable must have
a value corresponding to each value in the data variables. Data values belong to the same
group when the corresponding values in the grouping variable are the same.
This table shows examples of data variables, grouping variables, and the groups that you
can create when you split the data variables using the grouping variables.
You can give groups of data meaningful names when you use cell arrays of character
vectors or categorical arrays as grouping variables. A categorical array is an efficient and
flexible choice of grouping variable.
9-81
9 Tables
Group Definition
Typically, there are as many groups as there are unique values in the grouping variable.
(A categorical array also can include categories that are not represented in the data.) The
groups and the order of the groups depend on the data type of the grouping variable.
The findgroups function can accept multiple grouping variables, for example G =
findgroups(A1,A2). You also can include multiple grouping variables in a table, for
example T = table(A1,A2); G = findgroups(T). The findgroups function defines
groups by the unique combinations of values across corresponding elements of the
grouping variables. findgroups decides the order by the order of the first grouping
variable, and then by the order of the second grouping variable, and so on. For example,
if A1 = {'a','a','b','b'} and A2 = [0 1 0 0], then the unique values across the
grouping variables are 'a' 0, 'a' 1, and 'b' 0, defining three groups.
The findgroups function returns a vector of group numbers that define groups based on
the unique values in the grouping variables. splitapply uses the group numbers to split
the data into groups efficiently before applying a function.
9-82
See Also
See Also
findgroups | rowfun | splitapply | varfun
9-83
9 Tables
Related Examples
• “Access Data in Tables” on page 9-44
• “Split Table Data Variables and Apply Functions” on page 9-69
• “Split Data into Groups and Calculate Statistics” on page 9-65
9-84
Changes to DimensionNames Property in R2016b
T =
Number Party
______ __________
Display its properties, including the dimension names. The default values of the
dimension names are 'Row' and 'Variables'.
T.Properties
ans =
Description: ''
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {'Number' 'Party'}
VariableDescriptions: {}
VariableUnits: {}
RowNames: {5×1 cell}
9-85
9 Tables
Starting in R2016b, you can assign new names to the dimension names, and use them to
access table data. Dimension names must be valid MATLAB identifiers, and must not be
one of the reserved names, 'Properties', 'RowNames', or 'VariableNames'.
Assign a new name to the first dimension name, and use it to access the row names of the
table.
T.Properties.DimensionNames{1} = 'Name';
T.Name
ans =
'Van Buren'
'Arthur'
'Fillmore'
'Garfield'
'Polk'
Create a new table variable called Name. When you create the variable, the table modifies
its first dimension name to prevent a conflict. The updated dimension name becomes
Name_1.
Warning: DimensionNames property was modified to avoid conflicting dimension and variable names:
'Name'. See Compatibility Considerations for Using Tables for more details. This will become an
error in a future release.
T =
T.Properties.DimensionNames
ans =
'Name_1' 'Data'
9-86
Changes to DimensionNames Property in R2016b
Similarly, if you assign a dimension name that is not a valid MATLAB identifier, the name
is modified.
ans =
'LastName' 'Data'
In R2016b, tables raise warnings when dimension names are not valid identifiers, or
conflict with variable names or reserved names, so that you can continue to work with
code and tables created with previous releases. If you encounter these warnings, it is
recommended that you update your code to avoid them.
9-87
10
Timetables
Create Timetables
This example shows how to create a timetable, combine timetables, and adjust the data
from multiple timetables to a common time vector. The common time vector can contain
the times from either or both timetables, or it can be an entirely new time vector that you
specify. The example shows how to compute and display a daily mean for weather
measurements contained in different timetables.
A timetable is a type of table that associates a time with each row. A timetable can store
column-oriented data variables that have different data types and sizes, so long as each
variable has the same number of rows. In addition, timetables provide time-specific
functions to combine, subscript into, and adjust their data.
Load air quality data and weather measurements into two different timetables. The dates
of the measurements range from November 15, 2015, to November 19, 2015. The air
quality data come from a sensor inside a building, while the weather measurements come
from sensors outside.
Read the air quality data from a table with the readtable function. Then convert it from
a table to a timetable with the table2timetable function. The readtable function
returns a table only, not a timetable.
indoors = readtable('indoors.csv');
indoors = table2timetable(indoors);
You also can create a timetable from an M-by-N array with the array2timetable
function, or from workspace variables with the timetable function.
Display the first five rows of indoors. Each row of the timetable has a time that labels
that row of data.
indoors(1:5,:)
ans=5×3 timetable
Time Humidity AirQuality
___________________ ________ __________
2015-11-15 00:00:24 36 80
2015-11-15 01:13:35 36 80
2015-11-15 02:26:47 37 79
2015-11-15 03:39:59 37 82
10-2
Create Timetables
2015-11-15 04:53:11 36 80
Load the timetable with weather measurements. Display the first five rows of outdoors.
load outdoors
outdoors(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
Synchronize Timetables
The timetables, indoors and outdoors, contain different measurements taken inside
and outside a building at different times. Combine all the data into one timetable with the
synchronize function.
tt = synchronize(indoors,outdoors);
tt(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:24 36 80 49 5
2015-11-15 01:13:35 36 80 NaN
2015-11-15 01:30:24 NaN NaN 48.9 5
2015-11-15 02:26:47 37 79 NaN
2015-11-15 03:00:24 NaN NaN 48.9 5
The output timetable, tt contains all the times from both timetables. synchronize puts
a missing data indicator where there are no data values to place in tt. When both input
timetables have a variable with the same name, such as Humidity, synchronize
renames both variables and adds both to the output timetable.
10-3
10 Timetables
Synchronize the timetables again, and this time fill in missing data values with linear
interpolation.
ttLinear = synchronize(indoors,outdoors,'union','linear');
ttLinear(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:24 36 80 49
2015-11-15 01:13:35 36 80 48.919 51
2015-11-15 01:30:24 36.23 79.77 48.9
2015-11-15 02:26:47 37 79 48.9
2015-11-15 03:00:24 37 80.378 48.9
You also can adjust the data in a single timetable to a new time vector. Calculate the
means of the variables in ttLinear over six-hour intervals with the retime function. If
any rows have NaN values after you adjust the data, remove them with the rmmissing
function.
tv = [datetime(2015,11,15):hours(6):datetime(2015,11,18)];
ttHourly = retime(ttLinear,tv,'mean');
ttHourly = rmmissing(ttHourly);
Normalize the data in ttHourly to the mean for each variable in the timetable. Plot the
mean daily values of these measurements. You can use the Variables property of a
timetable to access the variables. ttHourly.Variables returns the same variables as
ttHourly{:,:}.
ttMeanVars = ttHourly.Variables./mean(ttHourly.Variables);
plot(ttHourly.Time,ttMeanVars);
legend(ttHourly.Properties.VariableNames,'Interpreter','none');
xlabel('Time');
ylabel('Normalized Weather Measurements');
title('Mean Daily Weather Trends');
10-4
See Also
See Also
retime | rmmissing | synchronize | table2timetable | timerange | timetable
Related Examples
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Combine Timetables and Synchronize Their Data” on page 10-11
• “Retime and Synchronize Timetable Variables Using Different Methods” on page 10-
18
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
10-5
10 Timetables
10-6
Resample and Aggregate Data in Timetable
Import Timetable
Load a timetable containing weather measurements taken from November 15, 2015, to
November 19, 2015. The timetable contains humidity, temperature, and pressure readings
taken over this time period.
load outdoors
outdoors(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
Determine if the timetable is regular. A regular timetable is one in which the differences
between all consecutive row times are the same. outdoors is not a regular timetable.
TF = isregular(outdoors)
TF = logical
0
Find the differences in the time steps. They vary between half a minute and an hour and a
half.
dt = unique(diff(outdoors.Time))
10-7
10 Timetables
01:29:36
01:30:00
Adjust the data in the timetable with the retime function. Specify an hourly time vector.
Interpolate the timetable data to the new row times.
TT = retime(outdoors,'hourly','spline');
TT(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
Specify an hourly time vector for TT. For each row in TT, copy values from the
corresponding row in outdoors whose row time is nearest.
TT = retime(outdoors,'hourly','nearest');
TT(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
The retime function provides aggregation methods, such as mean. Calculate the daily
means for the data in outdoors.
10-8
Resample and Aggregate Data in Timetable
TT = retime(outdoors,'daily','mean');
TT
TT=4×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
Calculate the means over six-hour time intervals. Specify a regular time step using the
'regular' input argument and the 'TimeStep' name-value pair argument.
TT = retime(outdoors,'regular','mean','TimeStep',hours(6));
TT(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
As an alternative, you can specify a time vector that has the same six-hour time intervals.
Specify a format for the time vector to display both date and time when you display the
timetable.
tv = datetime(2015,11,15):hours(6):datetime(2015,11,18);
tv.Format = 'dd-MMM-yyyy HH:mm:ss';
TT = retime(outdoors,tv,'mean');
TT(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
10-9
10 Timetables
See Also
retime | synchronize | table2timetable | timetable
Related Examples
• “Create Timetables” on page 10-2
• “Combine Timetables and Synchronize Their Data” on page 10-11
• “Retime and Synchronize Timetable Variables Using Different Methods” on page 10-
18
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
10-10
Combine Timetables and Synchronize Their Data
Load timetables from openPricesSmall and concatenate them vertically. The timetables
are opWeek1 and opWeek2. They contain opening prices for some stocks during the first
and second weeks of January 2016.
load openPricesSmall
opWeek1=5×3 timetable
Time AAPL FB
____________________ ______ ______
opWeek2
opWeek2=5×3 timetable
Time AAPL FB
____________________ ______ ______
10-11
10 Timetables
Concatenate the timetables. You can concatenate timetables vertically when they have the
same variables. The row times label the rows and are not contained in a timetable
variable. Note that the row times of a timetable can be out of order and do not need to be
regularly spaced. For example, op does not include days that fall on weekends. A
timetable also can contain duplicate times. op contains two rows for 08-Jan-2016
09:00:00.
op = [opWeek2;opWeek1]
op=10×3 timetable
Time AAPL FB
____________________ ______ ______
You also can concatenate timetables horizontally. The timetables must have the same row
times and different variables.
Display the timetable opOtherStocks. The timetable has the same row times as
opWeek1, but variables for different stocks.
opOtherStocks
opOtherStocks=5×3 timetable
Time MSFT TWTR
____________________ _____ _____
10-12
Combine Timetables and Synchronize Their Data
Concatenate opWeek1 and opOtherStock. The output timetable has one set of row times
and the variables from both timetables.
op = [opWeek1 opOtherStocks]
op=5×5 timetable
Time AAPL FB MSFT TWTR
____________________ ______ ______ _____ _____
Load air quality data and weather measurements from two different timetables and
synchronize them. The dates of the measurements range from November 15, 2015, to
November 19, 2015. The air quality data come from a sensor inside a building, while the
weather measurements come from sensors outside.
load indoors
load outdoors
Display the first five lines of each timetable. They contain measurements of different
quantities taken at different times.
indoors(1:5,:)
ans=5×3 timetable
Time Humidity AirQuality
___________________ ________ __________
2015-11-15 00:00:24 36 80
2015-11-15 01:13:35 36 80
2015-11-15 02:26:47 37 79
2015-11-15 03:39:59 37 82
10-13
10 Timetables
2015-11-15 04:53:11 36 80
outdoors(1:5,:)
ans=5×4 timetable
Time Humidity TemperatureF PressureHg
___________________ ________ ____________ __________
Synchronize the timetables. The output timetable tt contains all the times from both
timetables. synchronize puts a missing data indicator where there are no data values to
place in tt. When both input timetables have a variable with the same name, such as
Humidity, synchronize renames both variables and adds both to the output timetable.
tt = synchronize(indoors,outdoors);
tt(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:24 36 80 49 5
2015-11-15 01:13:35 36 80 NaN
2015-11-15 01:30:24 NaN NaN 48.9 5
2015-11-15 02:26:47 37 79 NaN
2015-11-15 03:00:24 NaN NaN 48.9 5
Synchronize the timetables, and fill in missing timetable elements with linear
interpolation. To synchronize on a time vector that includes all times from both
timetables, specify 'union' for the output times.
ttLinear = synchronize(indoors,outdoors,'union','linear');
ttLinear(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
10-14
Combine Timetables and Synchronize Their Data
2015-11-15 00:00:24 36 80 49
2015-11-15 01:13:35 36 80 48.919 51
2015-11-15 01:30:24 36.23 79.77 48.9
2015-11-15 02:26:47 37 79 48.9
2015-11-15 03:00:24 37 80.378 48.9
Synchronize the timetables to an hourly time vector. The input timetables had irregular
row times. The output timetable has regular row times with one hour as the time step.
ttHourly = synchronize(indoors,outdoors,'hourly','linear');
ttHourly(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:00 36 80 49 51
2015-11-15 01:00:00 36 80 48.934 51
2015-11-15 02:00:00 36.634 79.366 48.9
2015-11-15 03:00:00 37 80.361 48.9
2015-11-15 04:00:00 36.727 81.453 48.834
Synchronize the timetables to a 30-minute time step. Specify a regular time step using
the 'regular' input argument and the 'TimeStep' name-value pair argument.
ttHalfHour = synchronize(indoors,outdoors,'regular','linear','TimeStep',minutes(30));
ttHalfHour(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:00 36 80 49 51
2015-11-15 00:30:00 36 80 48.967 51
2015-11-15 01:00:00 36 80 48.934 51
2015-11-15 01:30:00 36.224 79.776 48.9 51
2015-11-15 02:00:00 36.634 79.366 48.9
10-15
10 Timetables
As an alternative, you can synchronize the timetables to a time vector that specifies half-
hour intervals.
tv = [datetime(2015,11,15):minutes(30):datetime(2015,11,18)];
tv.Format = indoors.Time.Format;
ttHalfHour = synchronize(indoors,outdoors,tv,'linear');
ttHalfHour(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
2015-11-15 00:00:00 36 80 49 51
2015-11-15 00:30:00 36 80 48.967 51
2015-11-15 01:00:00 36 80 48.934 51
2015-11-15 01:30:00 36.224 79.776 48.9 51
2015-11-15 02:00:00 36.634 79.366 48.9
Synchronize the timetables and calculate the daily means for all variables in the output
timetable.
ttDaily = synchronize(indoors,outdoors,'daily','mean');
ttDaily
ttDaily=4×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
Synchronize the timetables to six-hour time intervals and calculate a mean for each
interval.
tt6Hours = synchronize(indoors,outdoors,'regular','mean','TimeStep',hours(6));
tt6Hours(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
10-16
See Also
As an alternative, specify a time vector that has the same six-hour time intervals.
tv = [datetime(2015,11,15):hours(6):datetime(2015,11,18)];
tv.Format = indoors.Time.Format;
tt6Hours = synchronize(indoors,outdoors,tv,'mean');
tt6Hours(1:5,:)
ans=5×6 timetable
Time Humidity_indoors AirQuality Humidity_outdoors Tempe
___________________ ________________ __________ _________________ _____
See Also
retime | synchronize | table2timetable | timetable
Related Examples
• “Create Timetables” on page 10-2
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Retime and Synchronize Timetable Variables Using Different Methods” on page 10-
18
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
10-17
10 Timetables
Create Timetable
Create a timetable that has simulated weather measurements for several days in May
2017. The timetable variables Tmax and Tmin contain maximum and minimum
temperature readings for each day, and PrecipTotal contains total precipitation for the
day. WXEvent is a categorical array, recording whether certain kinds of weather events,
such as thunder or hail, happened on any given day. The timetable has simulated data
from May 4 to May 10, 2017, but is missing data for two days, May 6th and May 7th.
Date = [datetime(2017,5,4) datetime(2017,5,5) datetime(2017,5,8:10)]';
Tmax = [60 62 56 59 60]';
Tmin = [44 45 40 42 45]';
PrecipTotal = [0.2 0 0 0.15 0]';
WXEvent = [2 0 0 1 0]';
WXEvent = categorical(WXEvent,[0 1 2 3 4],{'None','Thunder','Hail','Fog','Tornado'});
Station1 = timetable(Date,Tmax,Tmin,PrecipTotal,WXEvent)
Station1=5×5 timetable
Date Tmax Tmin PrecipTotal WXEvent
___________ ____ ____ ___________ _______
One way to fill in data for the two missing days is to use the retime function. If you call
retime without specifying a method, then retime fills in gaps with missing data
10-18
Retime and Synchronize Timetable Variables Using Different Methods
indicators. For instance, retime fills gaps in numeric variables with NaN values, and gaps
in the categorical variable with undefined elements.
Station1Daily = retime(Station1,'daily')
Station1Daily=7×5 timetable
Date Tmax Tmin PrecipTotal WXEvent
___________ ____ ____ ___________ ___________
If you specify a method when you call retime, it uses the same method to fill gaps in
every variable. To apply different methods to different variables, you can call retime
multiple times, each time indexing into the timetable to access a different subset of
variables.
However, you also can apply different methods by specifying the VariableContinuity
property of the timetable. You can specify whether each variable contains continuous or
discrete data. Then the retime function applies a different method to each timetable
variable, depending on the corresponding VariableContinuity value.
If you specify VariableContinuity, then the retime function fills in the output
timetable variables using the following methods:
• 'unset' — Fill in values using the missing data indicator for that type (such as NaN
for numeric variables).
• 'continuous' — Fill in values using linear interpolation.
• 'step' — Fill in values using previous value.
• 'event' — Fill in values using the missing data indicator for that type.
Specify that the temperature data in Station1 is continuous, that PrecipTotal is step
data, and that WXEvent is event data.
Station1.Properties.VariableContinuity = {'continuous','continuous','step','event'};
Station1.Properties
10-19
10 Timetables
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Date' 'Variables'}
VariableNames: {'Tmax' 'Tmin' 'PrecipTotal' 'WXEvent'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: [continuous continuous step event]
RowTimes: [5x1 datetime]
StartTime: 04-May-2017
SampleRate: NaN
TimeStep: NaN
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
Station1Daily = retime(Station1,'daily')
Station1Daily=7×5 timetable
Date Tmax Tmin PrecipTotal WXEvent
___________ ____ ______ ___________ ___________
If you specify a method, then retime applies that method to all variables, overriding the
values in VariableContinuity.
Station1Missing = retime(Station1,'daily','fillwithmissing')
Station1Missing=7×5 timetable
Date Tmax Tmin PrecipTotal WXEvent
___________ ____ ____ ___________ ___________
10-20
Retime and Synchronize Timetable Variables Using Different Methods
The synchronize function also fills in output timetable variables using different
methods, depending on the values specified in the VariableContinuity property of
each input timetable.
Create a second timetable that contains pressure readings in millibars from a second
weather station. The timetable has simulated readings from May 4 to May 8, 2017.
Date = datetime(2017,5,4:8)';
Pressure = [995 1003 1013 1018 1006]';
Station2 = timetable(Date,Pressure)
Station2=5×2 timetable
Date Pressure
___________ ________
04-May-2017 995
05-May-2017 1003
06-May-2017 1013
07-May-2017 1018
08-May-2017 1006
Synchronize the data from the two stations using the synchronize function.
synchronize fills in values for variables from Station1 according to the values in the
VariableContinuity property of Station1. However, since the
VariableContinuity property of Station2 is empty, synchronize fills in Pressure
with NaN values.
BothStations = synchronize(Station1,Station2)
BothStations=7×6 timetable
Date Tmax Tmin PrecipTotal WXEvent Pressure
___________ ____ ______ ___________ ___________ ________
10-21
10 Timetables
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Date' 'Variables'}
VariableNames: {'Pressure'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: continuous
RowTimes: [5x1 datetime]
StartTime: 04-May-2017
SampleRate: NaN
TimeStep: 1d
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
Synchronize the data from the two stations. synchronize fills in values in
BothStations.Pressure because Station2.Pressure has continuous data.
BothStations = synchronize(Station1,Station2)
BothStations=7×6 timetable
Date Tmax Tmin PrecipTotal WXEvent Pressure
___________ ____ ______ ___________ ___________ ________
10-22
See Also
See Also
retime | synchronize | timetable
Related Examples
• “Create Timetables” on page 10-2
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Combine Timetables and Synchronize Their Data” on page 10-11
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
10-23
10 Timetables
Create a timetable from the sample file outages.csv, containing data representing
electric utility outages in the United States. Read the table from the file with the
readtable function. Convert T.Cause and T.Region into categorical arrays. Then
convert the table to a timetable using the table2timetable function. Display the first
five rows of the timetable. TT is a timetable containing outage data from February 2002 to
January 2014.
T = readtable('outages.csv');
T.Cause = categorical(T.Cause);
T.Region = categorical(T.Region);
TT = table2timetable(T);
TT(1:5,:)
ans=5×6 timetable
OutageTime Region Loss Customers RestorationTime C
________________ _________ ______ __________ ________________ ______
Display a summary of TT. It is a timetable that contains 1468 rows and five variables.
summary(TT)
10-24
Select Timetable Data by Row Time and Variable Type
RowTimes:
Variables:
Values:
MidWest 142
NorthEast 557
SouthEast 389
SouthWest 26
West 354
Values:
Min 0
Median 180.26
Max 23418
NumMissing 604
Values:
Min 0
Median 75765
Max 5.9689e+06
NumMissing 328
Values:
10-25
10 Timetables
Values:
attack 294
earthquake 2
energy emergency 188
equipment fault 156
fire 25
severe storm 338
thunder storm 201
unknown 24
wind 95
winter storm 145
Access the row times. The row times are not in a variable. Instead, the vector of row
times is a property of the timetable. However, you can access the row times with dot
syntax. TT.OutageTime is a 1468-by-1 vector of datetime values. Display the first five
rows of TT.OutageTime.
TT.OutageTime(1:5)
To select all timetable rows that fall in a time range, create a subscript as a helper, using
the timerange function. The start and end times you specify do not have to match any of
the row times in the timetable.
Select all rows with outages that occurred between January 2002 and December 2003.
Display the first five rows of TT2.
TR = timerange('2002-01-01','2003-12-31');
TT2 = TT(TR,:);
TT2(1:5,:)
10-26
Select Timetable Data by Row Time and Variable Type
ans=5×6 timetable
OutageTime Region Loss Customers RestorationTime Cau
________________ _________ ______ __________ ________________ ______
TT2(end-4:end,:)
ans=5×6 timetable
OutageTime Region Loss Customers RestorationTime C
________________ _________ ______ __________ ________________ ______
TT2 is a timetable with only 98 rows, containing outage data only from 2002 and 2003.
You can index into TT with datetime values or character vectors representing specific
times in TT.OutageTime. But when you do so, the times you specify must have exact
matches in the time vector, and only those times are selected. Index into TT on times for
the first and third rows of TT.
ans=2×6 timetable
OutageTime Region Loss Customers RestorationTime Cau
________________ _________ ______ __________ ________________ ______
10-27
10 Timetables
Specify a tolerance when indexing on times. You can use the withtol function to create a
subscript as a helper. With the output of withtol, you can select row times that match
within the specified tolerance.
Index into TT on specified dates. Specify a tolerance of one day to return rows with row
times that are within one day of the specified dates. The times must be in a datetime or
duration vector, or in a cell array of character vectors that can be converted to datetime
or duration values. The tolerance must be specified as a duration, using a function such
as seconds, minutes, hours, or days.
rowTimes = {'2002-02-01','2003-02-07'};
S = withtol(rowTimes,days(1));
TT(S,:)
ans=2×6 timetable
OutageTime Region Loss Customers RestorationTime Cau
________________ _________ ______ __________ ________________ ______
To select all timetable variables that are of a given type, create a subscript as a helper,
using the vartype function. You can specify variable types without having to specify their
names or positions in the timetable.
Select all variables that contain numeric data. TT2 contains only the variables Loss and
Customers. The other three variables of TT are either categorical or datetime variables.
Display the first five rows of TT2.
S = vartype('numeric');
TT2 = TT(:,S);
TT2(1:5,:)
ans=5×3 timetable
OutageTime Loss Customers
________________ ______ __________
10-28
Select Timetable Data by Row Time and Variable Type
TR = timerange('2002-01-01','2003-12-31');
TT2 = TT(TR,S);
TT2(1:5,:)
ans=5×3 timetable
OutageTime Loss Customers
________________ ______ __________
Tables and timetables have a property, Variables, that you can use to extract data from
variables into a matrix, as long as the variables can be concatenated together.
Extract the numeric data from TT2 using the Variables property. A is a 1468-by-2
matrix of doubles. When you extract data from a timetable into an array, the row times
are not included.
A = TT2.Variables;
A(1:5,:)
ans = 5×2
106 ×
0.0005 1.8202
0.0005 0.2120
0.0003 0.1429
0.0002 0.2128
0 0
10-29
10 Timetables
The result of TT2.Variables is the same as the result of using curly braces to extract
data, using the TT2{:,:} syntax.
You can concatenate the variables in TT2 into an array of doubles. However, TT contains
numeric, categorical, and datetime variables that cannot be concatenated. The
Variables property returns an error when variables cannot be concatenated. To avoid
such an error, you can subscript by variable type before using the Variables property.
Subscript into TT to select numeric variables and extract them into a matrix.
A = TT(:,vartype('numeric')).Variables;
A(1:5,:)
ans = 5×2
106 ×
0.0005 1.8202
0.0005 0.2120
0.0003 0.1429
0.0004 0.3404
0.0002 0.2128
See Also
retime | synchronize | table2timetable | timerange | timetable | vartype |
withtol
Related Examples
• “Create Timetables” on page 10-2
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Combine Timetables and Synchronize Their Data” on page 10-11
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
10-30
Clean Timetable with Missing, Duplicate, or Nonuniform Times
Timetables can be irregular. They can contain rows that are not sorted by their row times.
Timetables can contain multiple rows with the same row time, though the rows can have
different data values. Even when row times are sorted and unique, they can differ by time
steps of different sizes. Timetables can even contain NaT or NaN values to indicate
missing row times.
10-31
10 Timetables
Load Timetable
Load a sample timetable from the MAT-file badTimes that contains weather
measurements taken over several hours on June 9, 2016. The timetable includes
temperature, rainfall, and wind speed measurements taken at irregular times during that
day.
load badTimes
TT
TT=12×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
10-32
Clean Timetable with Missing, Duplicate, or Nonuniform Times
NaT 56 0 0
09-Jun-2016 09:53:57 67 0.03 3.4
09-Jun-2016 08:49:10 62 0.01 2.7
09-Jun-2016 08:49:10 75.8 0.01 2.7
09-Jun-2016 08:49:10 82 0.01 2.7
09-Jun-2016 05:03:11 66.2 0.05 3
09-Jun-2016 08:49:10 67.2 0.01 2.7
09-Jun-2016 04:12:00 58.8 NaN NaN
Remove rows that have NaT, or a missing value, as the row time. To find missing values in
the vector of row times, use the ismissing function. ismissing returns a logical vector
that contains 1 wherever TT.Time has a missing value. Index back into the timetable to
keep only those rows that do not have missing values as row times. Assign those rows to
TT2.
TF = ismissing(TT.Time);
TT2 = TT(~TF,:);
TT2
TT2=11×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
This method removes only the rows that have missing row times. The table variables
might still have missing data values. For example, the last row of TT2 has NaN values for
the Rain and Windspeed variables.
10-33
10 Timetables
You can remove missing row times and missing data values using the rmmissing
function. rmmissing removes any timetable row that has a missing row time, missing
data values, or both.
Display the missing row time and missing data values of TT. Then remove all missing
values from TT.
TT
TT=12×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
TT = rmmissing(TT)
TT=10×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
10-34
Clean Timetable with Missing, Duplicate, or Nonuniform Times
Determine whether TT is sorted. Then, sort the timetable on its row times using the
sortrows function.
TF = issorted(TT)
TF = logical
0
TT = sortrows(TT)
TT=10×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
Determine whether TT is regular. A regular timetable has the same time interval between
consecutive row times. Even a sorted timetable can have time steps that are not uniform.
TF = isregular(TT)
TF = logical
0
10-35
10 Timetables
01:58:19
00:49:47
00:00:00
00:00:00
00:00:00
01:04:47
00:00:00
00:00:00
Timetables can have duplicate rows. Timetable rows are duplicates if they have the same
row times and the same data values. In this example, the last two rows of TT are
duplicates.
To remove the duplicate rows, use the unique function. unique returns the unique rows
and sorts them by their row times.
TT = unique(TT)
TT=9×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
Timetables can have rows with duplicate row times but different data values. In this
example, TT has several rows with the same row times but different values.
Find the rows that have duplicate row times. First, sort the row times and find
consecutive times that have no difference between them. Times with no difference
between them are the duplicates. Index back into the vector of row times and return a
unique set of times that identify the duplicate row times in TT.
10-36
Clean Timetable with Missing, Duplicate, or Nonuniform Times
dupTimes = sort(TT.Time);
TF = (diff(dupTimes) == 0);
dupTimes = dupTimes(TF);
dupTimes = unique(dupTimes)
Index into the timetable to display the rows with duplicate row times. When you index on
times, the output timetable contains all rows with matching row times.
TT(dupTimes,:)
ans=6×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
Select either the first and the last of the rows with duplicate row times using the unique
and retime functions.
First, create a vector of unique row times from TT using the unique function.
uniqueTimes = unique(TT.Time);
Select the first row from each set of rows that have duplicate times.
TT2 = retime(TT,uniqueTimes)
TT2=5×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
10-37
10 Timetables
Select the last rows from each set of rows that have duplicate times. Specify the
'previous' method of retime to copy data from the last row. When you specify
'previous', then retime starts at the end of the vector of row times and stops when it
encounters a duplicate row time. Then it copies the data from that row.
TT2 = retime(TT,uniqueTimes,'previous')
TT2=5×4 timetable
Time Temp Rain WindSpeed
____________________ ____ ____ _________
Aggregate data from rows that have duplicate row times. For example, you can calculate
the means of several measurements of the same quantity taken at the same time.
Calculate the mean temperature, rainfall, and wind speed for rows with duplicate row
times using the retime function.
TT = retime(TT,uniqueTimes,'mean')
TT=5×4 timetable
Time Temp Rain WindSpeed
____________________ _____ ____ _________
10-38
Clean Timetable with Missing, Duplicate, or Nonuniform Times
Create a regular timetable using retime. Interpolate the data onto a regular hourly time
vector. To use linear interpolation, specify 'linear'. Each row time in TT begins on the
hour, and there is a one-hour interval between consecutive row times.
TT = retime(TT,'hourly','linear')
TT=6×4 timetable
Time Temp Rain WindSpeed
____________________ ______ ________ _________
Instead of using a predefined time step such as 'hourly', you can specify a time step of
your own. To specify a time step of 30 minutes, use the 'regular' input argument and
the 'TimeStep' name-value pair argument. You can specify a time step of any size as a
duration or calendar duration value.
TT = retime(TT,'regular','linear','TimeStep',minutes(30))
TT=11×4 timetable
Time Temp Rain WindSpeed
____________________ ______ ________ _________
10-39
10 Timetables
You can export the timetable data for use with functions to analyze data that is regularly
spaced in time. For example, the Econometrics Toolbox™ and the Signal Processing
Toolbox™ have functions you can use for further analysis on regularly spaced data.
Extract the timetable data as an array. You can use the Variables property to return the
data as an array when the table variables can be concatenated.
A = TT.Variables
A = 11×3
A2 = TT{:,:}
A2 = 11×3
10-40
See Also
See Also
diff | fillmissing | isregular | issorted | retime | rmmissing | sortrows |
table2timetable | timetable | unique
Related Examples
• “Create Timetables” on page 10-2
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Combine Timetables and Synchronize Their Data” on page 10-11
• “Retime and Synchronize Timetable Variables Using Different Methods” on page 10-
18
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
10-41
10 Timetables
For example, you can sort a timetable on its row times, on one or more of its data
variables, or on row times and data variables together.
Create a timetable using the timetable function. A timetable has row times along its
first dimension, labeling the rows. The row times are a property of the timetable, not a
timetable variable.
Date = datetime(2016,7,[10;10;11;11;10;10;11;11]);
X = [1;1;1;1;2;2;2;2];
Y = {'a';'b';'a';'b';'a';'b';'a';'b'};
Z = [1;2;3;4;5;6;7;8];
TT = timetable(X,Y,Z,'RowTimes',Date)
TT=8×4 timetable
Time X Y Z
___________ _ _____ _
10-Jul-2016 1 {'a'} 1
10-Jul-2016 1 {'b'} 2
11-Jul-2016 1 {'a'} 3
11-Jul-2016 1 {'b'} 4
10-Jul-2016 2 {'a'} 5
10-Jul-2016 2 {'b'} 6
11-Jul-2016 2 {'a'} 7
11-Jul-2016 2 {'b'} 8
Rename the first dimension. By default, the name of the first dimension of a timetable is
Time. You can access the Properties.DimensionNames property to rename a
dimension.
10-42
Using Row Labels in Table and Timetable Operations
TT.Properties.DimensionNames{1} = 'Date';
TT.Properties.DimensionNames
As an alternative, you can specify the row times as the first input argument to
timetable, without specifying 'RowTimes'. The timetable function names the row
times, or the first dimension, after the first input argument, just as it names the timetable
variables after the other input arguments.
TT = timetable(Date,X,Y,Z)
TT=8×4 timetable
Date X Y Z
___________ _ _____ _
10-Jul-2016 1 {'a'} 1
10-Jul-2016 1 {'b'} 2
11-Jul-2016 1 {'a'} 3
11-Jul-2016 1 {'b'} 4
10-Jul-2016 2 {'a'} 5
10-Jul-2016 2 {'b'} 6
11-Jul-2016 2 {'a'} 7
11-Jul-2016 2 {'b'} 8
Sort the timetable by row times. To sort on row times, refer to the first dimension of the
timetable by name.
sortrows(TT,'Date')
ans=8×4 timetable
Date X Y Z
___________ _ _____ _
10-Jul-2016 1 {'a'} 1
10-Jul-2016 1 {'b'} 2
10-Jul-2016 2 {'a'} 5
10-Jul-2016 2 {'b'} 6
11-Jul-2016 1 {'a'} 3
11-Jul-2016 1 {'b'} 4
11-Jul-2016 2 {'a'} 7
10-43
10 Timetables
11-Jul-2016 2 {'b'} 8
sortrows(TT,{'X' 'Y'})
ans=8×4 timetable
Date X Y Z
___________ _ _____ _
10-Jul-2016 1 {'a'} 1
11-Jul-2016 1 {'a'} 3
10-Jul-2016 1 {'b'} 2
11-Jul-2016 1 {'b'} 4
10-Jul-2016 2 {'a'} 5
11-Jul-2016 2 {'a'} 7
10-Jul-2016 2 {'b'} 6
11-Jul-2016 2 {'b'} 8
sortrows(TT,{'Date' 'X'})
ans=8×4 timetable
Date X Y Z
___________ _ _____ _
10-Jul-2016 1 {'a'} 1
10-Jul-2016 1 {'b'} 2
10-Jul-2016 2 {'a'} 5
10-Jul-2016 2 {'b'} 6
11-Jul-2016 1 {'a'} 3
11-Jul-2016 1 {'b'} 4
11-Jul-2016 2 {'a'} 7
11-Jul-2016 2 {'b'} 8
When you group rows together using the rowfun, varfun, stack, and unstack
functions, you can specify row labels as grouping variables. When you join tables or
timetable together using the join, innerjoin, and outerjoin functions, you can
specify row labels as key variables.
10-44
Using Row Labels in Table and Timetable Operations
For example, you can perform an inner join two tables together, using row names and a
table variable together as key variables. An inner join keeps only those table rows that
match with respect to the key variables.
Create two tables of patient data. A table can have row names along its first dimension,
labeling the rows, but is not required to have them. Specify the last names of patients as
the row names of the tables. Add the first names of the patients as table variables.
A = table({'Michael';'Louis';'Alice';'Rosemary';'Julie'},[38;43;45;40;49],...
'VariableNames',{'FirstName' 'Age'},...
'RowNames',{'Garcia' 'Johnson' 'Wu' 'Jones' 'Picard'})
A=5×2 table
FirstName Age
____________ ___
Garcia {'Michael' } 38
Johnson {'Louis' } 43
Wu {'Alice' } 45
Jones {'Rosemary'} 40
Picard {'Julie' } 49
B = table({'Michael';'Beverly';'Alice'},...
[64;69;67],...
[119;163;133],...
[122 80; 109 77; 117 75],...
'VariableNames',{'FirstName' 'Height' 'Weight' 'BloodPressure'},...
'RowNames',{'Garcia' 'Johnson' 'Wu'})
B=3×4 table
FirstName Height Weight BloodPressure
___________ ______ ______ _____________
If a table has row names, then you can index into it by row name. Indexing by row names
is a convenient way to select rows of a table. Index into B by a patient's last name to
retrieve information about the patient.
B('Garcia',:)
10-45
10 Timetables
ans=1×4 table
FirstName Height Weight BloodPressure
___________ ______ ______ _____________
Perform an inner join on the two tables. Both tables use the last names of patients as row
names, and contain the first names as a table variable. Some patients in the two tables
have matching last names but different first names. To ensure that both last and first
names match, use the row names and FirstName as key variables. To specify the row
names as a key or grouping variable, use the name of the first dimension of the table. By
default, the name of the first dimension is 'Row'.
C = innerjoin(A,B,'Keys',{'Row','FirstName'})
C=2×5 table
FirstName Age Height Weight BloodPressure
___________ ___ ______ ______ _____________
If you rename the first dimension of a table, then you can refer to the row names by that
name instead of using 'Row'. Perform the same inner join as above but use a different
name to refer to the row names.
A.Properties.DimensionNames
Change the name of the first dimension of the table by using its
Properties.DimensionNames property. Then use the new name as a key variable.
A.Properties.DimensionNames{1} = 'LastName';
A.Properties.DimensionNames
10-46
See Also
Perform an inner join on A and B using LastName and FirstName as key variables.
B.Properties.DimensionNames{1} = 'LastName';
D = innerjoin(A,B,'Keys',{'LastName','FirstName'})
D=2×5 table
FirstName Age Height Weight BloodPressure
___________ ___ ______ ______ _____________
• You cannot stack or unstack row labels using the stack and unstack functions.
However, you can use row labels as grouping variables.
• You cannot perform a join using the join, innerjoin, or outerjoin functions when
the first argument is a table and the second argument is a timetable. However, you can
perform a join when both arguments are tables, both are timetables, or the first
argument is a timetable and the second is a table.
• The output of a join operation can have row labels if you specify row labels as key
variables. For more details on row labels from a join operation, see the documentation
on the 'Keys', 'LeftKeys', and 'RightKeys' arguments of the join, innerjoin,
and outerjoin functions.
See Also
innerjoin | join | outerjoin | rowfun | sortrows | stack | unstack | varfun
10-47
10 Timetables
The file quake.mat contains 200Hz data from the October 17, 1989 Loma Prieta
earthquake in the Santa Cruz Mountains. The data are courtesy of Joel Yellin at the
Charles F. Richter Seismological Laboratory, University of California, Santa Cruz.
In the workspace there are three variables, containing time traces from an accelerometer
located in the Natural Sciences building at UC Santa Cruz. The accelerometer recorded
the main shock amplitude of the earthquake wave. The variables n, e, v refer to the three
directional components measured by the instrument, which was aligned parallel to the
fault, with its N direction pointing in the direction of Sacramento. The data are
uncorrected for the response of the instrument.
Create a variable, Time, containing the timestamps sampled at 200Hz with the same
length as the other vectors. Represent the correct units with the seconds function and
multiplication to achieve the Hz (s−1) sampling rate. This results in a duration variable
which is useful for representing elapsed time.
Time = (1/200)*seconds(1:length(e))';
whos Time
10-48
Loma Prieta Earthquake Analysis
Create a timetable with the time and three acceleration variables and supply more
meaningful variable names. Display the first eight rows using the head function.
ans=8×4 timetable
Time EastWest NorthSouth Vertical
_________ ________ __________ ________
0.005 sec 5 3 0
0.01 sec 5 3 0
0.015 sec 5 2 0
0.02 sec 5 2 0
0.025 sec 5 2 0
0.03 sec 5 2 0
0.035 sec 5 1 0
0.04 sec 5 1 0
Explore the data by accessing the variables in the timetable with dot subscripting. (For
more information on dot subscripting, see Access Data in a Table.) We chose "East-West"
amplitude and plot it as function of the duration.
plot(quakeData.Time,quakeData.EastWest)
title('East-West Acceleration')
10-49
10 Timetables
Scale Data
Scale the data by the gravitational acceleration, or multiply each variable in the table by
the constant. Since the variables are all of the same type (double), you can access all
variables using the dimension name, Variables. Note that quakeData.Variables
provides a direct way to modify the numerical values within the timetable.
quakeData.Variables = 0.098*quakeData.Variables;
We are interested in the time region where the amplitude of the shockwave starts to
increase from near zero to maximum levels. Visual inspection of the above plot shows that
the time interval from 8 to 15 seconds is of interest. For better visualization we draw
10-50
Loma Prieta Earthquake Analysis
black lines at the selected time spots to draw attention to that interval. All subsequent
calculations will involve this interval.
t1 = seconds(8)*[1;1];
t2 = seconds(15)*[1;1];
hold on
plot([t1 t2],ylim,'k','LineWidth',2)
hold off
Create another timetable with data in this interval. Use timerange to select the rows
of interest.
10-51
10 Timetables
tr = timerange(seconds(8),seconds(15));
dataOfInterest = quakeData(tr,:);
head(dataOfInterest)
ans=8×4 timetable
Time EastWest NorthSouth Vertical
_________ ________ __________ ________
figure
subplot(3,1,1)
plot(dataOfInterest.Time,dataOfInterest.EastWest)
ylabel('East-West')
title('Acceleration')
subplot(3,1,2)
plot(dataOfInterest.Time,dataOfInterest.NorthSouth)
ylabel('North-South')
subplot(3,1,3)
plot(dataOfInterest.Time,dataOfInterest.Vertical)
ylabel('Vertical')
10-52
Loma Prieta Earthquake Analysis
To display statistical information about the data we use the summary function.
summary(dataOfInterest)
RowTimes:
10-53
10 Timetables
Variables:
Values:
Min -255.09
Median -0.098
Max 244.51
Values:
Min -198.55
Median 1.078
Max 204.33
Values:
Min -157.88
Median 0.98
Max 134.46
Additional statistical information about the data can be calculated using varfun.This is
useful for applying functions to each variable in a table or timetable. The function to
apply is passed to varfun as a function handle. Below we apply the mean function to all
three variables and output the result in format of a table, since the time is not meaningful
after computing the temporal means.
mn = varfun(@mean,dataOfInterest,'OutputFormat','table')
mn=1×3 table
mean_EastWest mean_NorthSouth mean_Vertical
_____________ _______________ _____________
10-54
Loma Prieta Earthquake Analysis
Below we perform the integration on all three variables to calculate the velocity. It is
convenient to create a function and apply it to the variables in the timetable with
varfun. In this example, we included the function at the end of this file and named it
velFun.
vel = varfun(@velFun,dataOfInterest);
head(vel)
ans=8×4 timetable
Time velFun_EastWest velFun_NorthSouth velFun_Vertical
_________ _______________ _________________ _______________
Apply the same function velFun to the velocities to determine the position.
pos = varfun(@velFun,vel);
head(pos)
ans=8×4 timetable
Time velFun_velFun_EastWest velFun_velFun_NorthSouth velFun_velFun_Ve
_________ ______________________ ________________________ ________________
10-55
10 Timetables
Notice how the variable names in the timetable created by varfun include the name of
the function used. It is useful to track the operations that have been performed on the
original data. Adjust the variable names back to their original values using dot notation.
pos.Properties.VariableNames = varNames;
Below we plot the 3 components of the velocity and position for the time interval of
interest.
figure
subplot(2,1,1)
plot(vel.Time,vel.Variables)
legend(quakeData.Properties.VariableNames,'Location','NorthWest')
title('Velocity')
subplot(2,1,2)
plot(vel.Time,pos.Variables)
legend(quakeData.Properties.VariableNames,'Location','NorthWest')
title('Position')
10-56
Loma Prieta Earthquake Analysis
Visualize Trajectories
The trajectories can be plotted in 2D or 3D by using the component value. In the following
we will show different ways of visualizing this data.
Begin with 2-dimensional projections. Here is the first with a few values of time
annotated.
figure
plot(pos.NorthSouth,pos.Vertical)
xlabel('North-South')
ylabel('Vertical')
% Select locations and label
nt = ceil((max(pos.Time) - min(pos.Time))/6);
10-57
10 Timetables
Use plotmatrix to visualize a grid of scatter plots of all variables against one another
and histograms of each variable on the diagonal. The output variable Ax, represents each
axes in the grid and can be used to identify which axes to label using xlabel and
ylabel.
figure
[S,Ax] = plotmatrix(pos.Variables);
for ii = 1:length(varNames)
xlabel(Ax(end,ii),varNames{ii})
10-58
Loma Prieta Earthquake Analysis
ylabel(Ax(ii,1),varNames{ii})
end
Plot a 3-D view of the trajectory and plot a dot at every tenth position point. The spacing
between dots indicates the velocity.
step = 10;
figure
plot3(pos.NorthSouth,pos.EastWest,pos.Vertical,'r')
hold on
plot3(pos.NorthSouth(1:step:end),pos.EastWest(1:step:end),pos.Vertical(1:step:end),'.')
hold off
box on
axis tight
10-59
10 Timetables
xlabel('North-South')
ylabel('East-West')
zlabel('Vertical')
title('Position')
Supporting Functions
function y = velFun(x)
y = (1/200)*cumsum(x);
10-60
See Also
y = y - mean(y);
end
See Also
duration | head | seconds | summary | timerange | timetable | varfun
Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Create Timetables” on page 10-2
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
• “Access Data in Tables” on page 9-44
10-61
10 Timetables
This example shows how to perform a variety of data cleaning, munging, and
preprocessing tasks such as removing missing values and synchronizing time-stamped
data with different timesteps. In addition, data exploration is highlighted including
visualizations and grouped calculations using the timetable data container to:
Import a sample of the bicycle traffic data from a comma-separated text file. The
readtable function returns the data in a table. Display the first eight rows using the
head function.
bikeTbl = readtable('BicycleCounts.csv');
head(bikeTbl)
ans=8×5 table
Timestamp Day Total Westbound Eastbound
___________________ _____________ _____ _________ _________
The data have timestamps, so it is convenient to use a timetable to store and analyze the
data. A timetable is similar to a table, but includes timestamps that are associated with
10-62
Preprocess and Explore Time-stamped Data Using timetable
the rows of data. The timestamps, or row times, are represented by datetime or
duration values. datetime and duration are the recommended data types for
representing points in time or elapsed times, respectively.
Convert bikeTbl into a timetable using the table2timetable function. You must use a
conversion function because readtable returns a table. table2timetable converts the
first datetime or duration variable in the table into the row times of a timetable. The
row times are metadata that label the rows. However, when you display a timetable, the
row times and timetable variables are displayed in a similar fashion. Note that the table
has five variables whereas the timetable has four.
bikeData = table2timetable(bikeTbl);
head(bikeData)
ans=8×5 timetable
Timestamp Day Total Westbound Eastbound
___________________ _____________ _____ _________ _________
Convert the Day variable to categorical. The categorical data type is designed for data
that consists of a finite set of discrete values, such as the names of the days of the week.
List the categories so they display in day order. Use dot subscripting to access variables
by name.
bikeData.Day = categorical(bikeData.Day,{'Sunday','Monday','Tuesday',...
'Wednesday','Thursday','Friday','Saturday'});
10-63
10 Timetables
In a timetable, the times are treated separately from the data variables. Access the
Properties of the timetable to show that the row times are the first dimension of the
timetable, and the variables are the second dimension. The DimensionNames property
shows the names of the two dimensions, while the VariableNames property shows the
names of the variables along the second dimension.
bikeData.Properties
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Timestamp' 'Variables'}
VariableNames: {'Day' 'Total' 'Westbound' 'Eastbound'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowTimes: [9387x1 datetime]
StartTime: 2015-06-24 00:00:00
SampleRate: NaN
TimeStep: NaN
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
ans =
TimetableProperties with properties:
Description: ''
UserData: []
DimensionNames: {'Time' 'Data'}
VariableNames: {'Day' 'Total' 'Westbound' 'Eastbound'}
VariableDescriptions: {}
10-64
Preprocess and Explore Time-stamped Data Using timetable
VariableUnits: {}
VariableContinuity: []
RowTimes: [9387x1 datetime]
StartTime: 2015-06-24 00:00:00
SampleRate: NaN
TimeStep: NaN
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
head(bikeData)
ans=8×5 timetable
Time Day Total Westbound Eastbound
___________________ _________ _____ _________ _________
Determine the number of days that elapsed between the latest and earliest row times. The
variables can be accessed by dot notation when referencing variables one at a time.
elapsedTime = duration
9383:30:00
elapsedTime.Format = 'd'
elapsedTime = duration
390.98 days
To examine the typical bicycle counts on a given day, calculate the means for the total
number of bikes, and the numbers travelling westbound and eastbound.
10-65
10 Timetables
Return the numeric data as a matrix by indexing into the contents of bikeData using
curly braces. Display the first eight rows. Use standard table subscripting to access
multiple variables.
counts = bikeData{:,2:end};
counts(1:8,:)
ans = 8×3
13 9 4
3 3 0
1 1 0
1 1 0
1 1 0
7 3 4
36 6 30
141 13 128
Since the mean is appropriate for only the numeric data, you can use the vartype
function to select the numeric variables. vartype can be more convenient than manually
indexing into a table or timetable to select variables. Calculate the means and omit NaN
values.
counts = bikeData{:,vartype('numeric')};
mean(counts,'omitnan')
ans = 1×3
To determine how many people bicycle during a holiday, examine the data on the 4th of
July holiday. Index into the timetable by row times for July 4, 2015. When you index on
row times, you must match times exactly. You can specify the time indices as datetime or
duration values, or as character vectors that can be converted to dates and times. You
can specify multiple times as an array.
Index into bikeData with specific dates and times to extract data for July 4, 2015. If you
specify the date only, then the time is assumed to be midnight, or 00:00:00.
bikeData('2015-07-04',:)
10-66
Preprocess and Explore Time-stamped Data Using timetable
ans=1×5 timetable
Time Day Total Westbound Eastbound
___________________ ________ _____ _________ _________
ans=2×5 timetable
Time Day Total Westbound Eastbound
___________________ ________ _____ _________ _________
It would be tedious to use this strategy to extract the entire day. You can also specify
ranges of time without indexing on specific times. To create a time range subscript as a
helper, use the timerange function.
Subscript into the timetable using a time range for the entire day of July 4, 2015. Specify
the start time as midnight on July 4, and the end time as midnight on July 5. By default,
timerange covers all times starting with the start time and up to, but not including, the
end time. Plot the bicycle counts over the course of the day.
tr = timerange('2015-07-04','2015-07-05');
jul4 = bikeData(tr,'Total');
head(jul4)
ans=8×2 timetable
Time Total
___________________ _____
2015-07-04 00:00:00 8
2015-07-04 01:00:00 13
2015-07-04 02:00:00 4
2015-07-04 03:00:00 1
2015-07-04 04:00:00 0
2015-07-04 05:00:00 1
2015-07-04 06:00:00 8
2015-07-04 07:00:00 16
10-67
10 Timetables
bar(jul4.Time,jul4.Total)
ylabel('Bicycle Counts')
title('Bicycle Counts on July 4, 2015')
From the plot, there is more volume throughout the day, leveling off in the afternoon.
Because many businesses are closed, the plot does not show typical traffic during
commute hours. Spikes later in the evening can be attributed to celebrations with
fireworks, which occur after dark. To examine these trends more closely, the data should
be compared to data for typical days.
Compare the data for July 4 to data for the rest of the month of July.
jul = bikeData(timerange('2015-07-01','2015-08-01'),:);
plot(jul.Time,jul.Total)
10-68
Preprocess and Explore Time-stamped Data Using timetable
hold on
plot(jul4.Time,jul4.Total)
ylabel('Total Count')
title('Bicycle Counts in July')
hold off
legend('Bicycle Count','July 4 Bicycle Count')
The plot shows variations that can be attributed to traffic differences between weekdays
and weekends. The traffic patterns for July 4 and 5 are consistent with the pattern for
weekend traffic. July 5 is a Monday but is often observed as a holiday. These trends can be
examined more closely with further preprocessing and analysis.
10-69
10 Timetables
Time-stamped data sets are often messy and may contain anomalies or errors. Timetables
are well suited for resolving anomalies and errors.
A timetable does not have to have its row times in any particular order. It can contain
rows that are not sorted by their row times. A timetable can also contain multiple rows
with the same row time, though the rows can have different data values. Even when row
times are sorted and unique, they can differ by time steps of different sizes. A timetable
can even contain NaT or NaN values to indicate missing row times.
The timetable data type provides a number of different ways to resolve missing,
duplicate, or nonuniform times. You can also resample or aggregate data to create a
regular timetable. When a timetable is regular, it has row times that are sorted and
unique, and have a uniform or evenly spaced time step between them.
10-70
Preprocess and Explore Time-stamped Data Using timetable
Determine if the timetable is sorted. A timetable is sorted if its row times are listed in
ascending order.
issorted(bikeData)
ans = logical
0
Sort the timetable. The sortrows function sorts the rows by their row times, from
earliest to latest time. If there are rows with duplicate row times, then sortrows copies
all the duplicates to the output.
bikeData = sortrows(bikeData);
issorted(bikeData)
10-71
10 Timetables
ans = logical
1
A timetable can have missing data indicators in its variables or its row times. For
example, you can indicate missing numeric values as NaNs, and missing datetime values
as NaTs. You can assign, find, remove, and fill missing values with the
standardizeMissing, ismissing, rmmissing, and fillmissing functions,
respectively.
Find and count the missing values in the timetable variables. In this example, missing
values indicate circumstances when no data were collected.
missData = ismissing(bikeData);
sum(missData)
ans = 1×4
1 3 3 3
The output from ismissing is a logical matrix, the same size as the table, identifying
missing data values as true. Display any rows which have missing data indicators.
idx = any(missData,2);
bikeData(idx,:)
ans=3×5 timetable
Time Day Total Westbound Eastbound
___________________ ___________ _____ _________ _________
ismissing(bikeData) finds missing data in the timetable variables only, not the times.
To find missing row times, call ismissing on the row times.
missTimes = ismissing(bikeData.Time);
bikeData(missTimes,:)
ans=2×5 timetable
Time Day Total Westbound Eastbound
10-72
Preprocess and Explore Time-stamped Data Using timetable
In this example, missing times or data values indicate measurement errors and can be
excluded. Remove rows of the table containing missing data values and missing row times
using rmmissing.
bikeData = rmmissing(bikeData);
sum(ismissing(bikeData))
ans = 1×4
0 0 0 0
sum(ismissing(bikeData.Time))
ans = 0
Determine if there are duplicate times and/or duplicate rows of data. You might want to
exclude exact duplicates, as these can also be considered measurement errors. Identify
duplicate times by finding where the difference between the sorted times is exactly zero.
idx = diff(bikeData.Time) == 0;
dup = bikeData.Time(idx)
Three times are repeated and November 19, 2015 is repeated twice. Examine the data
associated with the repeated times.
bikeData(dup(1),:)
ans=2×5 timetable
Time Day Total Westbound Eastbound
___________________ ______ _____ _________ _________
10-73
10 Timetables
bikeData(dup(2),:)
ans=3×5 timetable
Time Day Total Westbound Eastbound
___________________ ________ _____ _________ _________
The first has duplicated times but non-duplicate data, whereas the others are entirely
duplicated. Timetable rows are considered duplicates when they contain identical row
times and identical data values across the rows. You can use unique to remove duplicate
rows in the timetable. The unique function also sorts the rows by their row times.
bikeData = unique(bikeData);
The rows with duplicate times but non-duplicate data require some interpretation.
Examine the data around those times.
d = dup(1) + hours(-2:2);
bikeData(d,:)
ans=5×5 timetable
Time Day Total Westbound Eastbound
___________________ ________ _____ _________ _________
In this case, the duplicate time may have been mistaken since the data and surrounding
times are consistent. Though it appears to represent 01:00:00, it is uncertain what time
this should have been. The data can be accumulated to account for the data at both time
points.
10-74
Preprocess and Explore Time-stamped Data Using timetable
sum(bikeData{dup(1),2:end})
ans = 1×3
25 16 9
This is only one case which can be done manually. However, for many rows, the retime
function can perform this calculation. Accumulate the data for the unique times using the
sum function to aggregate. The sum is appropriate for numeric data but not the
categorical data in the timetable. Use vartype to identify the numeric variables.
vt = vartype('numeric');
t = unique(bikeData.Time);
numData = retime(bikeData(:,vt),t,'sum');
head(numData)
ans=8×4 timetable
Time Total Westbound Eastbound
___________________ _____ _________ _________
2015-06-24 00:00:00 13 9 4
2015-06-24 01:00:00 3 3 0
2015-06-24 02:00:00 1 1 0
2015-06-24 03:00:00 1 1 0
2015-06-24 04:00:00 1 1 0
2015-06-24 05:00:00 7 3 4
2015-06-24 06:00:00 36 6 30
2015-06-24 07:00:00 141 13 128
You cannot sum the categorical data, but since one label represents the whole day, take
the first value on each day. You can perform the retime operation again with the same
time vector and concatenate the timetables together.
vc = vartype('categorical');
catData = retime(bikeData(:,vc),t,'firstvalue');
bikeData = [catData,numData];
bikeData(d,:)
ans=4×5 timetable
Time Day Total Westbound Eastbound
___________________ ________ _____ _________ _________
10-75
10 Timetables
The data appear to have a uniform time step of one hour. To determine if this is true for
all the row times in the timetable, use the isregular function. isregular returns true
for sorted, evenly-spaced times (monotonically increasing), with no duplicate or missing
times (NaT or NaN).
isregular(bikeData)
ans = logical
0
The output of 0, or false, indicates that the times in the timetable are not evenly spaced.
Explore the time interval in more detail.
dt = diff(bikeData.Time);
[min(dt); max(dt)]
To put the timetable on a regular time interval, use retime or synchronize and specify
the time interval of interest.
Determine the counts per day using the retime function. Accumulate the count data for
each day using the sum method. This is appropriate for numeric data but not the
categorical data in the timetable. Use vartype to identify the variables by data type.
dayCountNum = retime(bikeData(:,vt),'daily','sum');
head(dayCountNum)
ans=8×4 timetable
Time Total Westbound Eastbound
___________________ _____ _________ _________
10-76
Preprocess and Explore Time-stamped Data Using timetable
As above, you can perform the retime operation again to represent the categorical data
using an appropriate method and concatenate the timetables together.
dayCountCat = retime(bikeData(:,vc),'daily','firstvalue');
dayCount = [dayCountCat,dayCountNum];
head(dayCount)
ans=8×5 timetable
Time Day Total Westbound Eastbound
___________________ _________ _____ _________ _________
Examine the effect of weather on cycling behavior by comparing the bicycle count with
weather data. Load the weather timetable which includes historical weather data from
Boston, MA including storm events.
load BostonWeatherData
head(weatherData)
ans=8×4 timetable
Time TemperatureF Humidity Events
___________ ____________ ________ ____________
01-Jul-2015 72 78 Thunderstorm
10-77
10 Timetables
02-Jul-2015 72 60 None
03-Jul-2015 70 56 None
04-Jul-2015 67 75 None
05-Jul-2015 72 67 None
06-Jul-2015 74 69 None
07-Jul-2015 75 77 Rain
08-Jul-2015 79 68 Rain
To summarize the times and variables in the timetable, use the summary function.
summary(weatherData)
RowTimes:
Variables:
Values:
Min 2
Median 55
Max 85
Values:
Min 29
Median 64
Max 97
Values:
Fog 7
10-78
Preprocess and Explore Time-stamped Data Using timetable
Hail 1
Rain 108
Rain-Snow 4
Snow 18
Thunderstorm 12
None 233
Combine the bicycle data with the weather data to a common time vector using
synchronize. You can resample or aggregate timetable data using any of the methods
documented on the reference page for the synchronize function.
Synchronize the data from both timetables to a common time vector, constructed from the
intersection of their individual daily time vectors.
data = synchronize(dayCount,weatherData,'intersection');
head(data)
ans=8×8 timetable
Time Day Total Westbound Eastbound TemperatureF
___________________ _________ _____ _________ _________ ____________
Compare bicycle traffic counts and outdoor temperature on separate y axes to examine
the trends. Remove the weekends from the data for visualization.
idx = ~isweekend(data.Time);
weekdayData = data(idx,{'TemperatureF','Total'});
figure
yyaxis left
plot(weekdayData.Time, weekdayData.Total)
ylabel('Bicycle Count')
yyaxis right
plot(weekdayData.Time,weekdayData.TemperatureF)
ylabel('Temperature (\circ F)')
title('Bicycle Counts and Temperature Over Time')
xlim([min(data.Time) max(data.Time)])
10-79
10 Timetables
The plot shows that the traffic and weather data might follow similar trends. Zoom in on
the plot.
xlim([datetime('2015-11-01'),datetime('2016-05-01')])
10-80
Preprocess and Explore Time-stamped Data Using timetable
The trends are similar, indicating that fewer people cycle on colder days.
Examine the data based on different intervals such as day of the week and time of day.
Determine the total counts per day using varfun to perform grouped calculations on
variables. Specify the sum function with a function handle and the grouping variable and
preferred output type using name-value pairs.
byDay = varfun(@sum,bikeData,'GroupingVariables','Day',...
'OutputFormat','table')
byDay=7×5 table
Day GroupCount sum_Total sum_Westbound sum_Eastbound
10-81
10 Timetables
figure
bar(byDay{:,{'sum_Westbound','sum_Eastbound'}})
legend({'Westbound','Eastbound'},'Location','eastoutside')
xticklabels({'Sun','Mon','Tue','Wed','Thu','Fri','Sat'})
title('Bicycle Count by Day of Week')
10-82
Preprocess and Explore Time-stamped Data Using timetable
The bar plot indicates that traffic is heavier on weekdays. Also, there is a difference in the
Eastbound and Westbound directions. This might indicate that people tend to take
different routes when entering and leaving the city. Another possibility is that some
people enter on one day and return on another day.
Determine the hour of day and use varfun for calculations by group.
bikeData.HrOfDay = hour(bikeData.Time);
byHr = varfun(@mean,bikeData(:,{'Westbound','Eastbound','HrOfDay'}),...
'GroupingVariables','HrOfDay','OutputFormat','table');
head(byHr)
ans=8×4 table
HrOfDay GroupCount mean_Westbound mean_Eastbound
10-83
10 Timetables
bar(byHr{:,{'mean_Westbound','mean_Eastbound'}})
legend('Westbound','Eastbound','Location','eastoutside')
xlabel('Hour of Day')
ylabel('Bicycle Count')
title('Mean Bicycle Count by Hour of Day')
10-84
Preprocess and Explore Time-stamped Data Using timetable
There are traffic spikes at the typical commute hours, around 9:00 a.m. and 5:00 p.m.
Also, the trends between the Eastbound and Westbound directions are different. In
general, the Westbound direction is toward residential areas surrounding the Cambridge
area and toward the Universities. The Eastbound direction is toward Boston.
The traffic is heavier later in the day in the Westbound direction compared to the
Eastbound direction. This might indicate university schedules and traffic due to
restaurants in the area. Examine the trend by day of week as well as hour of day.
byHrDay = varfun(@sum,bikeData,'GroupingVariables',{'HrOfDay','Day'},...
'OutputFormat','table');
head(byHrDay)
10-85
10 Timetables
ans=8×6 table
HrOfDay Day GroupCount sum_Total sum_Westbound sum_Eastbound
_______ _________ __________ _________ _____________ _____________
To arrange the timetable so that the days of the week are the variables, use the unstack
function.
hrAndDayWeek = unstack(byHrDay(:,{'HrOfDay','Day','sum_Total'}),'sum_Total','Day');
head(hrAndDayWeek)
ans=8×8 table
HrOfDay Sunday Monday Tuesday Wednesday Thursday Friday Saturd
_______ ______ ______ _______ _________ ________ ______ ______
ribbon(hrAndDayWeek.HrOfDay,hrAndDayWeek{:,2:end})
ylim([0 24])
xlim([0 8])
xticks(1:7)
xticklabels({'Sun','Mon','Tue','Wed','Thu','Fri','Sat'})
ylabel('Hour')
title('Bicycle Count by Hour and Day of Week')
10-86
Preprocess and Explore Time-stamped Data Using timetable
There are similar trends for the regular work days of Monday through Friday, with peaks
at rush hour and traffic tapering off in the evening. Friday has less volume, though the
overall trend is similar to the other work days. The trends for Saturday and Sunday are
similar to each other, without rush hour peaks and with more volume later in the day. The
late evening trends are also similar for Monday through Friday, with less volume on
Friday.
To examine the overall time of day trends, split up the data by rush hour times. It is
possible to use different times of day or units of time using the discretize function. For
example, separate the data into groups for AM, AMRush, Day, PMRush, PM. Then use
varfun to calculate the mean by group.
10-87
10 Timetables
bikeData.HrLabel = discretize(bikeData.HrOfDay,[0,6,10,15,19,24],'categorical',...
{'AM','RushAM','Day','RushPM','PM'});
byHrBin = varfun(@mean,bikeData(:,{'Total','HrLabel'}),'GroupingVariables','HrLabel',..
'OutputFormat','table')
byHrBin=5×3 table
HrLabel GroupCount mean_Total
_______ __________ __________
AM 2342 3.5508
RushAM 1564 94.893
Day 1955 45.612
RushPM 1564 98.066
PM 1955 35.198
bar(byHrBin.mean_Total)
cats = categories(byHrBin.HrLabel);
xticklabels(cats)
title('Mean Bicycle Count During Rush Hours')
10-88
See Also
In general, there is about twice as much traffic in this area during the evening and
morning rush hours compared to other times of the day. There is very little traffic in this
area in the early morning, but there is still significant traffic in the evening and late
evening, comparable to the day outside of the morning and evening rush hours.
See Also
datetime | head | retime | rmmissing | sortrows | summary | table2timetable |
timerange | timetable | unstack | varfun
10-89
10 Timetables
Related Examples
• “Represent Dates and Times in MATLAB” on page 7-2
• “Create Timetables” on page 10-2
• “Resample and Aggregate Data in Timetable” on page 10-7
• “Clean Timetable with Missing, Duplicate, or Nonuniform Times” on page 10-31
• “Select Timetable Data by Row Time and Variable Type” on page 10-24
10-90
11
Structures
Store a patient record in a scalar structure with fields name, billing, and test.
Add records for other patients to the array by including subscripts after the array name.
11-2
Create Structure Array
patient=2×3 struct
name
billing
test
Each patient record in the array is a structure of class struct. An array of structures is
often referred to as a struct array. Like other MATLAB arrays, a struct array can have any
dimensions.
11-3
11 Structures
Any unspecified fields for new structs in the array contain empty arrays.
Access data in the structure array to find how much the first patient owes, and to create a
bar graph of his test results.
amount_due = patient(1).billing
amount_due = 127
bar(patient(1).test)
title(['Test Results for ', patient(1).name])
11-4
See Also
See Also
Related Examples
• “Access Data in Structure Array” on page 11-7
• “Create Cell Array” on page 12-3
• “Create and Work with Tables” on page 9-2
11-5
11 Structures
More About
• “Cell vs. Structure Arrays” on page 12-19
• “Advantages of Using Tables” on page 9-74
11-6
Access Data in Structure Array
S = load('flujet.mat')
The variables from the file (X, caption, and map) are now fields in the structure.
Access the data using dot notation of the form structName.fieldName. For example,
pass the numeric data in field X to the image function:
image(S.X)
colormap(S.map)
11-7
11 Structures
To access part of a field, add indices as appropriate for the size and type of data in the
field. For example, pass the center left section of X to the image function.
centerLeft = S.X(150:250,1:50);
image(centerLeft)
11-8
Access Data in Structure Array
If a field contains a cell array, use curly braces to access the data, such as
S.cellField{1:50,1:80}.
Each element of a structure array must have the same fields. Both flujet.mat and
cape.mat contain variables X, map, and caption. S is a 1-by-2 array.
S(2) = load('cape.mat')
11-9
11 Structures
S=2×3 struct
X
map
caption
image(S(2).X)
colormap(S(2).map)
11-10
Access Data in Structure Array
capeSection = S(2).X(200:300,150:250);
image(capeSection)
Note You can index into part of a field only when you refer to a single element of a
structure array. MATLAB does not support statements such as S(1:2).X(1:50,1:80),
which attempt to index into a field for multiple elements of the structure.
11-11
11 Structures
See Also
Related Examples
• “Access Data in Nested Structures” on page 11-17
• “Access Elements of a Nonscalar Struct Array” on page 11-20
• “Generate Field Names from Variables” on page 11-15
11-12
Concatenate Structures
Concatenate Structures
This example shows how to concatenate structure arrays using the [] operator. To
concatenate structures, they must have the same set of fields, but the fields do not need
to contain the same sizes or types of data.
Create scalar (1-by-1) structure arrays struct1 and struct2, each with fields a and b:
struct1.a = 'first';
struct1.b = [1,2,3];
struct2.a = 'second';
struct2.b = rand(5);
struct1,struct2
Just as concatenating two scalar values such as [1,2] creates a 1-by-2 numeric array,
concatenating struct1 and struct2 creates a 1-by-2 structure array.
combined = [struct1,struct2]
combined=2×2 struct
a
b
When you want to access the contents of a particular field, specify the index of the
structure in the array. For example, access field a of the first structure.
combined(1).a
ans =
'first'
Concatenation also applies to nonscalar structure arrays. For example, create a 2-by-2
structure array named new. Because the 1-by-2 structure combined and the 2-by-2
11-13
11 Structures
structure new both have two columns, you can concatenate them vertically with a
semicolon separator.
new(1,1).a = 1;
new(1,1).b = 10;
new(1,2).a = 2;
new(1,2).b = 20;
new(2,1).a = 3;
new(2,1).b = 30;
new(2,2).a = 4;
new(2,2).b = 40;
larger=3×2 struct
a
b
Access field a of the structure larger(2,1). It contains the same value as new(1,1).a.
larger(2,1).a
ans = 1
See Also
Related Examples
• “Creating, Concatenating, and Expanding Matrices”
• “Access Data in Structure Array” on page 11-7
• “Access Elements of a Nonscalar Struct Array” on page 11-20
11-14
Generate Field Names from Variables
structName.(dynamicExpression)
currentDate = datestr(now,'mmmdd');
myStruct.(currentDate) = [1,2,3]
If the current date reported by your system is February 29, then this code assigns data to
a field named Feb29:
myStruct =
Feb29: [1 2 3]
The dynamic fieldname can return either a character vector or a string scalar. For
example, you can specify the field Feb29 using either single or, starting in R2017b,
double quotes.
myStruct.('Feb29')
ans =
1 2 3
myStruct.("Feb29")
ans =
1 2 3
Field names, like variable names, must begin with a letter, can contain letters, digits, or
underscore characters, and are case sensitive. To avoid potential conflicts, do not use the
names of existing variables or functions as field names.
See Also
fieldnames | getfield | setfield | struct
11-15
11 Structures
Related Examples
• “Variable Names” on page 1-6
• “Create Structure Array” on page 11-2
• “Access Data in Structure Array” on page 11-7
11-16
Access Data in Nested Structures
structName(index).nestedStructName(index).fieldName(indices)
When a structure is scalar (1-by-1), you do not need to include the indices to refer to the
single element. For example, create a scalar structure s, where field n is a nested scalar
structure with fields a, b, and c:
s.n.a = ones(3);
s.n.b = eye(4);
s.n.c = magic(5);
third_row_b = s.n.b(3,:)
third_row_b =
0 0 1 0
s(1).n(2).a = 2*ones(3);
s(1).n(2).b = 2*eye(4);
s(1).n(2).c = 2*magic(5);
s(2).n(1).a = '1a';
s(2).n(2).a = '2a';
s(2).n(1).b = '1b';
s(2).n(2).b = '2b';
s(2).n(1).c = '1c';
s(2).n(2).c = '2c';
11-17
11 Structures
s(1)
.n(1) .a 1 1 1 .n(2) .a 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
.b 1 0 0 0 .b 2 0 0 0
0 1 0 0 0 2 0 0
0 0 1 0 0 0 2 0
0 0 0 1 0 0 0 2
.c 17 24 1 8 15 .c 34 48 2 16 30
23 5 7 14 16 46 10 14 28 32
4 6 13 20 22 8 12 26 40 44
10 12 19 21 3 20 24 38 42 6
11 18 25 2 9 22 36 50 4 18
s(2)
.n(1) .a 1a .n(2) .a 2a
.b 1b .b 2b
.c 1c .c 2c
Access part of the array in field b of the second element in n within the first element of s:
part_two_eye = s(1).n(2).b(1:2,1:2)
part_two_eye =
2 0
0 2
See Also
getfield | setfield | struct
11-18
See Also
Related Examples
• “Create Structure Array” on page 11-2
• “Access Data in Structure Array” on page 11-7
• “Ways to Organize Data in Structure Arrays” on page 11-22
11-19
11 Structures
s(1).f = 1;
s(2).f = 'two';
s(3).f = 3 * ones(3);
Although each structure in the array must have the same number of fields and the same
field names, the contents of the fields can be different types and sizes. When you refer to
field f for multiple elements of the structure array, such as
s(1:3).f
or
s.f
MATLAB returns the data from the elements in a comma-separated list, which displays as
follows:
ans =
1
ans =
two
ans =
3 3 3
3 3 3
3 3 3
You cannot assign the list to a single variable with the syntax v = s.f because the fields
can contain different types of data. However, you can assign the list items to the same
number of variables, such as
c = {s.f};
11-20
Access Elements of a Nonscalar Struct Array
If all of the fields contain the same type of data and can form a hyperrectangle, you can
concatenate the list items. For example, create a structure nums with scalar numeric
values in field f, and concatenate the data from the fields:
nums(1).f = 1;
nums(2).f = 2;
nums(3).f = 3;
allNums = [nums.f]
allNums =
1 2 3
If you want to process each element of an array with the same operation, use the
arrayfun function. For example, count the number of elements in field f of each struct
in array s:
The syntax @(x) creates an anonymous function. This code calls the numel function for
each element of array s, such as numel(s(1).f), and returns
numElements =
1 3 9
11-21
11 Structures
Plane organization allows easier access to all values within a field. Element-by-element
organization allows easier access to all information related to a single element or record.
The following sections include an example of each type of organization:
When you create a structure array, MATLAB stores information about each element and
field in the array header. As a result, structures with more elements and fields require
more memory than simpler structures that contain the same data.
Plane Organization
Consider an RGB image with three arrays corresponding to color intensity values.
11-22
Ways to Organize Data in Structure Arrays
If you have arrays RED, GREEN, and BLUE in your workspace, then these commands create
a scalar structure named img that uses plane organization:
img.red = RED;
img.green = GREEN;
img.blue = BLUE;
Plane organization allows you to easily extract entire image planes for display, filtering, or
other processing. For example, multiply the red intensity values by 0.9:
adjustedRed = .9 * img.red;
If you have multiple images, you can add them to the img structure, so that each element
img(1),...,img(n) contains an entire image. For an example that adds elements to a
structure, see the following section.
11-23
11 Structures
Element-by-Element Organization
Consider a database with patient information. Each record contains data for the patient’s
name, test results, and billing amount.
Additional patients correspond to new elements in the structure. For example, add an
element for a second patient:
patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;
patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];
11-24
See Also
178.5000
212.0000
See Also
struct
More About
• “Access Data in Structure Array” on page 11-7
• “Access Elements of a Nonscalar Struct Array” on page 11-20
• “Memory Requirements for Structure Array” on page 11-26
11-25
11 Structures
Preallocate memory for the contents by assigning initial values with the struct function,
such as
newStruct(1:25,1:50) = struct('a',ones(20),'b',zeros(30),'c',rand(40));
This code creates and populates a 25-by-50 structure array S with fields a, b, and c.
If you prefer not to assign initial values, you can initialize a structure array by assigning
empty arrays to each field of the last element in the structure array, such as
newStruct(25,50).a = [];
newStruct(25,50).b = [];
newStruct(25,50).c = [];
or, equivalently,
newStruct(25,50) = struct('a',[],'b',[],'c',[]);
However, in this case, MATLAB only allocates memory for the header, and not for the
contents of the array.
11-26
12
Cell Arrays
There are two ways to refer to the elements of a cell array. Enclose indices in smooth
parentheses, (), to refer to sets of cells — for example, to define a subset of the array.
Enclose indices in curly braces, {}, to refer to the text, numbers, or other data within
individual cells.
12-2
Create Cell Array
When you have data to put into a cell array, create the array using the cell array
construction operator, {}.
myCell = {1, 2, 3;
'text', rand(5,10,2), {11; 22; 33}}
myCell=2×3 cell
{[ 1]} {[ 2]} {[ 3]}
{'text'} {5x10x2 double} {3x1 cell}
Like all MATLAB® arrays, cell arrays are rectangular, with the same number of cells in
each row. myCell is a 2-by-3 cell array.
You also can use the {} operator to create an empty 0-by-0 cell array.
C = {}
C =
To add values to a cell array over time or in a loop, create an empty N-dimensional array
using the cell function.
emptyCell = cell(3,4,2)
emptyCell(:,:,2) =
12-3
12 Cell Arrays
emptyCell is a 3-by-4-by-2 cell array, where each cell contains an empty array, [].
See Also
cell
Related Examples
• “Access Data in Cell Array” on page 12-5
• “Create Structure Array” on page 11-2
• “Create and Work with Tables” on page 9-2
More About
• “Cell vs. Structure Arrays” on page 12-19
• “Advantages of Using Tables” on page 9-74
12-4
Access Data in Cell Array
C=2×3 cell
{'one'} {'two'} {'three'}
{[ 1]} {[ 2]} {[ 3]}
There are two ways to refer to the elements of a cell array. Enclose indices in smooth
parentheses, (), to refer to sets of cells--for example, to define a subset of the array.
Enclose indices in curly braces, {}, to refer to the text, numbers, or other data within
individual cells.
Cell array indices in smooth parentheses refer to sets of cells. For example, to create a 2-
by-2 cell array that is a subset of C, use smooth parentheses.
upperLeft = C(1:2,1:2)
upperLeft=2×2 cell
{'one'} {'two'}
{[ 1]} {[ 2]}
Update sets of cells by replacing them with the same number of cells. For example,
replace cells in the first row of C with an equivalent-sized (1-by-3) cell array.
C(1,1:3) = {'first','second','third'}
C=2×3 cell
{'first'} {'second'} {'third'}
{[ 1]} {[ 2]} {[ 3]}
If cells in your array contain numeric data, you can convert the cells to a numeric array
using the cell2mat function.
numericCells = C(2,1:3)
12-5
12 Cell Arrays
numericCells=1×3 cell
{[1]} {[2]} {[3]}
numericVector = cell2mat(numericCells)
numericVector = 1×3
1 2 3
Access the contents of cells--the numbers, text, or other data within the cells--by indexing
with curly braces. For example, to access the contents of the last cell of C, use curly
braces.
last = C{2,3}
last = 3
last is a numeric variable of type double, because the cell contains a double value.
Similarly, you can index with curly braces to replace the contents of a cell.
C{2,3} = 300
C=2×3 cell
{'first'} {'second'} {'third'}
{[ 1]} {[ 2]} {[ 300]}
You can access the contents of multiple cells by indexing with curly braces. MATLAB®
returns the contents of the cells as a comma-separated list. Because each cell can contain
a different type of data, you cannot assign this list to a single variable. However, you can
assign the list to the same number of variables as cells. MATLAB® assigns to the
variables in column order.
12-6
See Also
r1c1 =
'first'
r2c1 = 1
r1c2 =
'second'
r2c2 = 2
If each cell contains the same type of data, you can create a single variable by applying
the array concatenation operator, [], to the comma-separated list.
nums = [C{2,:}]
nums = 1×3
1 2 300
See Also
cell | cell2mat
Related Examples
• “Create Cell Array” on page 12-3
• “Multilevel Indexing to Access Parts of Cells” on page 12-25
• “Comma-Separated Lists” on page 2-101
12-7
12 Cell Arrays
C = {1, 2, 3}
C=1×3 cell
{[1]} {[2]} {[3]}
Assign data to a cell outside the current dimensions. MATLAB® expands the cell array to
a rectangle that includes the specified subscripts. Any intervening cells contain empty
arrays.
C{4,4} = 44
C=4×4 cell
{[ 1]} {[ 2]} {[ 3]} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {[ 44]}
Add cells without specifying a value by assigning an empty array as the contents of a cell.
C is now a 5-by-5 cell array.
C{5,5} = []
C=5×5 cell
Columns 1 through 4
Column 5
{0x0 double}
{0x0 double}
{0x0 double}
12-8
See Also
{0x0 double}
{0x0 double}
See Also
Related Examples
• “Access Data in Cell Array” on page 12-5
• “Combine Cell Arrays” on page 12-12
• “Delete Data from Cell Array” on page 12-10
12-9
12 Cell Arrays
C = {1, 2, 3; 4, 5, 6; 7, 8, 9}
C=3×3 cell
{[1]} {[2]} {[3]}
{[4]} {[5]} {[6]}
{[7]} {[8]} {[9]}
Delete the contents of a particular cell by assigning an empty array to the cell, using curly
braces for content indexing, {}.
C{2,2} = []
C=3×3 cell
{[1]} {[ 2]} {[3]}
{[4]} {0x0 double} {[6]}
{[7]} {[ 8]} {[9]}
Delete sets of cells using standard array indexing with smooth parentheses, (). For
example, remove the second row of C.
C(2,:) = []
C=2×3 cell
{[1]} {[2]} {[3]}
{[7]} {[8]} {[9]}
See Also
Related Examples
• “Add Cells to Cell Array” on page 12-8
12-10
See Also
12-11
12 Cell Arrays
C1 = {1, 2, 3};
C2 = {'A', 'B', 'C'};
C3 = {10, 20, 30};
Concatenate cell arrays with the array concatenation operator, []. In this example,
vertically concatenate the cell arrays by separating them with semicolons:
C4 =
[ 1] [ 2] [ 3]
'A' 'B' 'C'
[10] [20] [30]
Create a nested cell array with the cell array construction operator, {}:
C5 =
{1x3 cell}
{1x3 cell}
{1x3 cell}
To combine cell arrays of character vectors into one character vector, use the strjoin
function.
See Also
strjoin
Related Examples
• “Creating, Concatenating, and Expanding Matrices”
12-12
Pass Contents of Cell Arrays to Functions
Pass the contents of a single cell by indexing with curly braces, {}.
This example creates a cell array that contains text and a 20-by-2 array of random
numbers.
12-13
12 Cell Arrays
Plot only the first column of data by indexing further into the content (multilevel
indexing).
figure
plot(randCell{1,2}(:,1))
title('First Column of Data')
Combine numeric data from multiple cells using the cell2mat function.
This example creates a 5-by-2 cell array that stores temperature data for three cities, and
plots the temperatures for each city by date.
12-14
Pass Contents of Cell Arrays to Functions
allTemps = cell2mat(temperature(:,2));
dates = datenum(temperature(:,1), 'dd-mmm-yyyy');
plot(dates, allTemps)
datetick('x','mmm')
This example plots X against Y , and applies line styles from a 2-by-3 cell array C.
12-15
12 Cell Arrays
X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));
12-16
See Also
See Also
More About
• “Access Data in Cell Array” on page 12-5
• “Multilevel Indexing to Access Parts of Cells” on page 12-25
• “Comma-Separated Lists” on page 2-101
12-17
12 Cell Arrays
Cell arrays do not require completely contiguous memory. However, each cell requires
contiguous memory, as does the cell array header that MATLAB creates to describe the
array. For very large arrays, incrementally increasing the number of cells or the number
of elements in a cell results in Out of Memory errors.
Initialize a cell array by calling the cell function, or by assigning to the last element. For
example, these statements are equivalent:
C = cell(25,50);
C{25,50} = [];
MATLAB creates the header for a 25-by-50 cell array. However, MATLAB does not allocate
any memory for the contents of each cell.
See Also
cell
Related Examples
• “Reshaping and Rearranging Arrays”
• “How MATLAB Allocates Memory” on page 30-13
12-18
Cell vs. Structure Arrays
Structure Arrays
patient
patient=2×3 struct
name
billing
test
numPatients = numel(patient);
for p = 1:numPatients
figure
bar(patient(p).test)
title(patient(p).name)
xlabel('Test')
ylabel('Result')
end
12-19
12 Cell Arrays
12-20
Cell vs. Structure Arrays
Cell Arrays
Cell arrays contain data in cells that you access by numeric indexing. Common
applications of cell arrays include storing separate pieces of text and storing
heterogeneous data from spreadsheets.
For example, store temperature data for three cities over time in a cell array.
12-21
12 Cell Arrays
temperature
temperature=5×2 cell
{'2009-12-31'} {1x3 double}
{'2010-04-03'} {1x3 double}
{'2010-06-20'} {1x3 double}
{'2010-09-15'} {1x3 double}
{'2010-12-09'} {1x3 double}
allTemps = cell2mat(temperature(:,2));
dates = datetime(temperature(:,1));
plot(dates,allTemps)
title('Temperature Trends for Different Locations')
xlabel('Date')
ylabel('Degrees (Fahrenheit)')
12-22
See Also
Struct and cell arrays are the most commonly used containers for storing heterogeneous
data. Tables are convenient for storing heterogeneous column-oriented or tabular data.
Alternatively, use map containers, or create your own class.
See Also
cell | cell2mat | containers.Map | datetime | plot | struct | table
12-23
12 Cell Arrays
Related Examples
• “Access Data in Cell Array” on page 12-5
• “Access Data in Structure Array” on page 11-7
• “Access Data in Tables” on page 9-44
More About
• “Advantages of Using Tables” on page 9-74
12-24
Multilevel Indexing to Access Parts of Cells
C = {myNum, 100*myNum;
myCell, myStruct}
C=2×2 cell
{1x3 double} {1x3 double}
{1x2 cell } {1x1 struct}
Access the complete contents of a particular cell using curly braces, {}. For example,
return a numeric vector from the cell that contains it.
C{1,2}
ans = 1×3
Access part of the contents of a cell by appending indices, using syntax that matches the
data type of the contents.
Enclose numeric indices in smooth parentheses. For example, C{1,1} returns the 1-by-3
numeric vector, [1 2 3]. Access the second element of that vector using smooth
parentheses.
C{1,1}(1,2)
ans = 2
Enclose cell array indices in curly braces. For example, C{2,1} returns the cell array,
{'one','two'}. Access the contents of the second cell within that cell array using curly
braces.
12-25
12 Cell Arrays
C{2,1}{1,2}
ans =
'two'
Refer to fields of a struct array with dot notation, and index into the array as described
for numeric and cell arrays. For example, C{2,2} returns a structure array, where
Field2 contains a 5-by-5 numeric array of fives. Access the element in the fifth row and
first column of that field using dot notation and smooth parentheses.
C{2,2}.Field2(5,1)
ans = 5
You can nest any number of cell and structure arrays. For example, add nested cells and
structures to C.
Access parts of the new data using curly braces, smooth parentheses, or dot notation.
copy_pi = C{2,1}{2,2}{1,1}
copy_pi = 3.1416
part_magic = C{2,2}.Field3.NestedField2(1:2,1:2)
part_magic = 2×2
16 2
5 11
nested_cell = C{2,2}.Field3.NestedField3{2,1}
nested_cell =
'more text'
12-26
See Also
See Also
Related Examples
• “Access Data in Cell Array” on page 12-5
12-27
13
Function Handles
You can create function handles to named and anonymous functions. You can store
multiple function handles in an array, and save and load them, as you would any other
variable.
• Pass a function to another function (often called function functions). For example,
passing a function to integration and optimization functions, such as integral and
fzero.
• Specify callback functions. For example, a callback that responds to a UI event or
interacts with data acquisition hardware.
• Construct handles to functions defined inline instead of stored in a program file
(anonymous functions).
• Call local functions from outside the main function.
f = @myfunction;
13-2
Create Function Handle
You call a function using a handle the same way you call the function directly. For
example, suppose that you have a function named computeSquare, defined as:
function y = computeSquare(x)
y = x.^2;
end
Create a handle and call the function to compute the square of four.
f = @computeSquare;
a = 4;
b = f(a)
b =
16
If the function does not require any inputs, then you can call the function with empty
parentheses, such as
h = @ones;
a = h()
a =
a = h
a =
@ones
Function handles are variables that you can pass to other functions. For example,
calculate the integral of x2 on the range [0,1].
q = integral(f,0,1);
Function handles store their absolute path, so when you have a valid handle, you can
invoke the function from any location. You do not have to specify the path to the function
when creating the handle, only the function name.
13-3
13 Function Handles
• Name length — Each part of the function name (including package and class names)
must be less than the number specified by namelengthmax. Otherwise, MATLAB
truncates the latter part of the name.
• Scope — The function must be in scope at the time you create the handle. Therefore,
the function must be on the MATLAB path or in the current folder. Or, for handles to
local or nested functions, the function must be in the current file.
• Precedence — When there are multiple functions with the same name, MATLAB uses
the same precedence rules to define function handles as it does to call functions. For
more information, see “Function Precedence Order” on page 20-43.
• Overloading — If the function you specify overloads a function in a class that is not a
fundamental MATLAB class, the function is not associated with the function handle at
the time it is constructed. Instead, MATLAB considers the input arguments and
determines which implementation to call at the time of evaluation.
Anonymous Functions
You can create handles to anonymous functions. An anonymous function is a one-line
expression-based MATLAB function that does not require a program file. Construct a
handle to an anonymous function by defining the body of the function,
anonymous_function, and a comma-separated list of input arguments to the
anonymous function, arglist. The syntax is:
h = @(arglist)anonymous_function
For example, create a handle, sqr, to an anonymous function that computes the square of
a number, and call the anonymous function using its handle.
x =
13-4
See Also
ans =
-1
ans =
See Also
func2str | functions | isa | str2func
Related Examples
• “Pass Function to Another Function” on page 13-6
More About
• “Anonymous Functions” on page 20-24
13-5
13 Function Handles
For example, to find the integral of the natural log from 0 through 5, pass a handle to the
log function to integral.
a = 0;
b = 5;
q1 = integral(@log,a,b)
q1 = 3.0472
Similarly, to find the integral of the sin function and the exp function, pass handles to
those functions to integral.
q2 = integral(@sin,a,b)
q2 = 0.7163
q3 = integral(@exp,a,b)
q3 = 147.4132
fun = @(x)x./(exp(x)-1);
q4 = integral(fun,0,Inf)
q4 = 1.6449
Functions that take a function as an input (called function functions) expect that the
function associated with the function handle has a certain number of input variables. For
example, if you call integral or fzero, the function associated with the function handle
must have exactly one input variable. If you call integral3, the function associated with
the function handle must have three input variables. For information on calling function
functions with more variables, see “Parameterizing Functions”.
13-6
See Also
See Also
Related Examples
• “Create Function Handle” on page 13-2
• “Parameterizing Functions”
More About
• “Anonymous Functions” on page 20-24
13-7
13 Function Handles
Create the following function in a file, ellipseVals.m, in your working folder. The
function returns a struct with handles to the local functions.
function fh = ellipseVals
fh.focus = @computeFocus;
fh.eccentricity = @computeEccentricity;
fh.area = @computeArea;
end
function f = computeFocus(a,b)
f = sqrt(a^2-b^2);
end
function e = computeEccentricity(a,b)
f = computeFocus(a,b);
e = f/a;
end
function ae = computeArea(a,b)
ae = pi*a*b;
end
h =
focus: @computeFocus
eccentricity: @computeEccentricity
13-8
See Also
area: @computeArea
Call a local function using its handle to compute the area of an ellipse.
h.area(3,1)
ans =
9.4248
Alternatively, you can use the localfunctions function to create a cell array of function
handles from all local functions automatically. This approach is convenient if you expect to
add, remove, or modify names of the local functions.
See Also
localfunctions
Related Examples
• “Create Function Handle” on page 13-2
More About
• “Local Functions” on page 20-30
13-9
13 Function Handles
MATLAB® considers function handles that you construct from the same named function
to be equal. The isequal function returns a value of true when comparing these types of
handles.
fun1 = @sin;
fun2 = @sin;
isequal(fun1,fun2)
ans =
logical
If you save these handles to a MAT-file, and then load them back into the workspace, they
are still equal.
Unlike handles to named functions, function handles that represent the same anonymous
function are not equal. They are considered unequal because MATLAB cannot guarantee
that the frozen values of nonargument variables are the same. For example, in this case, A
is a nonargument variable.
A = 5;
h1 = @(x)A * x.^2;
h2 = @(x)A * x.^2;
isequal(h1,h2)
ans =
logical
If you make a copy of an anonymous function handle, the copy and the original are equal.
13-10
Compare Function Handles
h1 = @(x)A * x.^2;
h2 = h1;
isequal(h1,h2)
ans =
logical
MATLAB considers function handles to the same nested function to be equal only if your
code constructs these handles on the same call to the function containing the nested
function. This function constructs two handles to the same nested function.
function z = findZ
z = a.^3 + b.^2 + c';
end
end
Function handles constructed from the same nested function and on the same call to the
parent function are considered equal.
[h1,h2] = test_eq(4,19,-7);
isequal(h1,h2)
ans =
logical
Function handles constructed from different calls are not considered equal.
13-11
13 Function Handles
[q1,q2] = test_eq(4,19,-7);
isequal(h1,q1)
ans =
logical
See Also
isequal
Related Examples
• “Create Function Handle” on page 13-2
13-12
14
Map Containers
Indices into the elements of a Map are called keys. These keys, along with the data values
associated with them, are stored within the Map. Each entry of a Map contains exactly
one unique key and its corresponding value. Indexing into the Map of rainfall statistics
shown below with a character vector representing the month of August yields the value
internally associated with that month, 37.3.
KEYS VALUES
Jan 327.2
Feb 368.2
Mar 197.6
Apr 178.4
May 100.0
Jun 69.9
Jul 32.3
Aug Aug 37.3 37.3
Sep 19.0
Oct 37.0
Nov 73.2
Dec 110.9
Annual 1551.0
Keys are not restricted to integers as they are with other arrays. Specifically, a key may
be any of the following types:
14-2
See Also
The values stored in a Map can be of any type. This includes arrays of numeric values,
structures, cells, character arrays, objects, or other Maps.
Note A Map is most memory efficient when the data stored in it is a scalar number or a
character array.
See Also
containers.Map | keys | values
Related Examples
• “Description of Map Class” on page 14-4
• “Create Map Object” on page 14-6
• “Examine Contents of Map” on page 14-9
14-3
14 Map Containers
To examine one of these properties, follow the name of the Map object with a dot and
then the property name. For example, to see what type of keys are used in Map mapObj,
use
mapObj.KeyType
A Map is a handle object. As such, if you make a copy of the object, MATLAB does not
create a new Map; it creates a new handle for the existing Map that you specify. If you
alter the Map's contents in reference to this new handle, MATLAB applies the changes
you make to the original Map as well. You can, however, delete the new handle without
affecting the original Map.
14-4
See Also
Method Description
isKey Check if Map contains specified key
keys Names of all keys in Map
length Length of Map
remove Remove key and its value from Map
size Dimensions of Map
values Values contained in Map
See Also
containers.Map | isKey | keys | length | remove | size | values
Related Examples
• “Overview of Map Data Structure” on page 14-2
• “Create Map Object” on page 14-6
• “Examine Contents of Map” on page 14-9
14-5
14 Map Containers
newMap = containers.Map(optional_keys_and_values)
newMap = containers.Map
newMap =
Count: 0
KeyType: char
ValueType: any
The properties of an empty Map object are set to their default values:
• Count = 0
• KeyType = 'char'
• ValueType = 'any'
Once you construct the empty Map object, you can use the keys and values methods to
populate it. For a summary of MATLAB functions you can use with a Map object, see
“Methods of Map Class” on page 14-5
14-6
Create Map Object
For those keys and values that are character vectors, be sure that you specify them
enclosed within single quotation marks. For example, when constructing a Map that has
character vectors as keys, use
mapObj = containers.Map(...
{'keystr1', 'keystr2', ...}, {val1, val2, ...});
As an example of constructing an initialized Map object, create a new Map for the
following key/value pairs taken from the monthly rainfall map shown earlier in this
section.
KEYS VALUES
Jan 327.2
Feb 368.2
Mar 197.6
Apr 178.4
May 100.0
Jun 69.9
Jul 32.3
Aug 37.3
Sep 19.0
Oct 37.0
Nov 73.2
Dec 110.9
Annual 1551.0
rainfallMap = containers.Map(k, v)
rainfallMap =
14-7
14 Map Containers
Count: 13
KeyType: char
ValueType: double
The Count property is now set to the number of key/value pairs in the Map, 13, the
KeyType is char, and the ValueType is double.
See Also
containers.Map | keys | values
Related Examples
• “Overview of Map Data Structure” on page 14-2
• “Description of Map Class” on page 14-4
• “Examine Contents of Map” on page 14-9
14-8
Examine Contents of Map
Create a new Map called ticketMap that maps airline ticket numbers to the holders of
those tickets. Construct the Map with four key/value pairs:
ticketMap = containers.Map(...
{'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham', ...
'Bradley Reid'});
Use the keys method to display all keys in the Map. MATLAB lists keys of type char in
alphabetical order, and keys of any numeric type in numerical order:
keys(ticketMap)
ans =
Next, display the values that are associated with those keys in the Map. The order of the
values is determined by the order of the keys associated with them.
keys values
2R175 James Enright
A479GY Sarah Latham
B7398 Carl Haynes
NZ1452 Bradley Reid
values(ticketMap)
ans =
14-9
14 Map Containers
See Also
containers.Map | isKey | keys | length | remove | size | values
Related Examples
• “Create Map Object” on page 14-6
• “Read and Write Using Key Index” on page 14-11
• “Modify Keys and Values in Map” on page 14-16
• “Map to Different Value Types” on page 14-19
14-10
Read and Write Using Key Index
Note For a large Map, the keys and value methods use a lot of memory as their outputs
are cell arrays.
You can find any single value by indexing into the Map with the appropriate key:
passenger = ticketMap('2R175')
passenger =
James Enright
ans =
14-11
14 Map Containers
To access the values of multiple keys, use the values method, specifying the keys in a
cell array:
values(ticketMap, {'2R175', 'B7398'})
ans =
Map containers support scalar indexing only. You cannot use the colon operator to access
a range of keys as you can with other MATLAB classes. For example, the following
statements throw an error:
ticketMap('2R175':'B7398')
ticketMap(:)
Add two more entries to the ticketMap Map. Verify that ticketMap now has six key/
value pairs:
ticketMap('947F4') = 'Susan Spera';
ticketMap('417R93') = 'Patricia Hughes';
ticketMap.Count
ans =
14-12
Read and Write Using Key Index
ans =
ans =
'James Enright' 'Patricia Hughes' 'Susan Spera' 'Sarah Latham' 'Carl Haynes' 'Bradley Reid'
• Only vertical vectors of Map objects are allowed. You cannot create an m-by-n array or
a horizontal vector of Map objects. For this reason, vertcat is supported for Map
objects, but not horzcat.
• All keys in each Map being concatenated must be of the same class.
• You can combine Maps with different numbers of key/value pairs. The result is a single
Map object containing key/value pairs from each of the contributing Map objects:
tMap1 = containers.Map({'2R175', 'B7398', 'A479GY'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham'});
The result of this concatenation is the same 6-element Map that was constructed in
the previous section:
ticketMap.Count
ans =
keys(ticketMap), values(ticketMap)
14-13
14 Map Containers
ans =
ans =
'James Enright' 'Patricia Hughes' 'Susan Spera' 'Sarah Latham' 'Carl Haynes' 'Bradley Reid'
• Concatenation does not include duplicate keys or their values in the resulting Map
object.
In the following example, both objects m1 and m2 use a key of 8. In Map m1, 8 is a key
to value C; in m2, it is a key to value X:
m = [m1; m2];
The resulting Map object m has only five key/value pairs. The value C was dropped
from the concatenation because its key was not unique:
keys(m), values(m)
ans =
ans =
See Also
containers.Map | isKey | keys | values
Related Examples
• “Create Map Object” on page 14-6
• “Examine Contents of Map” on page 14-9
14-14
See Also
14-15
14 Map Containers
Note Keep in mind that if you have more than one handle to a Map, modifying the handle
also makes changes to the original Map. See “Modify Copy of Map” on page 14-17,
below.
remove(mapName, 'keyname');
ticketMap = containers.Map(...
{'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham', ...
'Bradley Reid'});
Remove one entry (the specified key and its value) from the Map object:
remove(ticketMap, 'NZ1452');
values(ticketMap)
ans =
Modify Values
You can modify any value in a Map simply by overwriting the current value. The
passenger holding ticket A479GY is identified as Sarah Latham:
ticketMap('A479GY')
ans =
14-16
Modify Keys and Values in Map
Sarah Latham
Change the passenger's first name to Anna Latham by overwriting the original value for
the A479GY key:
ticketMap('A479GY')
ans =
Anna Latham
Modify Keys
To modify an existing key while keeping the value the same, first remove both the key and
its value from the Map. Then create a new entry, this time with the corrected key name.
remove(ticketMap, '2R175');
ticketMap('2S185') = 'James Enright';
k = keys(ticketMap); v = values(ticketMap);
str1 = ' ''%s'' has been assigned a new\n';
str2 = ' ticket number: %s.\n';
fprintf(str1, v{1})
fprintf(str2, k{1})
14-17
14 Map Containers
Make a copy of the ticketMap Map. Write to this copy, and notice that the change is
applied to the original Map object itself:
copiedMap = ticketMap;
ans =
unidentified person
Clean up:
remove(ticketMap, 'AZ12345');
clear copiedMap;
See Also
containers.Map | isKey | keys | length | remove | size | values
Related Examples
• “Create Map Object” on page 14-6
• “Examine Contents of Map” on page 14-9
• “Read and Write Using Key Index” on page 14-11
• “Map to Different Value Types” on page 14-19
14-18
Map to Different Value Types
ticketMap = containers.Map(...
{'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham', ...
'Bradley Reid'});
Then create the following structure array, containing ticket numbers and destinations:
Using this Map object, find information about the passenger who has reserved seat 09C:
seatingMap('09C')
ans =
ticketNum: 'B7398'
14-19
14 Map Containers
destination: 'Granada'
reserved: '30-Apr-2008'
origin: 'JFK'
Using ticketMap and seatingMap together, you can find the name of the person who
has reserved seat 15B:
ticket = seatingMap('15B').ticketNum;
passenger = ticketMap(ticket)
passenger =
Sarah Latham
See Also
cell | containers.Map | isKey | keys | struct | values
14-20
See Also
Related Examples
• “Create Map Object” on page 14-6
• “Create Structure Array” on page 11-2
• “Create Cell Array” on page 12-3
• “Examine Contents of Map” on page 14-9
• “Read and Write Using Key Index” on page 14-11
• “Modify Keys and Values in Map” on page 14-16
14-21
15
Data type conversion is done with respect to a preset precedence of classes. The
following table shows the five classes you can concatenate with an unlike type without
generating an error (that is, with the exception of character and logical).
For example, concatenating a double and single matrix always yields a matrix of type
single. MATLAB converts the double element to single to accomplish this.
See Also
More About
• “Combining Unlike Integer Types” on page 15-3
• “Combining Integer and Noninteger Data” on page 15-5
• “Combining Cell Arrays with Non-Cell Arrays” on page 15-6
• “Concatenation Examples” on page 15-8
• “Concatenating Objects of Different Classes”
15-2
Combining Unlike Integer Types
Overview
If you combine different integer types in a matrix (e.g., signed with unsigned, or 8-bit
integers with 16-bit integers), MATLAB returns a matrix in which all elements are of one
common type. MATLAB sets all elements of the resulting matrix to the data type of the
left-most element in the input matrix. For example, the result of the following
concatenation is a vector of three 16-bit signed integers:
A = [int16(450) uint8(250) int32(1000000)]
B = [int8(50) int16(5000)]
B =
50 127
The first operation returns a vector of 16-bit integers. The second returns a vector of 8-bit
integers. The element int16(5000) is set to 127, the maximum value for an 8-bit signed
integer.
15-3
15 Combining Unlike Classes
50
127
Note You can find the maximum or minimum values for any MATLAB integer type using
the intmax and intmin functions. For floating-point types, use realmax and realmin.
A = [int8(-100) uint8(100)]
A =
-100 100
B = [uint8(100) int8(-100)]
B =
100 0
MATLAB evaluates each element prior to concatenating them into a combined array. In
other words, the following statement evaluates to an 8-bit signed integer (equal to 50)
and an 8-bit unsigned integer (unsigned -50 is set to zero) before the two elements are
combined. Following the concatenation, the second element retains its zero value but
takes on the unsigned int8 type:
A = [int8(50), uint8(-50)]
A =
50 0
15-4
Combining Integer and Noninteger Data
15-5
15 Combining Unlike Classes
fprintf('Classes: %s %s %s %s\n',...
class(A{1}),class(A{2}),class(A{3}),class(A{4}))
15-6
Empty Matrices
Empty Matrices
If you construct a matrix using empty matrix elements, the empty matrices are ignored in
the resulting matrix:
15-7
15 Combining Unlike Classes
Concatenation Examples
In this section...
“Combining Single and Double Types” on page 15-8
“Combining Integer and Double Types” on page 15-8
“Combining Character and Double Types” on page 15-9
“Combining Logical and Double Types” on page 15-9
15-8
Concatenation Examples
class(x)
ans =
char
class(x)
ans =
double
15-9
16
Using Objects
16 Using Objects
Object Behavior
In this section...
“Two Copy Behaviors” on page 16-2
“Handle Object Copy” on page 16-2
“Value Object Copy Behavior” on page 16-2
“Handle Object Copy Behavior” on page 16-3
“Testing for Handle or Value Class” on page 16-6
Value objects behave like MATLAB fundamental types with respect to copy operations.
Copies are independent values. Operations that you perform on one object do not affect
copies of that object.
Handle objects are referenced by their handle variable. Copies of the handle variable
refer to the same object. Operations that you perform on a handle object are visible from
all handle variables that reference that object.
a = 8;
b = a;
16-2
Object Behavior
a = 6;
b
b =
8
b =
8
The copy behavior of values stored as properties in value objects is the same as numeric
variables. For example, suppose vobj1 is a value object with property a:
vobj1.a = 8;
If you copy vobj1 to vobj2, and then change the value of vobj1 property a, the value of
the copied object's property, vobj2.a, is unaffected:
vobj2 =vobj1;
vobj1.a = 5;
vobj2.a
ans =
8
16-3
16 Using Objects
end
end
Because this statement is not terminated with a semicolon, MATLAB displays information
about the object:
hobj1 =
Data: 8
The variable hobj1 is a handle that references the object created. Copying hobj1 to
hobj2 results in another handle referring to the same object:
hobj2 = hobj1
hobj2 =
Data: 8
Because handles reference the object, copying a handle copies the handle to a new
variable name, but the handle still refers to the same object. For example, given that
hobj1 is a handle object with property Data:
hobj1.Data
ans =
Change the value of hobj1's Data property and the value of the copied object's Data
property also changes:
hobj1.Data = 5;
hobj2.Data
ans =
16-4
Object Behavior
Because hobj2 and hobj1 are handles to the same object, changing the copy, hobj2,
also changes the data you access through handle hobj1:
hobj2.Data = 17;
hobj1.Data
ans =
17
Reassigning a handle variable produces the same result as reassigning any MATLAB
variable. When you create an object and assign it to hobj1:
hobj1 = HdClass(3.14);
hobj1 references the new object, not the same object referenced previously (and still
referenced by hobj2).
When you clear a handle from the workspace, MATLAB removes the variable, but does
not remove the object referenced by the other handle. However, if there are no references
to an object, MATLAB destroys the object.
Given hobj1 and hobj2, which both reference the same object, you can clear either
handle without affecting the object:
hobj1.Data = 2^8;
clear hobj1
hobj2
hobj2 =
Data: 256
If you clear both hobj1 and hobj2, then there are no references to the object. MATLAB
destroys the object and frees the memory used by that object.
16-5
16 Using Objects
To remove an object referenced by any number of handles, use delete. Given hobj1 and
hobj2, which both refer to the same object, delete either handle. MATLAB deletes the
object:
hobj1 = HdClass(8);
hobj2 = hobj1;
delete(hobj1)
hobj2
hobj2 =
Modifying Objects
When you pass an object to a function, MATLAB passes a copy of the object into the
function workspace. If the function modifies the object, MATLAB modifies only the copy of
the object that is in the function workspace. The differences in copy behavior between
handle and value classes are important in such cases:
• Value object — The function must return the modified copy of the object. To modify the
object in the caller’s workspace, assign the function output to a variable of the same
name
• Handle object — The copy in the function workspace refers to the same object.
Therefore, the function does not have to return the modified copy.
isa(obj,'handle')
16-6
See Also
ans =
isa(hobj,'containers.Map')
ans =
class(hobj)
ans =
containers.Map
See Also
Related Examples
• “Implement Copy for Handle Classes”
16-7
17
All MATLAB data types are implemented as object-oriented classes. You can add data
types of your own to your MATLAB environment by creating additional classes. These
user-defined classes define the structure of your new data type, and the functions, or
methods, that you write for each class define the behavior for that data type.
These methods can also define the way various MATLAB operators, including arithmetic
operations, subscript referencing, and concatenation, apply to the new data types. For
example, a class called polynomial might redefine the addition operator (+) so that it
correctly performs the operation of addition on polynomials.
3
18
Scripts
Create Scripts
Scripts are the simplest kind of program file because they have no input or output
arguments. They are useful for automating series of MATLAB commands, such as
computations that you have to perform repeatedly from the command line or series of
commands you have to reference.
• Highlight commands from the Command History, right-click, and select Create Script.
•
Click the New Script button on the Home tab.
• Use the edit function. For example, edit new_file_name creates (if the file does
not exist) and opens the file new_file_name. If new_file_name is unspecified,
MATLAB opens a new file called Untitled.
After you create a script, you can add code to the script and save it. For example, you can
save this code that generates random numbers from 0 through 100 as a script called
numGenerator.m.
columns = 10000;
rows = 1;
bins = columns/100;
rng(now);
list = 100*rand(rows,columns);
histogram(list,bins)
Save your script and run the code using either of these methods:
• Type the script name on the command line and press Enter. For example, to run the
numGenerator.m script, type numGenerator.
•
Click the Run button on the Editor tab
You also can run the code from a second program file. To do this, add a line of code with
the script name to the second program file. For example, to run the numGenerator.m
script from a second program file, add the line numGenerator; to the file. MATLAB runs
the code in numGenerator.m when you run the second file.
When execution of the script completes, the variables remain in the MATLAB workspace.
In the numGenerator.m example, the variables columns, rows, bins, and list remain
18-2
See Also
in the workspace. To see a list of variables, type whos at the command prompt. Scripts
share the base workspace with your interactive MATLAB session and with other scripts.
See Also
More About
• “Code Sections” on page 18-7
• “Scripts vs. Functions” on page 18-16
• “Base and Function Workspaces” on page 20-10
• “Create Live Scripts in the Live Editor” on page 19-7
18-3
18 Scripts
In the Live Editor, you can insert lines of text before and after code to describe a process
or code. Text lines provide additional flexibility such as standard formatting options, and
the insertion of images, hyperlinks, and equations. For more information, see “Create Live
Scripts in the Live Editor” on page 19-7.
Note When you have a MATLAB code file (.m) containing text that has characters in a
different encoding than that of your platform, when you save or publish your file, MATLAB
displays those characters as garbled text. Live scripts and functions (.mlx) support
storing and displaying characters across all locales.
To add comments to MATLAB code, use the percent (%) symbol. Comment lines can
appear anywhere in a program file, and you can append comments to the end of a line of
code.
For example:
To comment out multiple lines of code, use the block comment operators, %{ and %}. The
%{ and %} operators must appear alone on the lines that immediately precede and follow
the block of help text. Do not include any other text on these lines.
For example:
a = magic(3);
%{
sum(a)
diag(a)
sum(diag(a))
%}
sum(diag(fliplr(a)))
18-4
Add Comments to Programs
To comment out a selection, select the lines of code, go to the Editor or Live Editor tab,
and press the button. You also can type Ctrl+R. To uncomment the selected lines
code, press the button or type Ctrl+T.
To comment out part of a statement that spans multiple lines, use an ellipsis (...) instead
of a percent sign. For example:
The MATLAB Editor includes tools and context menu items to help you add, remove, or
change the format of comments for MATLAB, Java, and C/C++ code. For example,
suppose that you have this lengthy text into a commented line.
% This is a program that has a comment that is a little more than 75 columns wide.
disp('Hello, world')
With the cursor on the line, go to Editor tab and in the Edit section, press the button.
The Editor wraps the comment:
% This is a program that has a comment that is a little more than 75
% columns wide.
disp('Hello, world')
By default, as you type comments in the Editor, the text wraps when it reaches a column
width of 75. To change the column where the comment text wraps or to disable automatic
comment wrapping, go to the Home tab and in the Environment section, click
Preferences. Select MATLAB > Editor/Debugger > Language, and adjust the
Comment formatting preferences.
18-5
18 Scripts
See Also
Related Examples
• “Add Help for Your Program” on page 20-6
• “Create Scripts” on page 18-2
• “Create Live Scripts in the Live Editor” on page 19-7
• “Editor/Debugger Preferences”
18-6
Code Sections
Code Sections
In this section...
“Divide Your File into Code Sections” on page 18-7
“Evaluate Code Sections” on page 18-7
“Navigate Among Code Sections in a File” on page 18-9
“Example of Evaluating Code Sections” on page 18-9
“Change the Appearance of Code Sections” on page 18-12
“Use Code Sections with Control Statements and Functions” on page 18-13
To define code section boundaries explicitly, insert section breaks using these methods:
•
On the Editor tab, in the Edit section, in the Insert button group, click .
• Enter two percent signs (%%) at the start of the line where you want to begin the new
code section.
The text on the same line as %% is called the section title. Including section titles is
optional, however, it improves the readability of the file and appears as a heading if you
publish your code.
18-7
18 Scripts
The section evaluation features run the section code currently highlighted in yellow.
MATLAB does not automatically save your file when evaluating individual code sections.
The file does not have to be on your search path.
Operation Instructions
Run the code in the • Place the cursor in the code section.
current section. •
On the Editor tab, in the Run section, click Run
Section.
Run the code in the • Place the cursor in the code section.
current section, and then •
move to the next section. On the Editor tab, in the Run section, click Run and
Advance.
Run all the code in the file. • Type the saved script name in the Command Window.
•
On the Editor tab, in the Run section, click Run.
You can increment numbers within a section, rerunning that section after every change.
This helps you fine-tune and experiment with your code.
4
Input appropriate values in the / text box or / text box.
5
Click the , , , or button to add to, subtract from, multiply, or divide the
selected number in your section.
18-8
Code Sections
Note MATLAB software does not automatically save changes you make to the numbers in
your script.
Operation Instructions
Move to the next section. •
On the Editor tab, in the Run section, click Advance.
Move to the previous • Press Ctrl + Up arrow.
section.
Move to a specific section. •
On the Editor tab, in the Navigate section, use the
Go To to move the cursor to a selected section.
edit(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','sine_wave.m'))
1 Insert a section break and the following title on the first line of the file.
18-9
18 Scripts
18-10
Code Sections
3
Type 2 in the / text box.
4
Click the button several times.
18-11
18 Scripts
1
On the Home tab, in the Environment section, click Preferences.
You can choose whether to highlight the sections, the color of the highlighting, and
whether dividing lines appear between code sections.
18-12
Code Sections
• MATLAB inserts a section break at the top and bottom of a file, creating a code section
that encompasses the entire file. However, the Editor does not highlight the resulting
section, which encloses the entire file, unless you add one or more explicit code
sections to the file.
• If you define a section break within a control flow statement (such as an if or while
statement), MATLAB automatically inserts section breaks at the lines containing the
start and end of the statement.
• If you define a section break within a function, MATLAB inserts section breaks at the
function declaration and at the function end statement. If you do not end the function
with an end statement, MATLAB behaves as if the end of the function occurs
immediately before the start of the next function.
If an automatic break occurs on the same line as a break you insert, they collapse into one
section break.
for k = 3:2:9
%%
y = y + sin(k*t)/k;
if ~mod(k,3)
%%
display(sprintf('When k = %.1f',k));
plot(t,y)
end
end
18-13
18 Scripts
If you copy and paste this code into a MATLAB Editor, you see that the two section breaks
create three nested levels:
• At the outermost level of nesting, one section spans the entire file.
MATLAB only defines section in a code block if you specify section breaks at the same
level within the code block. Therefore, MATLAB considers the cursor to be within the
section that encompasses the entire file.
• At the second level of nesting, a section exists within the for loop.
18-14
See Also
See Also
More About
• “Create Scripts” on page 18-2
• “Create Live Scripts in the Live Editor” on page 19-7
• “Scripts vs. Functions” on page 18-16
18-15
18 Scripts
Both scripts and functions allow you to reuse sequences of commands by storing them in
program files. Scripts are the simplest type of program, since they store commands
exactly as you would type them at the command line. However, functions are more flexible
and more easily extensible.
Create a script in a file named triarea.m that computes the area of a triangle:
b = 5;
h = 3;
a = 0.5*(b.*h)
After you save the file, you can call the script from the command line:
triarea
a =
7.5000
To calculate the area of another triangle using the same script, you could update the
values of b and h in the script and rerun it. Each time you run it, the script stores the
result in a variable named a that is in the base workspace.
However, instead of manually updating the script each time, you can make your program
more flexible by converting it to a function. Replace the statements that assign values to b
and h with a function declaration statement. The declaration includes the function
keyword, the names of input and output arguments, and the name of the function.
function a = triarea(b,h)
a = 0.5*(b.*h);
end
After you save the file, you can call the function with different base and height values
from the command line without modifying the script:
a1 = triarea(1,5)
a2 = triarea(2,10)
a3 = triarea(3,6)
18-16
See Also
a1 =
2.5000
a2 =
10
a3 =
9
Functions have their own workspace, separate from the base workspace. Therefore, none
of the calls to the function triarea overwrite the value of a in the base workspace.
Instead, the function assigns the results to variables a1, a2, and a3.
See Also
More About
• “Create Scripts” on page 18-2
• “Create Functions in Files” on page 20-2
• “Add Functions to Scripts” on page 18-18
• “Base and Function Workspaces” on page 20-10
18-17
18 Scripts
To add local functions to a script, first, create the script. Go to the Home tab and select
New > Script. For more information about creating scripts, see “Create Scripts” on page
18-2. You also can “Create Live Scripts in the Live Editor” on page 19-7.
After you create the script, add code to the script and save it. For example, add this code
and save it as a script called mystats.m. This code declares an array, determines the
length of the array, and passes both values to the local functions mymean and mymedian.
The local functions mymean and mymedian calculate the average and median of the input
list and return the results.
x = 1:10;
n = length(x);
avg = mymean(x,n);
med = mymedian(x,n);
function a = mymean(v,n)
% MYMEAN Example of a local function.
a = sum(v)/n;
end
18-18
Add Functions to Scripts
function m = mymedian(v,n)
% MYMEDIAN Another example of a local function.
w = sort(v);
if rem(n,2) == 1
m = w((n + 1)/2);
else
m = (w(n/2) + w(n/2 + 1))/2;
end
end
You can add local functions in any order, as long as they all appear after the rest of the
script code. Each function begins with its own function definition statement, and ends
with the end keyword. The definition statement is the first executable line of any function,
for example, function a = mymean(v,n). For more information about function
definition statements, including how to create them, see “Create Functions in Files” on
page 20-2.
Access Help
Although you cannot call a local function from the command line or from functions in
other files, you can access its help using the help command. Specify the names of both
the script and the local function, separating them with a > character:
help mystats>mymean
Run Code
To run a script, including all local functions, click the Run (for scripts) or Run All
(for live scripts) button, or type the saved script name in the Command Window. You can
also run individual sections in a script by clicking the Run Section button.
Local functions in the current file have precedence over functions in other files. That is,
when you call a function within a program file, MATLAB checks whether the function is a
local function before looking for other functions. This allows you to create an alternate
version of a particular function while retaining the original in another file.
Scripts create and access variables in the base workspace. Local functions, similar to
other functions, have their own workspaces that are separate from the base workspace.
18-19
18 Scripts
Local functions cannot access variables in the workspace of other functions or in the base
workspace, unless you pass them as arguments. For more information, see “Base and
Function Workspaces” on page 20-10.
See Also
More About
• “Create Functions in Files” on page 20-2
• “Function Precedence Order” on page 20-43
18-20
19
• Add titles, headings, and formatted text to describe a process and include equations,
images, and hyperlinks as supporting material.
19-2
What Is a Live Script or Function?
• Save your narratives as richly formatted, executable documents and share them with
colleagues or the MATLAB community, or convert them to HTML, PDF, Microsoft
Word, or LaTeX documents for publication.
• Combine code and results with formatted text and mathematical equations.
• Create step-by-step lectures and evaluate them incrementally to illustrate a topic.
• Modify code on the fly to answer questions or explore related topics.
• Share lectures with students as interactive documents or in hard copy format, and
distribute partially completed files as assignments.
19-3
19 Live Scripts and Functions
19-4
What Is a Live Script or Function?
Requirements
• MATLAB R2016a — MATLAB supports live scripts in versions R2016a and above, and
live functions in versions R2018a and above.
• Operating System — MATLAB supports the Live Editor in most of the operating
systems supported by MATLAB. For more information, see System Requirements.
There are several operating systems supported by MATLAB that the Live Editor does
not support.
Some operating systems require additional configuration to run the Live Editor. If you
are unable to run the Live Editor on your system, Contact Technical Support for
information on how to configure your system.
19-5
19 Live Scripts and Functions
Unsupported Features
When deciding whether to create a live script or function, it is important to note several
unsupported features in the Live Editor:
• Classes — Classes are not supported in the Live Editor. Create classes as plain code
files (.m) instead. You then can use the classes in your live scripts or functions.
• Editor preferences — The Live Editor ignores most Editor preferences, including
custom keyboard shortcuts and Emacs-style keyboard shortcuts.
1 On the Live Editor tab, in the File section, select Save > Save As....
2 In the dialog box that appears, select MATLAB Code files (*.m) as the Save as
type.
3 Click Save.
See Also
Related Examples
• “Create Live Scripts in the Live Editor” on page 19-7
• “Live Code File Format (.mlx)” on page 19-80
• MATLAB Live Script Gallery
19-6
Create Live Scripts in the Live Editor
. You also can use the edit function in the Command Window. For example, type edit
penny.mlx to open or create the file penny.mlx. To ensure that a live script is created,
specify a .mlx extension. If an extension is not specified, MATLAB defaults to a file
with .m extension, which only supports plain code.
If you have an existing script, you can open it as a live script in the Live Editor. Opening a
script as a live script creates a copy of the file and leaves the original file untouched.
MATLAB converts publishing markup from the original script to formatted content in the
new live script.
To open an existing script (.m) as a live script (.mlx) from the Editor, right-click the
document tab, and select Open scriptName as Live Script from the context menu.
19-7
19 Live Scripts and Functions
Alternatively, go to the Editor tab, click Save , and select Save As. Then, set the Save as
type: to MATLAB Live Code Files (*.mlx) and click Save.
Note You must use one of the described conversion methods to convert your script to a
live script. Simply renaming the script with a .mlx extension does not work and can
corrupt the file.
Add Code
After you create a live script, you can add code and run it. For example, add this code that
plots a vector of random data and draws a horizontal line on the plot at the mean.
n = 50;
r = rand(n,1);
plot(r)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
19-8
Create Live Scripts in the Live Editor
MATLAB also autocompletes comments, character vectors, strings, and parentheses when
split across two lines. To escape out of an autocompletion, press Ctrl+Z or the Undo
button. Autocompletions are enabled by default. To disable them, see “Editor/Debugger
Autocoding Preferences”.
When adding or editing code, you can select and edit a rectangular area of code (also
known as column selection or block edit). This is useful if you want to copy or delete
several columns of data (as opposed to rows), or if you want to edit multiple lines at one
time. To select a rectangular area, press the Alt key while making a selection.
Run Code
To run the code, click the vertical striped bar to the left of the code. Alternatively, go to
the Live Editor tab and click Run. While your program is running, a status indicator
appears at the top left of the Editor window. A gray blinking bar to the left of a line of
code indicates the line that MATLAB is evaluating. To navigate to the line MATLAB is
evaluating, click the status indicator.
19-9
19 Live Scripts and Functions
You do not need to save your live script to run it. When you do save your live script,
MATLAB automatically saves it with a .mlx extension. For example, go the Live Editor
tab, click Save, and enter the name plotRand. MATLAB saves the live script as
plotRand.mlx.
Display Output
By default, MATLAB displays output to the right of the code. Each output is displayed
with the line that creates it, like in the Command Window.
You can change the size of the output display panel by dragging left or right on the
resizer bar between the code and output.
To clear an output, right-click the output or the code line that created it, and select Clear
Output. To clear all output, right-click anywhere in the script and select Clear All
Output. Alternatively, go to the View tab and in the Output section, click the Clear all
Output button.
When scrolling, MATLAB aligns the output to the code that generates it. To disable the
alignment of output to code, right-click the output section and select Disable
Synchronous Scrolling.
19-10
Create Live Scripts in the Live Editor
To move the output in line with the code, click the output inline button to the right of
the live script. You also can go to the View tab and in the View section, click the
Output Inline button.
To only display output, controls, and formatted text and hide the code, click the hide code
button. To show the code again, click the output inline button or the output on
right button.
To modify figures in the output, use the tools in the upper-right corner of the figure axes
or in the Figure toolstrip. You can use the tools to explore the data in a figure and add
formatting and annotations. For more information, see “Modify Figures in Live Scripts” on
page 19-27.
To open individual outputs, such as variables and figures, in a separate window, click the
button in the upper right corner of the output. Variables open in the Variables editor,
19-11
19 Live Scripts and Functions
and figures open in a new figure window. Changes made to variables or figures outside of
a live script do not apply to the output displayed in the live script.
Format Text
You can add formatted text, hyperlinks, images, and equations to your live scripts to
create a presentable document to share with others. For example, add a title and some
introductory text to plotRand.mlx:
1
Place your cursor at the top of the live script and, in the Live Editor tab, select
Text. A new text line appears above the code.
2
Click and select Title.
3 Add the text Plot Random Data.
4
With your cursor still in the line, click the button to center the text.
5 Press Enter to move to the next line.
6 Type the text This script plots a vector of random data and draws a
horizontal line on the plot at the mean.
For more information including a list of all available formatting options, see “Format Files
in the Live Editor” on page 19-39.
19-12
See Also
To adjust the displayed font size in the Live Editor, use the Ctrl + Mouse Scroll
keyboard shortcut. The change in the displayed font size is not honored when exporting
the live script to PDF, Microsoft Word, HTML, or LaTeX.
See Also
More About
• “Format Files in the Live Editor” on page 19-39
• “Run Sections in Live Scripts” on page 19-14
• “Modify Figures in Live Scripts” on page 19-27
• MATLAB Live Script Gallery
19-13
19 Live Scripts and Functions
To insert a section break into your live script, go to the Live Editor tab and in the
Section section, click the Section Break button. The new section is highlighted in blue,
indicating that it is selected. A vertical striped bar to the left of the section indicates that
the section is stale. A stale section is a section that has not yet been run, or that has been
modified since it was last run.
To delete a section break, click the beginning of the line directly after the section break
and press Backspace. You also can click the end of the line directly before the section
break and press Delete.
Evaluate Sections
Run your live script either by evaluating each section individually or by running all the
code at once. To evaluate a section individually, it must contain all the values it requires,
or the values must exist in the MATLAB workspace. Section evaluation runs the currently
selected section, highlighted in blue. If there is only one section in your program file, the
section is not highlighted, as it is always selected.
19-14
See Also
Operation Instructions
Run the code in the Click the blue bar to the left of the section.
selected section.
OR
See Also
Related Examples
• “Modify Figures in Live Scripts” on page 19-27
• “Debug Code in the Live Editor” on page 19-16
19-15
19 Live Scripts and Functions
Show Output
A simple way to determine where a problem occurs in your live script or function is to
show output. To show the output for a line, remove the semi-colon from the end of that
line. The Live Editor displays each output with the line of code that creates it, making it
easy to determine where a problem occurs.
For example, suppose that you have a script called plotRand.mlx that plots a vector of
random data and draws a horizontal line on the plot at the mean.
To display the output of the rand function at line 2, remove the semi-colon at the end of
the line. To display line numbers in the Live Editor if they are not visible, go to the View
tab and press the Line Numbers button. MATLAB displays the value of r.
19-16
Debug Code in the Live Editor
To run to a specified line of code and then pause, click the run to here button to the
left of the line. If the selected line cannot be reached, MATLAB continues running until
the end of the file is reached or a breakpoint is encountered. The run to here button is
only available in live functions when debugging.
For example, click the button to the left of line 2 in plotRand.mlx. MATLAB runs
plotRand.mlx starting at line 1 and pauses before running line 2.
19-17
19 Live Scripts and Functions
When MATLAB pauses, the Run button in the Live Editor tab changes to a
Continue button. The Live Editor indicates the line at which MATLAB is paused by
highlighting the line in green. The highlighted line does not run until after MATLAB
resumes running.
Tip It is a good practice to avoid modifying a file while MATLAB is paused. Changes that
are made while MATLAB is paused do not run until after MATLAB finished running the
code and the code is rerun.
19-18
Debug Code in the Live Editor
To continue running the code, click the Continue button. MATLAB continues running
the file until it reaches the end of the file or a breakpoint. You also can click the button
to the left of the line of code you want to continue running to.
To continue running the code line-by-line, on the Live Editor tab, click Step.
MATLAB executes the current line at which it is paused and the pauses at the next line.
You also can run up to the line with the cursor by going to the Live Editor tab, selecting
19-19
19 Live Scripts and Functions
You also can view the value of a variable by typing the variable name in the Command
Window. For example, to see the value of the variable n, type n and press Enter. The
Command Window displays the variable name and its value. To view all the variables in
the current workspace, use the Workspace browser.
To pause a program while it is running, go to the Live Editor tab and click the Pause
button. MATLAB pauses at the next executable line, and the Pause button changes to
a Continue button. To continue running, press the Continue button.
Pausing is useful if you want to check on the progress of a long running program to
ensure that it is running as expected.
Note Clicking the pause button can cause MATLAB to pause in a file outside your own
program file. Pressing the Continue button resumes running without changing the
results of the file.
19-20
Debug Code in the Live Editor
and clicking Stop. To avoid confusion, make sure to end your debugging session
every time you are done debugging. The Live Editor automatically ends the debugging
session when you save.
examine values. To step into a file, click the button directly to the left of the function
you want to step into. You also can use the F11 key to step into a function. The Live
Editor only displays the button if the line contains a call to another function.
By default, the button only shows for user-defined functions and scripts. To show the
button for MathWorks functions as well, on the Home tab, in the Environment section,
click Preferences. Then, select MATLAB > Editor/Debugger, and in the Debugging in
the Live Editor section, clear Only show Step in button for user-defined functions.
After stepping in, click the button at the top of the file to run the rest of the called
function, leave the called function, and then pause. You also can use Shift+F11 to step
out of a function.
You also can step in and out of functions by going to the Live Editor tab, selecting
Step and then selecting Step In or Step Out. These buttons do not honor the
Only show Step in button for user-defined functions preference and always step in
and out of both user-defined and MathWorks functions.
When you step into a called function or file, the Live Editor displays the list of the
functions MATLAB executed before pausing at the current line. The list is shown at the
top of the file and displays the functions in order, starting on the left with the first called
script or function, and ending on the right with the current script or function in which
MATLAB is paused. This list is called the function call stack.
19-21
19 Live Scripts and Functions
For each function in the function call stack, there is a corresponding workspace.
Workspaces contain variables that you create within MATLAB or import from data files or
other programs. Variables that you assign through the Command Window or create using
scripts belong to the base workspace. Variables that you create in a function belong to
their own function workspace.
To examine a variable during debugging, you must first select its workspace. The selected
function in the function call stack indicates the current workspace. To select or change
the workspace, click the function in the function call stack. MATLAB opens the function in
the Live Editor and changes the current workspace to the workspace of the function.
Once the workspace is selected, you can view the values of the variables in it using the
Workspace browser or as a data tip in the Live Editor.
19-22
Debug Code in the Live Editor
When you run the file, MATLAB pauses at the line of code indicated by the breakpoint.
Clear Breakpoints
To clear a breakpoint, right-click the breakpoint and select Clear Breakpoint from the
context menu. You also can use the F12 key to clear the breakpoint.
To clear all breakpoints in the file, select Clear All in File. To clear all breakpoints in all
files, select Clear All.
Disable Breakpoints
You can disable selected breakpoints so that your program temporarily ignores them and
runs uninterrupted. For example, you might disable a breakpoint after you identify and
correct a problem.
To disable a breakpoint, right-click it and select Disable Breakpoint from the context
menu. The breakpoint becomes gray to indicate that it is disabled.
19-23
19 Live Scripts and Functions
You can add a condition to a breakpoint that tells MATLAB when to pause at the specified
line. To add a condition, right-click the breakpoint and select Set/Modify Condition.
When the Editor dialog box opens, enter a condition and click OK. A condition is any valid
MATLAB expression that returns a logical scalar value. MATLAB evaluates the condition
before running the line.
For example, suppose that you only want to pause in plotRand.mlx if the random
generated data contains a 0.
any(r == 0)
19-24
Debug Code in the Live Editor
When you run the file, MATLAB pauses at the specified line when the condition is met. For
example, in the plotRand example, MATLAB pauses before running line 3 if any of the
values in r are equal to 0.
You can add multiple breakpoints in a line of MATLAB code that contains anonymous
functions. You can set a breakpoint for the line itself and for each anonymous function in
the line.
To add a breakpoint, click the gray area to the left on an executable line to add a
breakpoint for the line. MATLAB adds a breakpoint for the line, and a disabled breakpoint
for each anonymous function in the line. To enable a breakpoint for an anonymous
function, right-click it and select Enable Breakpoint.
To view information about all the breakpoints on a line, hover your pointer on the
breakpoint icon. A tooltip appears with available information. For example, in this code,
line 5 contains two anonymous functions, with a breakpoint at each one.
19-25
19 Live Scripts and Functions
When you set a breakpoint in an anonymous function, MATLAB pauses when the
anonymous function is called. The line highlighted in green is where the code defines the
anonymous function. The line highlighted in gray is where the code calls the anonymous
functions. For example, in this code, MATLAB pauses the program at a breakpoint set for
the anonymous function g, defined at line 5, and called at line 6.
See Also
More About
• “Run Sections in Live Scripts” on page 19-14
19-26
Modify Figures in Live Scripts
Explore Data
You can pan, zoom, and rotate a figure in your script using the tools that appear in the
upper-right corner of the figure axes when you hover over the figure.
To undo or redo an action, click or at the upper right corner of the toolstrip.
Note
•
When you open a saved live script, appears next to each output figure, indicating
that the interactive tools are not available yet. To make these tools available, run the
live script.
• The interactive tools are not available for invisible axes.
Suppose that you want to explore the health information for 100 different patients. Create
a live script called patients.mlx and add code that loads the data and adds a scatter
plot that shows the height versus weight of two groups of patients, female and male. Run
the code by going to the Live Editor tab and clicking Run.
load patients
figure
19-27
19 Live Scripts and Functions
Gender = categorical(Gender);
scatter(Height(Gender=='Female'),Weight(Gender=='Female'));
hold on
scatter(Height(Gender=='Male'),Weight(Gender=='Male'));
hold off
Explore the points where the patient height is 64 inches. Select the button and click
one of the data points where height is 64. MATLAB zooms into the figure.
19-28
Modify Figures in Live Scripts
For example, in the live script patients.mlx, after zooming in on patients with a height
of 64, click the Update Code button. MATLAB adds the generated code after the line
containing the code for creating the plot.
19-29
19 Live Scripts and Functions
xlim([61.31 69.31])
ylim([116.7 183.3])
If MATLAB is unable to determine where to place the generated code, the Update Code
button is disabled. This occurs, for example, if you modify the code without running the
script again. In this case, use the Copy button to copy the generated code into the
clipboard. You then can paste the code into your script at the appropriate location.
•
Title — Add a title to the axes. To modify an existing title, click the existing title
and enter the modified text.
•
X-Label, Y-Label — Add a label to the axes. To modify an existing label,
click the existing label and enter the modified text.
•
Legend — Add a legend to the figure. To modify the existing legend
descriptions, click the existing descriptions and enter the modified text. Select
Remove Legend from the Annotations section to remove the legend from the axes.
•
Colorbar — Add a color bar legend to the figure. Select Remove Colorbar
from the Annotations section to remove the color bar legend from the axes.
•
Grid, X-Grid, Y-Grid — Add grid lines to the figure. Select
Remove Grid from the Annotations section to remove all the grid lines from the
axes.
19-30
Modify Figures in Live Scripts
•
Line, Arrow, Text Arrow, Double Arrow — Add a line or
arrow annotation to the figure. Draw the arrow from tail to head. To move an existing
annotation, click the annotation to select it and drag it to the desired location. Press
the Delete key to delete the selected annotation.
Note Adding formatting and annotations using the Figure tab is not supported for
invisible axes.
For example, suppose that you want to add formatting and annotations to the figure in
patients.mlx.
1
Add a title — In the Annotations section, select Title. A blue rectangle
appears prompting you to enter text. Type the text Weight vs. Height and press
Enter.
2
Add X and Y Labels — In the Annotations section, select X-Label. A blue
rectangle appears prompting you to enter text. Type the text Height and press
Enter. Select Y-Label. A blue rectangle appears prompting you to enter text.
Type the text Weight and press Enter.
3
Add a legend — In the Annotations section, select Legend. A legend
appears at the top right corner of the axes. Click the data1 description in the legend
and replace the text with Female. Click the data2 description in the legend and
replace the text with Male. Press Enter.
4
Add grid lines — In the Annotations section, select Grid. Grid lines appear
in the axes.
5
Add an arrow annotation — In the Annotations section, select Text Arrow.
Drawing the arrow from tail to head, position the arrow on the scatter plot pointing
to the lightest patient. Enter the text Lightest Patient and press Enter
6 Update the code — In the selected figure, click the Update Code button. The live
script now contains the code needed to reproduce the figure changes.
19-31
19 Live Scripts and Functions
grid on
legend({'Female','Male'})
title('Weight vs Height')
xlabel('Height')
ylabel('Weight')
annotation('textarrow',[0.455 0.3979],[0.3393 0.13],'String','Lightest Patient');
19-32
Modify Figures in Live Scripts
the Subplot button. You only can add additional subplots to a figure if the figure
contains one subplot. If a figure contains multiple subplots, the Subplot button is
disabled.
For example, suppose that you want to compare the blood pressure of smoking and non-
smoking patients. Create a live script called patients_smoking.mlx and add code that
loads the health information for 100 different patients.
load patients
Run the code by going to the Live Editor tab and clicking Run.
Add a scatter plot that shows the systolic blood pressure of patients that smoke versus
the systolic blood pressure of patients that do not smoke. Run the code.
figure
scatter(Age(Smoker==1),Systolic(Smoker==1));
hold on
scatter(Age(Smoker==0),Systolic(Smoker==0));
hold off
In the Figure tab, select Subplot and choose the layout for two horizontal graphs.
19-33
19 Live Scripts and Functions
In the newly created figure, click the Update Code button. The live script now contains
the code needed to reproduce the two subplots.
subplot(2,1,1,gca)
subplot(2,1,2)
Add a scatter plot that shows the diastolic blood pressure of patients that smoke versus
the diastolic blood pressure of patients that do not smoke. Run the code.
scatter(Age(Smoker==1),Diastolic(Smoker==1));
hold on
scatter(Age(Smoker==0),Diastolic(Smoker==0));
hold off
Add formatting:
19-34
Modify Figures in Live Scripts
1
Add titles to each subplot — In the Annotations section, select Title. A
blue rectangle appears in each subplot prompting you to enter text. Type the text
Systolic Blood Pressure of Smokers vs Non-Smokers in the first subplot
and Diastolic Blood Pressure of Smokers vs Non-Smokers in the second
subplot and press Enter.
2
Add grid lines to each subplot — In the Annotations section, select Grid.
An Add Grid button appears on each subplot. Click the Add Grid button on each
subplot. Grid lines appear in both subplots.
19-35
19 Live Scripts and Functions
3 Update the code — In the selected figure, click the Update Code button. The live
script now contains the code needed to reproduce the figure changes.
subplot(2,1,1)
grid on
title('Systolic Blood Pressure of Smokers vs Non-Smokers')
subplot(2,1,2)
grid on
title('Diastolic Blood Pressure of Smokers vs Non-Smokers')
19-36
Modify Figures in Live Scripts
1
Click the button in the upper-right corner of the output. This opens the figure in a
separate figure window.
2 a To save the figure — Select File > Save As. For more information on saving
figures, see “Save Plot as Image or Vector Graphics File” or “Save Figure to
Reopen in MATLAB Later”.
b To print the figure — Select File > Print. For more information on printing
figures, see “Print Figure from File Menu”.
19-37
19 Live Scripts and Functions
Note Any changes made to the figure in the separate figure window are not reflected in
the live script. Similarly, any changes made to the figure in the live script are not
reflected in the open figure window.
See Also
Related Examples
• “Format Files in the Live Editor” on page 19-39
• “Run Sections in Live Scripts” on page 19-14
19-38
Format Files in the Live Editor
To insert a new item, go to the Insert tab and select from the available options:
19-39
19 Live Scripts and Functions
To format existing text, use any of the options included in the Live Editor tab Text
section:
Heading 1
Heading 2
Heading 3
Title
19-40
Format Files in the Live Editor
Right
Lists Numbered list
Bulleted list
Standard
Bold
Formatting
Italic
Underline
Monospace
To change the case of selected text or code from all uppercase to lowercase, or vice versa,
select the text, right-click, and select Change Case. You also can press Ctrl+Shift+A. If
the text contains both uppercase and lowercase text, MATLAB changes the case to all
uppercase.
To adjust the displayed font size in the Live Editor, use the Ctrl + Mouse Scroll
keyboard shortcut. The change in the displayed font size is not honored when exporting
the live script to PDF, Microsoft Word, HTML, or LaTeX.
Autoformatting
For quick formatting in live scripts and functions, you can use a combination of keyboard
shortcuts and character sequences. Formatting appears after you enter the final
character in a sequence.
This table shows a list of formatting styles and their available keyboard shortcuts and
autoformatting sequences.
19-41
19 Live Scripts and Functions
--- + Enter
*** + Enter
Bulleted list * text Ctrl + Alt + U
- text
+ text
Numbered list number. Ctrl + Alt + O
text
Italic *text* Ctrl + I
_text_
Bold **text** Ctrl + B
__text__
Bold and ***text*** Ctrl + B, then Ctrl + I
italic
___text___
19-42
Format Files in the Live Editor
|text|
Underline None Ctrl + U
LaTeX $LaTeX$ Ctrl + Shift + L
equation
Hyperlink URL + Space Ctrl + K
or Enter
<URL>
[Label]
(URL)
Trademark, (TM) None
service mark,
and copyright (SM)
symbols (™,
℠, ®, and ©) (R)
(C)
Note Title, heading, section break, and list sequences must be entered at the beginning
of a line.
Backspace key or by clicking Undo . For example, if you type ## text + Enter, a
heading in the Heading 1 style with the word text appears. To undo the formatting style
and simply display ## text, press the Backspace key. You only can escape out of a
sequence directly after completing it. After you enter another character or move the
cursor, escaping is no longer possible.
To revert the autoformatting for LaTeX equations and hyperlinks, use the Backspace key
at any point.
19-43
19 Live Scripts and Functions
To force formatting to reappear after escaping out of a sequence, click the Redo
button. You only can redo an action directly after escaping it. After you enter another
character or move the cursor, the redo action is no longer possible. In this case, to force
the formatting to reappear, delete the last character in the sequence and type it again.
To disable all or certain autoformatting sequences, you can adjust the “Editor/Debugger
Autoformatting Preferences”.
See Also
More About
• “Insert Equations into the Live Editor” on page 19-45
• “Share Live Scripts and Functions” on page 19-77
19-44
Insert Equations into the Live Editor
There are two ways to insert an equation into a live script or function.
19-45
19 Live Scripts and Functions
2 Build your equation by selecting symbols, structures, and matrices from the options
displayed in the Equation tab. View additional options by clicking the to the right
of each section.
When adding or editing a matrix, a context menu appears, which you can use to
delete and insert rows and columns. You also can use the context menu to change or
remove matrix delimiters.
3 Format your equation using the options available in the Text section. Formatting is
only available for text within the equation. Numbers and symbols cannot be
formatted. The formatting option is disabled unless the cursor is placed within text
that can be formatted.
The equation editor provides a few shortcuts for adding elements to your equation:
• To insert symbols, structures, and matrices, type a backslash followed by the name of
the symbol. For example, type \pi to insert a π symbol into the equation. To discover
the name of a symbol or structure, hover over the corresponding button in the
Equation tab. You can also type backslash in the equation editor to bring up a
completion menu of all supported names.
19-46
Insert Equations into the Live Editor
Note Although the \name syntax closely resembles LaTeX command syntax, entering
full LaTeX expressions is not supported when inserting equations interactively.
• To insert subscripts, superscripts, and fractions, use the symbols ‘_’, ‘^’ or ‘/’. For
example:
19-47
19 Live Scripts and Functions
The preview pane shows a preview of equation as it would appear in the live script.
3 To include a description of the LaTeX equation when exporting the live script to
HTML, add text to the Alt Text field. For example, you can enter the text Maclaurin
series for sin(x).
19-48
Insert Equations into the Live Editor
The description specifies alternative text for the equation and is saved as an alt
attribute in the HTML document. It is used to provide additional information for the
equation if, for example, a user is using a screen reader.
4 Press OK to insert the equation into your live script.
LaTeX expressions describe a wide range of equations. This table shows several examples
of LaTeX expressions and their appearance when inserted into a live script.
\int_{0}^{2} x^2\sin(x) dx
∫ x sin(x)dx
2
2
0
\sin(x) = \sum_{n=0}^{\infty}{\frac{(-1)^n ∞ n
( − 1) x2n + 1
x^{2n+1}}{(2n+1)!}} sin(x) = ∑ (2n + 1)!
n=0
MATLAB supports most standard LaTeX math mode commands. These tables show a list
of supported LaTeX commands.
Greek/Hebrew Letters
19-49
19 Live Scripts and Functions
Δ Delta Φ Phi Θ
Γ Gamma Π Pi ϒ
Λ Lambda Ψ Psi Ξ
Ω Omega Σ Sigma
Operator Symbols
19-50
Insert Equations into the Live Editor
Relation Symbols
Note The leq, geq, equiv, approx, cong, sim, simeq, models, ni, succ, succeq,
prec, preceq, parallel, subset, supset, subseteq, and supseteq commands can
be combined with the not command to create the negated version of the symbol. For
example, \not\leq creates the symbol ≰.
Arrows
19-51
19 Live Scripts and Functions
↦ mapsto longmapsto
Brackets
Bigg, Biggl,
Biggr, Biggm
19-52
Insert Equations into the Live Editor
Misc Symbols
ℜ Re j ′
ℓ ell imath ¬
⋯ cdots : colon ←
⋱ ddots ⋅ cdotp →
⋮ vdots . ldotp
Note The exists command can be combined with the not command to create the
negated version of the symbol. For example, \not\exists creates the symbol ∄.
Accents
Functions
19-53
19 Live Scripts and Functions
Math Constructs
∫
a
c d
left right ab
cd
19-54
See Also
Note To create a matrix using the matrix and pmatrix commands, use the & symbol to
separate columns, and \cr to separate rows. For example, to create a 2–by–2 matrix, use
the expression \matrix{a & b \cr c & d}.
White Space
Text Styling
See Also
Related Examples
• “Share Live Scripts and Functions” on page 19-77
External Websites
• https://www.latex-project.org/
19-55
19 Live Scripts and Functions
Insert Controls
To insert a control into a live script, go to the Live Editor tab, and in the Code section,
click Control. Then, select from the available options. To replace an existing value with a
control, select the value and then insert the control. The Control menu only shows
options available for the selected value.
To configure an existing control, right-click the control and select Configure Control.
Press Tab or Enter, or click outside of the control configuration menu to return to the
live script.
19-56
Add Interactive Controls to a Live Script
Hover over the text displayed in In the Item Values field, specify
the drop-down list to see its the values for each item in the
current value. drop-down line. Make sure to
enclose text values in quotes or
double quotes, because the Live
Editor interprets each item in the
list as code.
Check Box Use check boxes to interactively N/A
set the value of a variable to either
the logical value 1 (true) or the
logical value 0 (false).
19-57
19 Live Scripts and Functions
Labels
To specify the label that you want to display next to the control when the code is hidden,
in the Label section, enter a label name. This is also the text that displays on button
controls in all views.
To hide the code and only display labeled controls, output, and formatted text, click the
hide code button to the right of the live script. You also can go to the View tab, and in
the View section, click Hide Code. To show the code again, click the output inline
button or the output on right button.
Execution
By default, when the value of a control changes, the Live Editor runs the code in the
current section. To configure this behavior, in the Execution section of the control
configuration menu, specify these fields:
• Run On (slider control only) — Select Value changing to run the code while the
value of the slider is changing. Select Value changed to wait until the slider value is
done changing (user has released the slider thumb).
• Run — Select from the available options to determine what code runs when the value
of the control changes. For example, Current section to only run the section that
includes the control when the control value changes. Select Current section to end
to run the section that includes the control and any sections that follow.
19-58
Add Interactive Controls to a Live Script
Tip When using a button control in a live script, consider setting the Run field for all
other controls in the live script to None. Then, the code only runs when the user clicks
the button control. This can be useful when the live script requires multiple control
values to be set before running the code.
Use the interactive controls to specify the gender of the patients to plot, as well as the
threshold height and weight. To view and interact with the controls, open this example in
your browser or in MATLAB.
load patients
sp1 = scatter(Height(Gender==selectedGender),Weight(Gender==selectedGender),'blue');
hold on
19-59
19 Live Scripts and Functions
If you share the live script itself as an interactive document, consider hiding the code in
the live script before sharing it. When the code is hidden, the Live Editor only displays
labeled controls, output, and formatted text. To hide the code, click the hide code
button to the right of the live script. You also can go to the View tab, and in the View
section, click Hide Code.
19-60
See Also
If you share the live script as a static PDF, Microsoft Word, HTML, or LaTeX document,
the Live Editor saves the control as code. For example, in the live script shown here, the
Live Editor replaces the slider controls with their current value (68 and 132) and replaces
the drop-down control with the current value of the drop-down ("Female").
See Also
More About
• “Add Interactive Tasks to a Live Script” on page 19-62
• “Share Live Scripts and Functions” on page 19-77
19-61
19 Live Scripts and Functions
Tasks represent a series of MATLAB commands. You can display their output either inline
or on the right. To see the MATLAB commands that the task runs, show the generated
code.
Insert Tasks
To add a task to a live script, go to the Live Editor tab, click Task , and select from
the available tasks. You also can type the name of the task in a live script code block. As
you type, the Live Editor displays possible matches, and you can select and insert the
desired task. For example, create a live script that creates a vector of data containing an
outlier.
19-62
Add Interactive Tasks to a Live Script
Add the Clean Outlier Data task to your live script to smooth the noisy data and avoid
skewed results. To add the task, start typing the word clean in the live script and select
Clean Outlier Data from the suggested command completions.. In the task, set Input
data to A. The task identifies and fills two outliers in the data and creates the variable
cleanedData in the MATLAB workspace with the stored results. You also can see the
results in the output plot for the task. Continue modifying additional parameters until you
are satisfied with the results.
19-63
19 Live Scripts and Functions
19-64
Add Interactive Tasks to a Live Script
To restore all parameter values back to their defaults, click the options button at the
top-right of the task and select Restore Default Values.
When you are done modifying parameters, you can collapse the task to help with
readability. To collapse the task, click the arrow at the top-left of the task.
Delete Tasks
To delete a task, click the task and then press Delete or Backspace. You also can place
your cursor directly before or after the task and use the Delete or Backspace key,
respectively.
19-65
19 Live Scripts and Functions
cleanmydata.mlx, the entire section including the code that creates the vector of noisy
data reruns every time you modify the value of a parameter in the Clean Outlier Data
task. To run only the task, add section breaks before and after the task. For more
information about sections and how to add section breaks, see “Run Sections in Live
Scripts” on page 19-14.
A green circular icon at the top-right of the task indicates that the task runs automatically
when you modify the task parameters.
To disable running the section automatically, click the autorun icon. The icon updates to
display the disabled state. To run the task and current section, on the Live Editor tab,
Some tasks do not run automatically by default. This default setting ensures optimal
performance for those tasks.
19-66
Add Interactive Tasks to a Live Script
You can use the resulting output argument in subsequent code, including as inputs to
additional Live Editor tasks.
To see the MATLAB commands that the task runs, click the options button at the top-
right of the task and select either Controls and Code or Code Only. You also can use
the down arrow at the bottom of the task to show and hide the generated code. The
generated code is read-only.
To edit the generated code, click the options button and select Convert Task to
Editable Code. This option removes the task and replaces it with the generated code,
which you then can edit.
19-67
19 Live Scripts and Functions
See Also
More About
• “Add Interactive Controls to a Live Script” on page 19-56
• “Clean Messy Data and Locate Extrema Using Live Editor Tasks”
• MATLAB Live Script Gallery
19-68
Create Live Functions
If you have an existing function, you can open it as a live function in the Live Editor.
Opening a function as a live function creates a copy of the file and leaves the original file
untouched. MATLAB converts publishing markup from the original script to formatted
content in the new live function.
To open an existing function (.m) as a live function (.mlx) from the Editor, right-click the
document tab and select Open functionName as Live Function from the context menu.
Alternatively, go to the Editor tab, click Save , and select Save As. Then, set the Save as
type: to MATLAB Live Code Files (*.mlx) and click Save.
Note You must use one of the described conversion methods to convert your function to a
live function. Simply renaming the function with a .mlx extension does not work and can
corrupt the file.
If you have an existing large live script or function, you can break it into smaller pieces by
automatically converting selected areas of code into functions or local functions. This is
called code refactoring.
To refactor a selected area of code, select one or more lines of code and on the Live
Editor tab, in the Code section, click Refactor. Then, select from the available
options. MATLAB creates a function with the selected code and replaces the original code
with a call to the newly created function.
19-69
19 Live Scripts and Functions
Add Code
After you create the live function, add code to the function and save it. For example, add
this code and save it as a function called mymean.mlx. The mymean function calculates
the average of the input list and returns the results.
function a = mymean(v,n)
a = sum(v)/n;
end
Add Help
To document the function, add formatted help text above the function definition. For
example, add a title and some text to describe the functionality. For more information
about adding help text to functions, see “Add Help for Live Functions” on page 19-73.
To run a live function from the Command Window, enter the name of the function in the
Command Window. For example, use mymean.mlx to calculate the mean of 10 sequential
numbers from 1 to 10.
mymean(1:10, 10)
ans =
5.5000
19-70
Create Live Functions
You also can call the live function from a live script. For example, create a live script
called mystats.mlx. Add this code that declares an array, determines the length of the
array, and passes both values to the function mymean.
x = 1:10;
n = length(x);
avg = mymean(x,n);
disp(['Average = ', num2str(avg)])
Run the live script. The Live Editor displays the output.
If a live function displays text or returns values, the Live Editor displays the output in the
calling live script, in line with the call to the live function. For example, add a line to
mymean that displays the calculated mean before returning the value:
function a = mymean(v,n)
a = sum(v)/n;
disp(['a = ', num2str(a)])
end
When you run mystats, the Live Editor displays the output for mymean with the output
from mystats.
19-71
19 Live Scripts and Functions
See Also
More About
• “Add Help for Live Functions” on page 19-73
19-72
Add Help for Live Functions
Create help text by inserting text at the beginning of the file, immediately before the
function definition line (the line with the function keyword).
For example, create a live function called addme.mlx with this code:
function c = addme(a,b)
switch nargin
case 2
c = a + b;
case 1
c = a + a;
otherwise
c = 0;
end
19-73
19 Live Scripts and Functions
When you type help addme in the Command Window, the help text displays.
The first line of help text, often called the H1 line, typically contains a brief description of
the function. When displaying help for a function, MATLAB first displays the name of the
function followed by the H1 line. Then, MATLAB displays the syntax of the function.
Finally, MATLAB displays any remaining help text.
To add "See also" links, add a text line at the end of the help text that begins with the
words See also followed by a list of function names. If the functions exist on the search
path or in the current folder, the help command displays each of these function names as
a hyperlink to its help. Otherwise, help prints the function names as they appear in the
help text.
Note When multiple programs have the same name, the help command determines
which help text to display by applying the rules described in “Function Precedence
Order” on page 20-43. However, if a program has the same name as a built-in function,
the Help on Selection option in context menus always displays documentation for the
built-in function.
To enhance the documentation displayed in the Help browser further, you can format the
text and add hyperlinks, images, equations, and example code. For example, in the addme
function, select the H1 line and in the Live Editor tab, change the Normal text style to
Title. Then, position your cursor at the end of the second syntax description, go to the
Insert tab, and select Equation. Enter the equation c = a + b and press Esc.
Finally, in the Insert tab, select Code Example > MATLAB and add two examples. For
more information about formatting files in the Live Editor, see “Format Files in the Live
Editor” on page 19-39.
19-74
Add Help for Live Functions
Use the doc command to display the help text in a separate browser.
19-75
19 Live Scripts and Functions
You also can add help to live functions by inserting comments at the beginning of the file.
Comments at the beginning of the file display as help text when you use the help and
doc commands, similar to how text at the beginning of the file displays. For more
information about adding help using comments, see “Add Help for Your Program” on page
20-6.
See Also
More About
• “Create Live Functions” on page 19-69
• “Format Files in the Live Editor” on page 19-39
• “Share Live Scripts and Functions” on page 19-77
19-76
Share Live Scripts and Functions
This table shows the different ways to share live scripts and functions.
19-77
19 Live Scripts and Functions
To hide the code, click the hide code button to the right of the live script. You also can
go to the View tab, and in the View section, click Hide Code. To show the code again,
19-78
See Also
See Also
Related Examples
• “Create Live Scripts in the Live Editor” on page 19-7
• “Format Files in the Live Editor” on page 19-39
• MATLAB Live Script Gallery
19-79
19 Live Scripts and Functions
Source Control
To determine and display code differences between live scripts or functions, use the
MATLAB Comparison Tool.
If you use source control, register the .mlx extension as binary. For more information,
see “Register Binary Files with SVN” on page 33-18 or “Register Binary Files with Git”
on page 33-32.
19-80
See Also
See Also
More About
• “What Is a Live Script or Function?” on page 19-2
• “Create Live Scripts in the Live Editor” on page 19-7
External Websites
• Open Packaging Conventions Fundamentals
• Office Open XML File Formats (ECMA-376)
19-81
19 Live Scripts and Functions
Create a live script in the Live Editor. To create a live script, on the Home tab, click New
Live Script.
Divide your live script into sections. Sections can contain text, code, and output. MATLAB
code appears with a gray background and output appears with a white background. To
create a new section, go to the Live Editor tab and click the Section Break button.
pop = 1×11
Sections can be run independently. To run the code in a section, go to the Live Editor tab
and click the Run Section button. You can also click the blue bar that appears when you
move the mouse to the left of the section. When you run a section, output and figures
appear together with the code that produced them.
19-82
Introduction to the Live Editor
Add supporting information to the text, including equations, images, and hyperlinks.
Let's try fitting the data with polynomials. We'll use the MATLAB polyfit function to get
the coefficients.
19-83
19 Live Scripts and Functions
y = ax + b linear
y= ax2 + bx + c quadratic
y = ax3 + bx2 + cx + d . cubic
x = (years-1900)/50;
coef1 = polyfit(x,pop,1)
coef1 = 1×2
98.9924 66.1296
coef2 = polyfit(x,pop,2)
coef2 = 1×3
coef3 = polyfit(x,pop,3)
coef3 = 1×4
We can plot the linear, quadratic, and cubic curves fitted to the data. We'll use the
polyval function to evaluate the fitted polynomials at the points in x.
pred1 = polyval(coef1,x);
pred2 = polyval(coef2,x);
pred3 = polyval(coef3,x);
[pred1; pred2; pred3]
ans = 3×11
19-84
Introduction to the Live Editor
hold on
plot(years,pred1)
plot(years,pred2)
plot(years,pred3)
ylim([50 300])
legend({'Data' 'Linear' 'Quadratic' 'Cubic'},'Location', 'NorthWest')
hold off
You can share your live script with other MATLAB users so that they can reproduce your
results. You also can publish your results as PDF, Microsoft® Word, or HTML documents.
19-85
19 Live Scripts and Functions
Add controls to your live scripts to show users how important parameters affect the
analysis. To add controls, go to the Live Editor tab, click the Controls button, and select
from the available options.
We can now calculate the predicted population of a given year using our three equations.
year = ;
xyear = (year-1900)/50;
pred1 = polyval(coef1,xyear);
pred2 = polyval(coef2,xyear);
pred3 = polyval(coef3,xyear);
[pred1 pred2 pred3]
ans = 1×3
For the year 2010 for example, the linear and cubic fits predict similar values of about
284 million people, while the quadratic fit predicts a much higher value of about 300
million people.
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
• MATLAB Live Script Gallery
19-86
Accelerate Exploratory Programming Using the Live Editor
The Live Editor displays output together with the code that produced it. To run a section,
go to the Live Editor tab and select the Run Section button. You can also click the blue
bar that appears when you move your mouse to the left edge of a section.
In this example, we explore some highway fatality data. Start by loading the data. The
variables are shown as the column headers of the table.
load fatalities
fatalities(1:10,:)
ans=10×8 table
longitude latitude deaths drivers vehicles v
_________ ________ ______ _______ ________ _
19-87
19 Live Scripts and Functions
The Live Editor allows you to divide your program into sections containing text, code, and
output. To create a new section, go to the Live Editor tab and click the Section Break
button. The code in a section can be run independently, which makes it easy to explore
ideas as you write your program.
Calculate the fatality rate per one million vehicle miles. From these values we can find the
states with the lowest and highest fatality rates.
states = fatalities.Properties.RowNames;
rate = fatalities.deaths./fatalities.vehicleMiles;
[~, minIdx] = min(rate); % Minimum accident rate
[~, maxIdx] = max(rate); % Maximum accident rate
disp([states{minIdx} ' has the lowest fatality rate at ' num2str(rate(minIdx))])
Distribution of Fatalities
You can include visualizations in your program. Like output, plots and figures appear
together with the code that produced them.
We can use a bar chart to see the distribution of fatality rates among the states. There are
11 states that have a fatality rate greater than 0.02 per million vehicle miles.
histogram(rate,10)
xlabel('Fatalities per Million Vehicle Miles')
ylabel('Number of States')
19-88
Accelerate Exploratory Programming Using the Live Editor
You can explore your data quickly in the Live Editor by experimenting with parameter
values to see how your results will change. Add controls to change parameter values
interactively. To add controls, go to the Live Editor tab, click the Controls button, and
select from the available options.
We can experiment with the data to see if any of the variables in the table are correlated
with highway fatalities. For example, it appears that highway fatality rates are lower in
states with a higher percentage urban population.
dataToPlot = ;
close % Close any open figures
scatter(fatalities.(dataToPlot),rate) % Plot fatalities vs. selected variable
19-89
19 Live Scripts and Functions
xlabel(dataToPlot)
ylabel('Percent Fatalities per Million Vehicle Miles')
hold on
xmin = min(fatalities.(dataToPlot));
xmax = max(fatalities.(dataToPlot));
p = polyfit(fatalities.(dataToPlot),rate,1); % Calculate & plot least squares line
plot([xmin xmax], polyval(p,[xmin xmax]))
Summarize your results and share your live script with your colleagues. Using your live
script, they can recreate and extend your analysis. You can also save your analysis as
HTML, Microsoft® Word, or PDF documents for publication.
19-90
Accelerate Exploratory Programming Using the Live Editor
Based on this analysis, we can summarize our findings using a plot of fatality rates and
urban population on a map of the continental United States.
load usastates.mat
figure
geoplot([usastates.Lat], [usastates.Lon], 'black')
geobasemap darkwater
hold on
geoscatter(fatalities.latitude,fatalities.longitude,2000*rate,fatalities.urbanPopulatio
c = colorbar;
title(c,'Percent Urban')
19-91
19 Live Scripts and Functions
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
• MATLAB Live Script Gallery
19-92
Create an Interactive Narrative with the Live Editor
Overall Approach
Include formatted text as part of the interactive narrative. Use bold, italic, and underlined
text to highlight important words. Use bullets or numbers to format lists.
Estimate the power output from a typical solar panel installation on a specific date, time,
and location by calculating the following:
• Solar time
• Solar declination and solar elevation
• Air mass and the solar radiation reaching the earth's surface
• Radiation on a solar panel given its position, tilt, and efficiency
• Power generated in a day and over the entire year
Use the results of these calculations to plot solar and panel radiation for the example day
and location. Then, plot the expected panel power generation over the course of a year. To
streamline the analysis, use two MATLAB functions created for this example:
solarCorrection and panelRadiation.
Solar Time
Show output together with the code that produced it. To run a section of code, go to the
Live Editor tab and click the Run Section button.
19-93
19 Live Scripts and Functions
Power generation in a solar panel depends on how much solar radiation reaches the
panel. This in turn depends on the sun's position relative to the panel as the sun moves
across the sky. For example, suppose that you want to calculate power output for a solar
panel on June 1st at 12 noon in Boston, Massachusetts.
lambda = ; % longitude
phi = ; % latitude
UTCoff = ; % UTC offset
january1 = datetime(2019,1,1); % January 1st
localTime = datetime(2019,6,1,12,0,0) % Noon on June 1
localTime = datetime
01-Jun-2019 12:00:00
To calculate the sun's position for a given date and time, use solar time. Twelve noon solar
time is the time when the sun is highest in the sky. To calculate solar time, apply a
correction to local time. That correction has two parts:
• A term which corrects for the difference between the observer's location and the local
meridian.
• An orbital term related to the earth's orbital eccentricity and axial tilt.
solarTime = datetime
01-Jun-2019 12:18:15
Include equations to describe the underlying mathematics. Create equations using LaTeX
commands. To add a new equation, go to the Live Editor tab and click the Equation
button. Double-click an equation to edit it in the Equation Editor.
The solar declination (δ) is the angle of the sun relative to the earth's equatorial plane.
∘
The solar declination is 0 at the vernal and autumnal equinoxes, and rises to a maximum
∘
of 23 . 45 at the summer solstice. Calculate the solar declination for a given day of the
year (d) using the equation
19-94
Create an Interactive Narrative with the Live Editor
−1 360
δ = sin sin(23 . 45)sin (d − 81)
365
Then, use the declination (δ), the latitude (ϕ), and the hour angle (ω) to calculate the
sun's elevation (α) at the current time. The hour angle is the number of degrees of
rotation of the earth between the current solar time and solar noon.
−1
α = sin sinδsinϕ + cosδcosϕcosω
Calculate the time of sunrise and sunset in Standard Time using the sun's declination and
the local latitude.
midnight = dateshift(localTime,'start','day');
sr = 12 - acosd(-tand(phi)*tand(delta))/15 - solarCorr/60;
sunrise = timeofday(midnight + hours(sr));
ss = 12 + acosd(-tand(phi)*tand(delta))/15 - solarCorr/60;
sunset = timeofday(midnight + hours(ss));
disp(['Sunrise = ', datestr(sunrise,'HH:MM:SS'), ' Sunset = ', datestr(sunset,'HH:MM:
Include images to illustrate important points in your story. To include an image, copy and
paste an image from another source or go to the Live Editor tab and click the Image
button.
As light from the sun passes through the earth's atmosphere, some of the solar radiation
is absorbed. Air mass is the length of the path of light through the atmosphere (Y) relative
19-95
19 Live Scripts and Functions
to the shortest possible path (X) when the sun's elevation is 90∘, as shown in the diagram
below. It is a function of solar elevation (α).
The larger the air mass, the less radiation reaches the ground. Calculate the air mass
using the equation
1
AM = −1 . 6364
cos(90 − α) + 0 . 5057(6 . 0799 + α) .
Then, calculate the solar radiation reaching the ground (in kilowatts per square meter)
using the empirical equation
0 . 678
sRad = 1 . 353 * 0 . 7 AM .
AM = 1/(cosd(90-alpha) + 0.50572*(6.07955+alpha)^-1.6354);
solarRad = 1.353*0.7^(AM^0.678); % kW/m^2
disp(['Air Mass = ' num2str(AM) ' Solar Radiation = ' num2str(solarRad) ' kW/m^2'])
19-96
Create an Interactive Narrative with the Live Editor
Panels installed with a solar tracker can move with the sun and receive 100% of the sun's
radiation as the sun moves across the sky. However, most solar cell installations have
panels set at a fixed azimuth and tilt. Therefore, the actual radiation reaching the panel
also depends on the solar azimuth. The solar azimuth (γ) is the compass direction of the
sun's position in the sky. At solar noon in the Northern hemisphere the solar azimuth is
∘
180 corresponding to the direction south. Calculate the solar azimuth using the equation
sinδcosϕ − cosδsinϕcosω
cos−1 for solar time ≤ 12
cosα
γ=
∘ sinδcosϕ − cosδsinϕcosω
360 − cos−1 for solar time > 12
cosα
In the northern hemisphere, a typical solar panel installation has panels oriented toward
∘
the south with a panel azimuth (β) of 180 . At northern latitudes, a typical tilt angle (τ) is
∘
35 . Calculate the panel radiation for fixed panels from the total solar radiation using the
equation
Modify parameters using interactive controls. Display plots together with the code that
produced them.
19-97
19 Live Scripts and Functions
Panel Radiation
For a given day of the year, calculate the total solar radiation and the radiation on the
panel. To simplify the analysis, use the panelRadiation function. Try different dates to
see how the solar and panel radiation change depending on the time of year.
selectedMonth = ;
selectedDay = ;
selectedDate = datetime(2019,selectedMonth,selectedDay);
[times,solarRad,panelRad] = panelRadiation(selectedDate,lambda,phi,UTCoff,tau,beta) ;
plot(times,solarRad,times,panelRad)
title(['Solar and Panel Radiation for ' datestr(selectedDate,'mmmm dd yyyy')])
xlabel('Hour of Day');
ylabel('Radiation, kW/m^2')
legend('Available Solar Radiation','Solar Radiation on Panel', 'Location','South')
19-98
Create an Interactive Narrative with the Live Editor
Power Generation
So far, the calculations assume that all of the radiation reaching the panel is available to
generate power. However, solar panels do not convert 100% of available solar radiation
into electricity. The efficiency of a solar panel is the fraction of the available radiation that
is converted. The efficiency of a solar panel depends on the design and materials of the
cell.
19-99
19 Live Scripts and Functions
Hover over a plot to interact with it. Interacting with a plot in the Live Editor will
generate code that you can then add to your script.
Repeat the calculation to estimate power generation for each day of the year.
plot(yearDates,dailyPower)
title('Yearly Power Generation')
xlabel('Date');
ylabel('Power Generation, kW-hrs')
19-100
Create an Interactive Narrative with the Live Editor
yearlyPower = sum(dailyPower);
disp(['Expected annual power output = ' num2str(yearlyPower) ' kW-hrs'])
Use a heatmap to determine how panel tilt affects power generation. The heatmap below
shows that the optimal panel tilt for any location is about 5∘ less than the latitude.
load LatitudeVsTilt.mat
heatmap(powerTbl,'Tilt','Latitude',...
'ColorVariable','Power');
xlabel('Panel Tilt')
19-101
19 Live Scripts and Functions
ylabel('Latitude')
title('Normalized Power Output')
Share your analysis with colleagues. Invite them to reproduce or extend your analysis.
Work collaboratively using the Live Editor.
In reality, true power output from a solar installation is significantly affected by local
weather conditions. An interesting extension of this analysis would be to see how cloud
cover affects the results. In the US, you can use data from these government websites.
• Use historical local weather data from the National Weather Service website.
19-102
See Also
• Use measured solar radiation data from the National Solar Radiation Database.
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
• “Format Files in the Live Editor” on page 19-39
• MATLAB Live Script Gallery
19-103
19 Live Scripts and Functions
Add equations to explain the underlying mathematics for concepts that you want to teach.
To add an equation, go to the Live Editor tab and click the Equation button. Then, select
from the symbols and structures in the Equation tab.
Today we're going to talk about finding the roots of 1. What does it mean to find the nth
root of 1? The nth roots of 1 are the solutions to the equation xn − 1 = 0.
For square roots, this is easy. The values are x = ± 1 = ± 1. For higher-order roots, it
gets a bit more difficult. To find the cube roots of 1 we need to solve the equation
x3 − 1 = 0. We can factor this equation to get
x − 1 x2 + x + 1 = 0 .
So the first cube root is 1. Now we can use the quadratic formula to get the second and
third cube roots.
2
−b ± b − 4ac
x=
2a
19-104
Create Interactive Course Materials Using the Live Editor
To execute individual sections of MATLAB code, go to the Live Editor tab and click the
Run Section button. Output appears together with the code that created it. Create
sections using the Section Break button.
In our case a, b, and c are all equal to 1. The other two roots are calculated from these
formulas:
a = 1 ; b = 1 ; c = 1;
roots = [];
roots(1) = 1;
roots(2) = (-b + sqrt(b^2 - 4*a*c))/(2*a); % Use the quadratic formula
roots(3) = (-b - sqrt(b^2 - 4*a*c))/(2*a);
disp(roots')
1.0000 + 0.0000i
-0.5000 - 0.8660i
-0.5000 + 0.8660i
Include plots in the Live Editor so students can visualize important concepts.
We can visualize the roots in the complex plane to see their location.
range = 0:0.01:2*pi;
plot(cos(range),sin(range),'k') % Plot the unit circle
axis square; box off
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
hold on
plot(real(roots), imag(roots), 'ro') % Plot the roots
19-105
19 Live Scripts and Functions
To add supporting information, go to the Live Editor tab and click the Hyperlink and
Image buttons. Students can use supporting information to explore lecture topics outside
of the classroom.
Once you get past n = 3, things get even trickier. For 4th roots we could use the quartic
formula discovered by Lodovico Ferrari in 1540. But this formula is long and unwieldy,
and doesn't help us find roots higher than 4. Luckily, there is a better way, thanks to a
17th century French mathematician named Abraham de Moivre.
19-106
Create Interactive Course Materials Using the Live Editor
Abraham de Moivre was born in Vitry in Champagne on May 26, 1667. He was a
contemporary and friend of Isaac Newton, Edmund Halley, and James Stirling. https://
en.wikipedia.org/wiki/Abraham_de_Moivre
He is best known for de Moivre's theorem that links complex numbers and trigonometry,
and for his work on the normal distribution and probability theory. De Moivre wrote a
book on probability theory, The Doctrine of Chances, said to have been prized by
gamblers. De Moivre first discovered Binet's formula, the closed-form expression for
Fibonacci numbers linking the nth power of the golden ratio φ to the nth Fibonacci
number. He was also the first to postulate the Central Limit Theorem, a cornerstone of
probability theory.
de Moivre's theorem states that for any real x and any integer n,
n
cos x + i sin x = cos nx + i sin nx .
How does that help us solve our problem? We also know that for any integer k,
19-107
19 Live Scripts and Functions
Use the Live Editor to experiment with MATLAB code interactively. Add controls to show
students how important parameters affect the analysis. To add controls, go to the Live
Editor tab, click the Controls button, and select from the available options.
We can use this last equation to find the nth roots of 1. For example, for any value of n, we
can use the formula above with values of k = 0…n − 1. We can use this MATLAB code to
experiment with different values of n:
n = ;
roots = zeros(1, n);
for k = 0:n-1
roots(k+1) = cos(2*k*pi/n) + 1i*sin(2*k*pi/n); % Calculate the roots
end
disp(roots')
1.0000 + 0.0000i
0.5000 - 0.8660i
-0.5000 - 0.8660i
-1.0000 - 0.0000i
-0.5000 + 0.8660i
0.5000 + 0.8660i
Plotting the roots in the complex plane shows that the roots are equally spaced around
the unit circle at intervals of 2π/n.
cla
plot(cos(range),sin(range),'k') % Plot the unit circle
hold on
plot(real(roots),imag(roots),'ro') % Plot the roots
19-108
Create Interactive Course Materials Using the Live Editor
Use additional examples to reinforce important concepts. Modify code during the lecture
to answer questions or explore ideas in more depth.
We can find the roots of -1, i, and -i just by using extensions of the approach described
above. If we look at the unit circle we see that the values of 1, i, -1, -i appear at angles 0,
π/2, π, and 3π/2 respectively.
r = ones(1,4);
theta = [0 pi/2 pi 3*pi/2];
[x,y] = pol2cart(theta,r);
cla
plot(cos(range),sin(range),'k') % Plot the unit circle
19-109
19 Live Scripts and Functions
hold on
plot(x, y, 'ro') % Plot the values of 1, i, -1, and -i
text(x(1)+0.05,y(1),'1') % Add text labels
text(x(2),y(2)+0.1,'i')
text(x(3)-0.1,y(3),'-1')
text(x(4)-0.02,y(4)-0.1,'-i')
1/n 1/n
i = cos 2k + 1/2 π + i sin 2k + 1/2 π
19-110
See Also
Homework
Use live scripts as the basis for assignments. Give students the live script used in the
lecture and have them complete exercises that test their understanding of the material.
Exercise 3: Describe the mathematical approach you would use to calculate the nth roots
of an arbitrary complex number. Include the equations you used in your approach.
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
• MATLAB Live Script Gallery
19-111
19 Live Scripts and Functions
To view and interact with the controls, open this example in MATLAB®.
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
19-112
See Also
19-113
19 Live Scripts and Functions
To view and interact with the example, open it in MATLAB®. To view the code for this
example in MATLAB, go to the View tab and click Output Inline or Output on Right.
19-114
See Also
19-115
19 Live Scripts and Functions
19-116
Create a Real-time Dashboard Using the Live Editor
To view and interact with the example, open it in MATLAB®. To view the code for this
example in MATLAB, go to the View tab and click Output Inline or Output on Right.
19-117
19 Live Scripts and Functions
See Also
More About
• “Create Live Scripts in the Live Editor” on page 19-7
19-118
See Also
19-119
20
Function Basics
function f = fact(n)
f = prod(1:n);
end
This type of function must be defined within a file, not at the command line. Often, you
store a function in its own file. In that case, the best practice is to use the same name for
the function and the file (in this example, fact.m), since MATLAB associates the program
with the file name. Save the file either in the current folder or in a folder on the MATLAB
search path.
You can call the function from the command line, using the same syntax rules that apply
to functions installed with MATLAB. For instances, calculate the factorial of 5.
x = 5;
y = fact(5)
y =
120
Starting in R2016b, another option for storing functions is to include them at the end of a
script file. For instance, create a file named mystats.m with a few commands and two
functions, fact and perm. The script calculates the permutation of (3,2).
x = 3;
y = 2;
z = perm(x,y)
function p = perm(n,r)
p = fact(n)*fact(n-r);
end
function f = fact(n)
f = prod(1:n);
end
20-2
Create Functions in Files
mystats
z =
function myFunction(x)
function [] = myFunction(x)
20-3
20 Function Basics
Function name (required) Valid function names follow the same rules as variable names.
They must start with a letter, and can contain letters, digits,
or underscores.
Note To avoid confusion, use the same name for both the
function file and the first function within the file. MATLAB
associates your program with the file name, not the function
name. Script files cannot have the same name as a function in
the file.
Input arguments If your function accepts any inputs, enclose their names in
(optional) parentheses after the function name. Separate inputs with
commas.
function y = myFunction(one,two,three)
Tip When you define a function with multiple input or output arguments, list any
required arguments first. This ordering allows you to call your function without specifying
optional arguments.
Program files can contain multiple functions. If the file contains only function definitions,
the first function is the main function, and is the function that MATLAB associates with
the file name. Functions that follow the main function or script code are called local
functions. Local functions are only available within the file.
End Statements
Functions end with either an end statement, the end of the file, or the definition line for a
local function, whichever comes first. The end statement is required if:
20-4
See Also
• Any function in the file contains a nested function (a function completely contained
within its parent).
• The function is a local function within a function file, and any local function in the file
uses the end keyword.
• The function is a local function within a script file.
See Also
function
More About
• “Files and Folders that MATLAB Accesses”
• “Base and Function Workspaces” on page 20-10
• “Types of Functions” on page 20-20
• “Add Functions to Scripts” on page 18-18
20-5
20 Function Basics
Create help text by inserting comments at the beginning of your program. If your
program includes a function, position the help text immediately below the function
definition line (the line with the function keyword).
For example, create a function in a file named addme.m that includes help text:
function c = addme(a,b)
% ADDME Add two values together.
% C = ADDME(A) adds A to itself.
%
% C = ADDME(A,B) adds A and B together.
%
% See also SUM, PLUS.
switch nargin
case 2
c = a + b;
case 1
c = a + a;
otherwise
c = 0;
end
When you type help addme at the command line, the help text displays in the Command
Window:
addme Add two values together.
C = addme(A) adds A to itself.
The first help text line, often called the H1 line, typically includes the program name and
a brief description. The Current Folder browser and the help and lookfor functions use
the H1 line to display information about the program.
Create See also links by including function names at the end of your help text on a line
that begins with % See also. If the function exists on the search path or in the current
20-6
See Also
folder, the help command displays each of these function names as a hyperlink to its
help. Otherwise, help prints the function names as they appear in the help text.
You can include hyperlinks (in the form of URLs) to Web sites in your help text. Create
hyperlinks by including an HTML <a></a> anchor element. Within the anchor, use a
matlab: statement to execute a web command. For example:
End your help text with a blank line (without a %). The help system ignores any comment
lines that appear after the help text block.
Note When multiple programs have the same name, the help command determines
which help text to display by applying the rules described in “Function Precedence
Order” on page 20-43. However, if a program has the same name as a MathWorks
function, the Help on Selection option in context menus always displays documentation
for the MathWorks function.
See Also
help | lookfor
Related Examples
• “Add Comments to Programs” on page 18-4
• “Create Help Summary Files — Contents.m” on page 31-12
• “Check Which Programs Have Help” on page 31-9
• “Display Custom Documentation” on page 31-27
• “Use Help Files with MEX Functions”
20-7
20 Function Basics
3 Replace the text type code to run with an expression that allows you to run the
function.
y = myfunction(1:10)
For more complicated, multiline commands, create a separate script file, and then
run the script.
Note Run commands use the base workspace. Any variables that you define in a run
command can overwrite variables in the base workspace that have the same name.
4 Run the function by clicking Run or a specific run command from the drop-down list.
For myfunction.m, and an input of 1:10, this result appears in the Command
Window:
20-8
Run Functions in the Editor
y =
2 6 12 20 30 42 56 72 90 110
When you select a run command from the list, it becomes the default for the Run
button.
To edit or delete an existing run command, select the command, right-click, and then
select Edit or Delete.
20-9
20 Function Basics
The base workspace stores variables that you create at the command line. This includes
any variables that scripts create, assuming that you run the script from the command line
or from the Editor. Variables in the base workspace exist until you clear them or end your
MATLAB session.
Functions do not use the base workspace. Every function has its own function workspace.
Each function workspace is separate from the base workspace and all other workspaces
to protect the integrity of the data. Even local functions in a common file have their own
workspaces. Variables specific to a function workspace are called local variables.
Typically, local variables do not remain in memory from one function call to the next.
When you call a script from a function, the script uses the function workspace.
Like local functions, nested functions have their own workspaces. However, these
workspaces are unique in two significant ways:
• Nested functions can access and modify variables in the workspaces of the functions
that contain them.
• All of the variables in nested functions or the functions that contain them must be
explicitly defined. That is, you cannot call a function or script that assigns values to
variables unless those variables already exist in the function workspace.
See Also
Related Examples
• “Share Data Between Workspaces” on page 20-11
More About
• “Nested Functions” on page 20-32
20-10
Share Data Between Workspaces
Introduction
This topic shows how to share variables between workspaces or allow them to persist
between function executions.
In most cases, variables created within a function are local variables known only within
that function. Local variables are not available at the command line or to any other
function. However, there are several ways to share data between functions or
workspaces.
For example, create two functions, update1 and update2, that share and modify an
input value. update2 can be a local function in the file update1.m, or can be a function
in its own file, update2.m.
function y1 = update1(x1)
y1 = 1 + update2(x1);
function y2 = update2(x2)
y2 = 2 * x2;
Call the update1 function from the command line and assign to variable Y in the base
workspace:
20-11
20 Function Basics
X = [1,2,3];
Y = update1(X)
Y =
3 5 7
Nested Functions
A nested function has access to the workspaces of all functions in which it is nested. So,
for example, a nested function can use a variable (in this case, x) that is defined in its
parent function:
function primaryFx
x = 1;
nestedFx
function nestedFx
x = x + 1;
end
end
When parent functions do not use a given variable, the variable remains local to the
nested function. For example, in this version of primaryFx, the two nested functions
have their own versions of x that cannot interact with each other.
function primaryFx
nestedFx1
nestedFx2
function nestedFx1
x = 1;
end
function nestedFx2
x = 2;
end
end
Persistent Variables
When you declare a variable within a function as persistent, the variable retains its value
from one function call to the next. Other local variables retain their value only during the
20-12
Share Data Between Workspaces
Declare variables using the persistent keyword before you use them. MATLAB
initializes persistent variables to an empty matrix, [].
For example, define a function in a file named findSum.m that initializes a sum to 0, and
then adds to the value on each iteration.
function findSum(inputvalue)
persistent SUM_X
if isempty(SUM_X)
SUM_X = 0;
end
SUM_X = SUM_X + inputvalue;
When you call the function, the value of SUM_X persists between subsequent executions.
• clear all
• clear functionname
• Editing the function file
To prevent clearing persistent variables, lock the function file using mlock.
Global Variables
Global variables are variables that you can access from functions or from the command
line. They have their own workspace, which is separate from the base and function
workspaces.
• Any function can access and update a global variable. Other functions that use the
variable might return unexpected results.
• If you unintentionally give a “new” global variable the same name as an existing global
variable, one function can overwrite the values expected by another. This error is
difficult to diagnose.
20-13
20 Function Basics
If you use global variables, declare them using the global keyword before you access
them within any particular location (function or command line). For example, create a
function in a file called falling.m:
function h = falling(t)
global GRAVITY
h = 1/2*GRAVITY*t.^2;
global GRAVITY
GRAVITY = 32;
y = falling((0:.1:5)');
The two global statements make the value assigned to GRAVITY at the command prompt
available inside the function. However, as a more robust alternative, redefine the function
to accept the value as an input:
function h = falling(t,gravity)
h = 1/2*gravity*t.^2;
GRAVITY = 32;
y = falling((0:.1:5)',GRAVITY);
Like global variables, these functions carry risks of overwriting existing data. Use them
sparingly.
evalin and assignin are sometimes useful for callback functions in graphical user
interfaces to evaluate against the base workspace. For example, create a list box of
variable names from the base workspace:
function listBox
figure
lb = uicontrol('Style','listbox','Position',[10 10 100 100],...
'Callback',@update_listBox);
update_listBox(lb)
20-14
See Also
function update_listBox(src,~)
vars = evalin('base','who');
src.String = vars;
For other programming applications, consider argument passing and the techniques
described in “Alternatives to the eval Function” on page 2-110.
See Also
More About
• “Base and Function Workspaces” on page 20-10
20-15
20 Function Basics
Scoping issues can be the source of some coding problems. For instance, if you are
unaware that nested functions share a particular variable, the results of running your
code might not be as you expect. Similarly, mistakes in usage of local, global, and
persistent variables can cause unexpected results.
The Code Analyzer does not always indicate scoping issues because sharing a variable
across functions is not an error—it may be your intent. Use MATLAB function and
variable highlighting features to identify when and where your code uses functions and
variables. If you have an active Internet connection, you can watch the Variable and
Function Highlighting video for an overview of the major features.
For conceptual information on nested functions and the various types of MATLAB
variables, see “Sharing Variables Between Parent and Nested Functions” on page 20-33
and “Share Data Between Workspaces” on page 20-11.
To enable and disable highlighting or to change the colors, click Preferences and
select MATLAB > Colors > Programming tools. In MATLAB Online, highlighting is
enabled by default and changing the preferences for highlighting is not available.
• Highlights all instances of a given function or local variable in sky blue when you place
the cursor within a function or variable name. For instance:
20-16
Check Variable Scope in Editor
• Displays a variable with shared scope in teal blue, regardless of the cursor location.
For instance:
x = ones(2,10);
[n, m] = size(x);
rowTotals = zeros(1,n);
for i = 1:n
rowTotals(i) = addToSum;
end
end
When you run this code, instead of returning the sum of the values in each row and
displaying:
ans =
10 10
MATLAB displays:
ans =
0 0 0 0 0 0 0 0 0 10
20-17
20 Function Basics
1
On the Home tab in the Environment section, click Preferences and select
MATLAB > Colors > Programming tools. Ensure that Automatically highlight
and Variables with shared scope are selected.
2 Copy the rowsum code into the Editor.
Notice the variable appears in teal blue, which indicates i is not a local variable.
Both the rowTotals function and the addToSum functions set and use the variable i.
The variable n, at line 6 appears in black, indicating that it does not span multiple
functions.
Every reference to i highlights in sky blue and markers appear in the indicator bar
on the right side of the Editor.
20-18
Check Variable Scope in Editor
A tooltip appears and displays the name of the function or variable and the line of
code represented by the marker.
7 Click a marker to navigate to the line indicated in tooltip for that marker.
This is particularly useful when your file contains more code than you can view at one
time in the Editor.
You can see similar highlighting effects when you click on a function reference. For
instance, click on addToSum.
20-19
20 Function Basics
Types of Functions
In this section...
“Local and Nested Functions in a File” on page 20-20
“Private Functions in a Subfolder” on page 20-21
“Anonymous Functions Without a File” on page 20-21
Local functions are subroutines that are available within the same file. Local functions are
the most common way to break up programmatic tasks. In a function file, which contains
only function definitions, local functions can appear in the file in any order after the main
function in the file. In a script file, which contains commands and function definitions,
local function must be at the end of the file. (Functions in scripts are supported in R2016b
or later.)
For example, create a function file named myfunction.m that contains a main function,
myfunction, and two local functions, squareMe and doubleMe:
function b = myfunction(a)
b = squareMe(a)+doubleMe(a);
end
function y = squareMe(x)
y = x.^2;
end
function y = doubleMe(x)
y = x.*2;
end
You can call the main function from the command line or another program file, although
the local functions are only available to myfunction:
myfunction(pi)
ans =
16.1528
20-20
Types of Functions
Nested functions are completely contained within another function. The primary
difference between nested functions and local functions is that nested functions can use
variables defined in parent functions without explicitly passing those variables as
arguments.
Nested functions are useful when subroutines share data, such as applications that pass
data between components. For example, create a function that allows you to set a value
between 0 and 1 using either a slider or an editable text box. If you use nested functions
for the callbacks, the slider and text box can share the value and each other’s handles
without explicitly passing them:
function myslider
value = 0;
f = figure;
s = uicontrol(f,'Style','slider','Callback',@slider);
e = uicontrol(f,'Style','edit','Callback',@edittext,...
'Position',[100,20,100,20]);
function slider(obj,~)
value = obj.Value;
e.String = num2str(value);
end
function edittext(obj,~)
value = str2double(obj.String);
s.Value = value;
end
end
20-21
20 Function Basics
functions is to define a mathematical expression, and then evaluate that expression over a
range of values using a MATLAB® function function, i.e., a function that accepts a
function handle as an input.
For example, this statement creates a function handle named s for an anonymous
function:
s = @(x) sin(1./x);
This function has a single input, x. The @ operator creates the function handle.
You can use the function handle to evaluate the function for particular values, such as
y = s(pi)
y = 0.3130
Or, you can pass the function handle to a function that evaluates over a range of values,
such as fplot:
range = [0.01,0.1];
fplot(s,range)
20-22
See Also
See Also
More About
• “Local Functions” on page 20-30
• “Nested Functions” on page 20-32
• “Private Functions” on page 20-41
• “Anonymous Functions” on page 20-24
20-23
20 Function Basics
Anonymous Functions
In this section...
“What Are Anonymous Functions?” on page 20-24
“Variables in the Expression” on page 20-25
“Multiple Anonymous Functions” on page 20-26
“Functions with No Inputs” on page 20-27
“Functions with Multiple Inputs or Outputs” on page 20-27
“Arrays of Anonymous Functions” on page 20-28
For example, create a handle to an anonymous function that finds the square of a number:
Variable sqr is a function handle. The @ operator creates the handle, and the parentheses
() immediately after the @ operator include the function input arguments. This
anonymous function accepts a single input x, and implicitly returns a single output, an
array the same size as x that contains the squared values.
Find the square of a particular value (5) by passing the value to the function handle, just
as you would pass an input argument to a standard function.
a = sqr(5)
a =
25
Many MATLAB functions accept function handles as inputs so that you can evaluate
functions over a range of values. You can create handles either for anonymous functions
or for functions in program files. The benefit of using anonymous functions is that you do
not have to edit and maintain a file for a function that requires only a brief definition.
20-24
Anonymous Functions
For example, find the integral of the sqr function from 0 to 1 by passing the function
handle to the integral function:
q = integral(sqr,0,1);
You do not need to create a variable in the workspace to store an anonymous function.
Instead, you can create a temporary function handle within an expression, such as this
call to the integral function:
q = integral(@(x) x.^2,0,1);
For example, create a function handle to an anonymous function that requires coefficients
a, b, and c.
a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c;
Because a, b, and c are available at the time you create parabola, the function handle
includes those values. The values persist within the function handle even if you clear the
variables:
clear a b c
x = 1;
y = parabola(x)
y =
31.5000
To supply different values for the coefficients, you must create a new function handle:
a = -3.9;
b = 52;
c = 0;
parabola = @(x) a*x.^2 + b*x + c;
x = 1;
y = parabola(1)
20-25
20 Function Basics
y =
48.1000
You can save function handles and their associated values in a MAT-file and load them in a
subsequent MATLAB session using the save and load functions, such as
save myfile.mat parabola
The final function allows you to solve the equation for any value of c. For example:
g(2)
20-26
Anonymous Functions
ans =
2.3333
t = @() datestr(now);
d = t()
d =
26-Jan-2012 15:11:47
Omitting the parentheses in the assignment statement creates another function handle,
and does not execute the function:
d = t
d =
@() datestr(now)
x = 1;
y = 10;
z = myfunction(x,y)
z = 111
However, you do not explicitly define output arguments when you create an anonymous
function. If the expression in the function returns multiple outputs, then you can request
them when you call the function. Enclose multiple output variables in square brackets.
For example, the ndgrid function can return as many outputs as the number of input
vectors. This anonymous function that calls ndgrid can also return multiple outputs:
20-27
20 Function Basics
c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi);
You can use the output from mygrid to create a mesh or surface plot:
z = sin(x) + cos(y);
mesh(x,y,z)
20-28
See Also
function handles using a cell array or structure array. The most common approach is to
use a cell array, such as
f = {@(x)x.^2;
@(y)y+10;
@(x,y)x.^2+y+10};
When you create the cell array, keep in mind that MATLAB interprets spaces as column
separators. Either omit spaces from expressions, as shown in the previous code, or
enclose expressions in parentheses, such as
f = {@(x) (x.^2);
@(y) (y + 10);
@(x,y) (x.^2 + y + 10)};
Access the contents of a cell using curly braces. For example, f{1} returns the first
function handle. To execute the function, pass input values in parentheses after the curly
braces:
x = 1;
y = 10;
f{1}(x)
f{2}(y)
f{3}(x,y)
ans =
1
ans =
20
ans =
21
See Also
More About
• “Create Function Handle” on page 13-2
20-29
20 Function Basics
Local Functions
This topic explains the term local function, and shows how to create and use local
functions.
MATLAB program files can contain code for more than one function. In a function file, the
first function in the file is called the main function. This function is visible to functions in
other files, or you can call it from the command line. Additional functions within the file
are called local functions, and they can occur in any order after the main function. Local
functions are only visible to other functions in the same file. They are equivalent to
subroutines in other programming languages, and are sometimes called subfunctions.
As of R2016b, you can also create local functions in a script file, as long as they all appear
after the last line of script code. For more information, see “Add Functions to Scripts” on
page 18-18.
For example, create a function file named mystats.m that contains a main function,
mystats, and two local functions, mymean and mymedian.
function a = mymean(v,n)
% MYMEAN Example of a local function.
a = sum(v)/n;
end
function m = mymedian(v,n)
% MYMEDIAN Another example of a local function.
w = sort(v);
if rem(n,2) == 1
m = w((n + 1)/2);
else
m = (w(n/2) + w(n/2 + 1))/2;
end
end
20-30
See Also
The local functions mymean and mymedian calculate the average and median of the input
list. The main function mystats determines the length of the list n and passes it to the
local functions.
Although you cannot call a local function from the command line or from functions in
other files, you can access its help using the help function. Specify names of both the file
and the local function, separating them with a > character:
help mystats>mymean
Local functions in the current file have precedence over functions in other files. That is,
when you call a function within a program file, MATLAB checks whether the function is a
local function before looking for other main functions. Therefore, you can create an
alternate version of a particular function while retaining the original in another file.
All functions, including local functions, have their own workspaces that are separate from
the base workspace. Local functions cannot access variables used by other functions
unless you pass them as arguments. In contrast, nested functions (functions completely
contained within another function) can access variables used by the functions that
contain them.
See Also
localfunctions
More About
• “Nested Functions” on page 20-32
• “Function Precedence Order” on page 20-43
20-31
20 Function Basics
Nested Functions
In this section...
“What Are Nested Functions?” on page 20-32
“Requirements for Nested Functions” on page 20-32
“Sharing Variables Between Parent and Nested Functions” on page 20-33
“Using Handles to Store Function Parameters” on page 20-34
“Visibility of Nested Functions” on page 20-37
For example, this function named parent contains a nested function named nestedfx:
function parent
disp('This is the parent function')
nestedfx
function nestedfx
disp('This is the nested function')
end
end
The primary difference between nested functions and other types of functions is that they
can access and modify variables that are defined in their parent functions. As a result:
• Nested functions can use variables that are not explicitly passed as input arguments.
• In a parent function, you can create a handle to a nested function that contains the
data necessary to run the nested function.
20-32
Nested Functions
• You cannot define a nested function inside any of the MATLAB program control
statements, such as if/elseif/else, switch/case, for, while, or try/catch.
• You must call a nested function either directly by name (without using feval), or
using a function handle that you created using the @ operator (and not str2func).
• All of the variables in nested functions or the functions that contain them must be
explicitly defined. That is, you cannot call a function or script that assigns values to
variables unless those variables already exist in the function workspace. (For more
information, see “Variables in Nested and Anonymous Functions” on page 20-39.)
This means that both a nested function and a function that contains it can modify the
same variable without passing that variable as an argument. For example, in each of
these functions, main1 and main2, both the main function and the nested function can
access variable x:
When parent functions do not use a given variable, the variable remains local to the
nested function. For example, in this function named main, the two nested functions have
their own versions of x that cannot interact with each other:
function main
nestedfun1
nestedfun2
function nestedfun1
x = 1;
end
20-33
20 Function Basics
function nestedfun2
x = 2;
end
end
Functions that return output arguments have variables for the outputs in their workspace.
However, parent functions only have variables for the output of nested functions if they
explicitly request them. For example, this function parentfun does not have variable y in
its workspace:
function parentfun
x = 5;
nestfun;
function y = nestfun
y = x + 1;
end
end
function y = nestfun
y = x + 1;
end
end
• Input arguments
• Variables defined within the nested function
• Variables defined in a parent function, also called externally scoped variables
When you create a function handle for a nested function, that handle stores not only the
name of the function, but also the values of externally scoped variables.
20-34
Nested Functions
For example, create a function in a file named makeParabola.m. This function accepts
several polynomial coefficients, and returns a handle to a nested function that calculates
the value of that polynomial.
function p = makeParabola(a,b,c)
p = @parabola;
function y = parabola(x)
y = a*x.^2 + b*x + c;
end
end
The makeParabola function returns a handle to the parabola function that includes
values for coefficients a, b, and c.
At the command line, call the makeParabola function with coefficient values of 1.3, .2,
and 30. Use the returned function handle p to evaluate the polynomial at a particular
point:
p = makeParabola(1.3,.2,30);
X = 25;
Y = p(X)
Y =
847.5000
Many MATLAB functions accept function handle inputs to evaluate functions over a range
of values. For example, plot the parabolic equation from -25 to +25:
fplot(p,[-25,25])
20-35
20 Function Basics
You can create multiple handles to the parabola function that each use different
polynomial coefficients:
firstp = makeParabola(0.8,1.6,32);
secondp = makeParabola(3,4,50);
range = [-25,25];
figure
hold on
fplot(firstp,range)
fplot(secondp,range,'r:')
hold off
20-36
Nested Functions
• From the level immediately above it. (In the following code, function A can call B or D,
but not C or E.)
• From a function nested at the same level within the same parent function. (Function B
can call D, and D can call B.)
• From a function at any lower level. (Function C can call B or D, but not E.)
function A(x, y) % Main function
B(x,y)
20-37
20 Function Basics
D(y)
The easiest way to extend the scope of a nested function is to create a function handle
and return it as an output argument, as shown in “Using Handles to Store Function
Parameters” on page 20-34. Only functions that can call a nested function can create a
handle to it.
See Also
More About
• “Variables in Nested and Anonymous Functions” on page 20-39
• “Create Function Handle” on page 13-2
• “Argument Checking in Nested Functions” on page 21-11
20-38
Variables in Nested and Anonymous Functions
This table describes typical operations that attempt dynamic assignment, and the
recommended ways to avoid it.
K>> assignin('base','X',myvalue)
Another way to avoid dynamic assignment is to explicitly declare the variable within the
function. For example, suppose a script named makeX.m assigns a value to variable X. A
function that calls makeX and explicitly declares X avoids the dynamic assignment error
because X is in the function workspace. A common way to declare a variable is to initialize
its value to an empty array:
function noerror
X = [];
nestedfx
function nestedfx
20-39
20 Function Basics
makeX
end
end
See Also
More About
• “Base and Function Workspaces” on page 20-10
20-40
Private Functions
Private Functions
This topic explains the term private function, and shows how to create and use private
functions.
Private functions are useful when you want to limit the scope of a function. You designate
a function as private by storing it in a subfolder with the name private. Then, the
function is available only to functions in the folder immediately above the private
subfolder, or to scripts called by the functions that reside in the parent folder.
For example, within a folder that is on the MATLAB search path, create a subfolder
named private. Do not add private to the path. Within the private folder, create a
function in a file named findme.m:
function findme
% FINDME An example of a private function.
Change to the folder that contains the private folder and create a file named
visible.m.
function visible
findme
Change your current folder to any location and call the visible function.
visible
Although you cannot call the private function from the command line or from functions
outside the parent of the private folder, you can access its help:
help private/findme
Private functions have precedence over standard functions, so MATLAB finds a private
function named test.m before a nonprivate program file named test.m. This allows you
to create an alternate version of a particular function while retaining the original in
another folder.
20-41
20 Function Basics
See Also
More About
• “Function Precedence Order” on page 20-43
20-42
Function Precedence Order
Before assuming that a name matches a function, MATLAB checks for a variable with
that name in the current workspace.
Note If you create a variable with the same name as a function, MATLAB cannot run
that function until you clear the variable from memory.
2 Function or class whose name matches an explicitly imported name
The import function allows functions with compound names (names comprised of
several parts joined by dots) to be called using only the final part of the compound
name. When a function name matches an explicit (non-wildcard) imported function,
MATLAB uses the imported compound name and gives it precedence over all other
functions with the same name.
3 Nested functions within the current function
4 Local functions within the current file
5 Function or class whose name matches a wildcard-based imported name
An object function accepts a particular class of object in its input argument list. When
there are multiple object functions with the same name, MATLAB checks the classes
of the input arguments to determine which function to use.
20-43
20 Function Basics
When determining the precedence of functions within the same folder, MATLAB considers
the file type, in this order:
1 Built-in function
2 MEX-function
3 Simulink model files that are not loaded, with file types in this order:
a SLX file
b MDL file
4 App file (.mlapp) created using MATLAB App Designer
5 Program file with a .mlx extension
6 P-file (that is, an encoded program file with a .p extension)
7 Program file with a .m extension
For example, if MATLAB finds a .m file and a P-file with the same name in the same folder,
it uses the P-file. Because P-files are not automatically regenerated, make sure that you
regenerate the P-file whenever you edit the program file.
To determine the function MATLAB calls for a particular input, include the function name
and the input in a call to the which function.
20-44
See Also
See Also
import
More About
• “What Is the MATLAB Search Path?”
• Variables on page 1-6
• “Types of Functions” on page 20-20
• “Class Precedence and MATLAB Path”
20-45
20 Function Basics
These changes impact the behavior of the import function. You should analyze and
possibly update your code. To start, search your code for import statements. For example,
use “Find Files and Folders” to search for .m and .mlx files containing text import.
Refer to these search results when evaluating the effects of the following changes.
If this behavior change impacts your code, rename either the variable or the function so
that they have different names.
20-46
Update Code for R2019b Changes to Function Precedence Order
Consider the following code. MATLAB used to treat x as a variable because of colon-
indexing. Starting in R2019b, if a function of the same name exists on the path, MATLAB
treats x as a function.
function myfunc
load data.mat; % data.mat contains variable x
disp(x(:))
end
This table shows some examples of how you can update your code.
Before After
function myfunc function myfunc
load data.mat; load data.mat x;
disp(x(:)) disp(x(:))
end end
function myfunc2 function myfunc2
myscript; % Contains variable x x = [];
disp(x(:)) myscript;
end disp(x(:))
end
20-47
20 Function Basics
For example, in the following code, identifier x in myfunc is different from variable x in
the nested function. If x is a function on the path, MATLAB treats x in myfunc as a
function and the code runs. Otherwise, MATLAB throws an error.
function myfunc
nested;
x(3) % x is not a shared variable
function nested
x = [1 2 3];
end
end
To use an identifier as a variable shared between parent and nested functions, you might
need to update your code. For example, you can initialize the identifier to an empty array
in the parent function.
Before After
function myfunc function myfunc
nested; x = [];
x(3) nested;
function nested x(3)
x = [1 2 3]; function nested
end x = [1 2 3];
end end
end
For example, in this code, the statement local() calls myfunc/local instead of
pkg1.local in the wildcard-based import. The statement nest() calls myfunc/nest
instead of pkg1.nest.
20-48
Update Code for R2019b Changes to Function Precedence Order
In the search results for import, look for statements that include the wildcard character
(*).
20-49
20 Function Basics
20-50
Update Code for R2019b Changes to Function Precedence Order
20-51
20 Function Basics
% Package p1 has functions plot and bar % Package p1 has functions plot and bar
import p1.plot import p1.plot
import p1.* import p1.*
nest nest
For example, suppose a package pkg contains a class foo with a static method bar and
also a subpackage foo with a function bar.
In R2019b, a call to which pkg.foo.bar returns the path to the package function.
which pkg.foo.bar
+pkg/+foo/bar.m
Previously, a static method took precedence over a package function in cases where a
package and a class had the same name.
20-52
See Also
See Also
import
More About
• “Import Classes”
20-53
20 Function Basics
Example
Consider the function:
function y = myStruct(x)
y = struct("Afield",x);
end
This function creates a structure with one field, named Afield, and assigns a value to
the field. You can invoke the function and create a structure with a field containing the
value 1 with the command:
myStruct(1)
ans =
Afield: 1
However, if you want to return the field value directly, you can index into the function call
result with the command:
myStruct(1).Afield
ans =
After this command executes, the temporary structure created by the command
myStruct(1) no longer exists, and MATLAB returns only the field value. Conceptually,
this usage is the same as creating the structure, indexing into it, and then deleting it:
20-54
See Also
S = struct("Afield",1);
S.Afield
clear S
Supported Syntaxes
MATLAB supports dot indexing into function call results, as in foo(arg).prop. Other
forms of indexing into function call results (with parentheses such as foo(arg)(2) or
with curly braces such as foo(arg){2}) are not supported. Successful commands must
meet the criteria:
MATLAB always attempts to apply the dot indexing operation to the temporary variable,
even if the function returns a variable for which dot indexing is not defined. For example,
if you try to index into the matrix created by magic(3), then you get an error.
magic(3).field
You can add more indexing commands onto the end of an expression as long as the
temporary variables can continue to be indexed. For example, consider the expression:
table(rand(10,2)).Var1(3,:)
In this expression, you index into a table to get the matrix it contains, and then index into
the matrix to get the third row:
• table(rand(10,2)) creates a table with one variable named Var1. The variable
contains a 10-by-2 matrix.
• table(rand(10,2)).Var1 returns the 10-by-2 matrix contained in Var1.
• table(rand(10,2)).Var1(3,:) returns the third row in the matrix contained in
Var1.
See Also
function | subsref
20-55
20 Function Basics
More About
• “Types of Functions” on page 20-20
• “Array Indexing”
• “Access Data in Tables” on page 9-44
20-56
21
Function Arguments
Input Arguments
Create a function in a file named addme.m that accepts up to two inputs. Identify the
number of inputs with nargin.
function c = addme(a,b)
switch nargin
case 2
c = a + b;
case 1
c = a + a;
otherwise
c = 0;
end
addme(42)
ans =
84
addme(2,4000)
ans =
4002
addme
ans =
0
Output Arguments
Create a new function in a file named addme2.m that can return one or two outputs (a
result and its absolute value). Identify the number of requested outputs with nargout.
21-2
See Also
switch nargin
case 2
result = a + b;
case 1
result = a + a;
otherwise
result = 0;
end
if nargout > 1
absResult = abs(result);
end
value = addme2(11,-22)
value =
-11
[value,absValue] = addme2(11,-22)
value =
-11
absValue =
11
Functions return outputs in the order they are declared in the function definition.
See Also
nargin | narginchk | nargout | nargoutchk
21-3
21 Function Arguments
function plotWithTitle(varargin)
if rem(nargin,2) ~= 0
myTitle = varargin{nargin};
numPlotInputs = nargin - 1;
else
myTitle = 'Default Title';
numPlotInputs = nargin;
end
plot(varargin{1:numPlotInputs})
title(myTitle)
Because varargin is a cell array, you access the contents of each cell using curly braces,
{}. The syntax varargin{1:numPlotInputs} creates a comma-separated list of inputs
to the plot function.
x = [1:.1:10];
y1 = sin(x);
y2 = cos(x);
plotWithTitle(x,y1,x,y2,'Sine and Cosine')
You can use varargin alone in an input argument list, or at the end of the list of inputs,
such as
function myfunction(a,b,varargin)
21-4
See Also
In this case, varargin{1} corresponds to the third input passed to the function, and
nargin returns length(varargin) + 2.
See Also
nargin | varargin
Related Examples
• “Access Data in Cell Array” on page 12-5
More About
• “Argument Checking in Nested Functions” on page 21-11
• “Comma-Separated Lists” on page 2-101
21-5
21 Function Arguments
Create a function in a file named magicfill.m that assigns a magic square to each
requested output.
for k = 1:nOutputs
varargout{k} = magic(k);
end
[first,second,third] = magicfill
first =
1
second =
1 3
4 2
third =
8 1 6
3 5 7
4 9 2
MATLAB assigns values to the outputs according to their order in the varargout array.
For example, first == varargout{1}.
You can use varargout alone in an output argument list, or at the end of the list of
outputs, such as
21-6
See Also
In this case, varargout{1} corresponds to the third output that the function returns,
and nargout returns length(varargout) + 2.
See Also
nargout | varargout
Related Examples
• “Access Data in Cell Array” on page 12-5
More About
• “Argument Checking in Nested Functions” on page 21-11
21-7
21 Function Arguments
MATLAB checks whether your function receives more arguments than expected when it
can determine the number from the function definition. For example, this function accepts
up to two outputs and three inputs:
If you pass too many inputs to myFunction, MATLAB issues an error. You do not need to
call narginchk to check for this case.
[X,Y] = myFunction(1,2,3,4)
Use the narginchk and nargoutchk functions to verify that your function receives:
Define a function in a file named testValues.m that requires at least two inputs. The
first input is a threshold value to compare against the other inputs.
function testValues(threshold,varargin)
minInputs = 2;
maxInputs = Inf;
narginchk(minInputs,maxInputs)
for k = 1:(nargin-1)
if (varargin{k} > threshold)
fprintf('Test value %d exceeds %d\n',k,threshold);
end
end
21-8
Validate Number of Function Arguments
testValues(10)
testValues(10,1,11,111)
Define a function in a file named mysize.m that returns the dimensions of the input array
in a vector (from the size function), and optionally returns scalar values corresponding
to the sizes of each dimension. Use nargoutchk to verify that the number of requested
individual sizes does not exceed the number of available dimensions.
sizeVector = size(x);
varargout = cell(1,nargout-1);
for k = 1:length(varargout)
varargout{k} = sizeVector(k);
end
A = rand(3,4,2);
[fullsize,nrows,ncols,npages] = mysize(A)
fullsize =
3 4 2
nrows =
3
ncols =
21-9
21 Function Arguments
npages =
2
A = 1;
[fullsize,nrows,ncols,npages] = mysize(A)
See Also
narginchk | nargoutchk
Related Examples
• “Support Variable Number of Inputs” on page 21-4
• “Support Variable Number of Outputs” on page 21-6
21-10
Argument Checking in Nested Functions
varargin and varargout allow you to create functions that accept variable numbers of
input or output arguments. Although varargin and varargout look like function names,
they refer to variables, not functions. This is significant because nested functions share
the workspaces of the functions that contain them.
If you do not use varargin or varargout in the declaration of a nested function, then
varargin or varargout within the nested function refers to the arguments of an outer
function.
For example, create a function in a file named showArgs.m that uses varargin and has
two nested functions, one that uses varargin and one that does not.
function showArgs(varargin)
nested1(3,4)
nested2(5,6,7)
function nested1(a,b)
disp('nested1: Contents of varargin{1}')
disp(varargin{1})
end
function nested2(varargin)
disp('nested2: Contents of varargin{1}')
disp(varargin{1})
end
end
Call the function and compare the contents of varargin{1} in the two nested functions.
showArgs(0,1,2)
21-11
21 Function Arguments
On the other hand, nargin and nargout are functions. Within any function, including
nested functions, calls to nargin or nargout return the number of arguments for that
function. If a nested function requires the value of nargin or nargout from an outer
function, pass the value to the nested function.
For example, create a function in a file named showNumArgs.m that passes the number of
input arguments from the primary (parent) function to a nested function.
function showNumArgs(varargin)
function nestedFx(n,varargin)
disp(['Number of inputs to nestedFx: ',int2str(nargin)]);
disp(['Number of inputs to its parent: ',int2str(n)]);
end
end
Call showNumArgs and compare the output of nargin in the parent and nested functions.
showNumArgs(0,1)
See Also
nargin | nargout | varargin | varargout
21-12
Ignore Function Inputs
Use this operator when your function must accept a predefined set of inputs, but your
function does not use all of the inputs. Common applications include defining callback
functions, as shown here.
Define a callback for a push button in a file named colorButton.m that does not use the
eventdata input. Ignore the input with a tilde.
function colorButton
figure;
uicontrol('Style','pushbutton','String','Click me','Callback',@btnCallback)
function btnCallback(h,~)
set(h,'BackgroundColor',rand(3,1))
The function declaration for btnCallback is effectively the same as the following.
function btnCallback(h,eventdata)
However, using the tilde prevents the addition of eventdata to the function workspace
and makes it clearer that the function does not use eventdata.
You can ignore any number of inputs in your function definition, in any position in the
argument list. Separate consecutive tildes with a comma. For example:
function myFunction(myInput,~,~)
See Also
More About
• “Ignore Function Outputs” on page 1-4
21-13
21 Function Arguments
validateattributes requires that you pass the variable to check and the supported
data types for that variable. Optionally, pass a set of attributes that describe the valid
dimensions or values.
Define a function in a file named checkme.m that accepts up to three inputs: a, b, and c.
Check whether:
function checkme(a,b,c)
validateattributes(a,{'double'},{'positive','2d'})
validateattributes(b,{'numeric'},{'numel',100,'ncols',10})
validateattributes(c,{'char','cell'},{'nonempty'})
The curly braces {} indicate that the set of data types and the set of additional attributes
are in cell arrays. Cell arrays allow you to store combinations of text and numeric data, or
character vectors of different lengths, in a single variable.
checkme(pi,rand(5,10,2),'text')
Call checkme with invalid inputs. The validateattributes function issues an error for
the first input that fails validation, and checkme stops processing.
checkme(-4)
21-14
Check Function Inputs with validateattributes
checkme(pi,rand(3,4,2))
checkme(pi,rand(5,10,2),struct)
char, cell
The default error messages use the generic term input to refer to the argument that
failed validation. When you use the default error message, the only way to determine
which input failed is to view the specified line of code in checkme.
Define a function in a file named checkdetails.m that performs the same validation as
checkme, but adds details about the input name and position to the error messages.
function checkdetails(a,b,c)
validateattributes(a,{'double'},{'positive','2d'},'','First',1)
validateattributes(b,{'numeric'},{'numel',100,'ncols',10},'','Second',2)
validateattributes(c,{'char'},{'nonempty'},'','Third',3)
The empty character vector '' for the fourth input to validateattributes is a
placeholder for an optional function name. You do not need to specify a function name
because it already appears in the error message. Specify the function name when you
want to include it in the error identifier for additional error handling.
checkdetails(-4)
21-15
21 Function Arguments
checkdetails(pi,rand(3,4,2))
See Also
validateattributes | validatestring
21-16
Parse Function Inputs
The Input Parser provides a consistent way to validate and assign defaults to inputs,
improving the robustness and maintainability of your code. To validate the inputs, you can
take advantage of existing MATLAB functions or write your own validation routines.
Create a function in a file named printPhoto.m. The printPhoto function has one
required input for the file name, and optional inputs for the finish (glossy or matte), color
space (RGB or CMYK), width, and height.
function printPhoto(filename,varargin)
In your function declaration statement, specify required inputs first. Use varargin to
support optional inputs.
p = inputParser;
Add inputs to the parsing scheme in your function using addRequired, addOptional, or
addParameter. For optional inputs, specify default values.
For each input, you can specify a handle to a validation function that checks the input and
returns a scalar logical (true or false) or errors. The validation function can be an
existing MATLAB function (such as ischar or isnumeric) or a function that you create
(such as an anonymous function or a local function).
In the printPhoto function, filename is a required input. Define finish and color as
optional inputs, and width and height as optional parameter value pairs.
defaultFinish = 'glossy';
validFinishes = {'glossy','matte'};
checkFinish = @(x) any(validatestring(x,validFinishes));
21-17
21 Function Arguments
defaultColor = 'RGB';
validColors = {'RGB','CMYK'};
checkColor = @(x) any(validatestring(x,validColors));
defaultWidth = 6;
defaultHeight = 4;
addRequired(p,'filename',@ischar);
addOptional(p,'finish',defaultFinish,checkFinish)
addOptional(p,'color',defaultColor,checkColor)
addParameter(p,'width',defaultWidth,@isnumeric)
addParameter(p,'height',defaultHeight,@isnumeric)
Inputs that you add with addRequired or addOptional are positional arguments. When
you call a function with positional inputs, specify those values in the order they are added
to the parsing scheme.
Inputs added with addParameter are not positional, so you can pass values for height
before or after values for width. However, parameter value inputs require that you pass
the input name ('height' or 'width') along with the value of the input.
If your function accepts optional input strings or character vectors and parameter name
and value pairs, specify validation functions for the optional inputs. Otherwise, the Input
Parser interprets the optional strings or character vectors as parameter names. For
example, the checkFinish validation function ensures that printPhoto interprets
'glossy' as a value for finish and not as an invalid parameter name.
By default, the Input Parser makes assumptions about case sensitivity, function names,
structure array inputs, and whether to allow additional parameter names and values that
are not in the scheme. Properties allow you to explicitly define the behavior. Set
properties using dot notation, similar to assigning values to a structure array.
Allow printPhoto to accept additional parameter value inputs that do not match the
input scheme by setting the KeepUnmatched property of the Input Parser.
p.KeepUnmatched = true;
If KeepUnmatched is false (default), the Input Parser issues an error when inputs do
not match the scheme.
21-18
Parse Function Inputs
Within your function, call the parse method. Pass the values of all of the function inputs.
parse(p,filename,varargin{:})
• Results — Structure array with names and values of all inputs in the scheme.
• Unmatched — Structure array with parameter names and values that are passed to
the function, but are not in the scheme (when KeepUnmatched is true).
• UsingDefaults — Cell array with names of optional inputs that are assigned their
default values because they are not passed to the function.
Within the printPhoto function, display the values for some of the inputs:
if ~isempty(fieldnames(p.Unmatched))
disp('Extra inputs:')
disp(p.Unmatched)
end
if ~isempty(p.UsingDefaults)
disp('Using defaults: ')
disp(p.UsingDefaults)
end
• Required inputs first, in the order they are added to the parsing scheme with
addRequired.
• Optional positional inputs in the order they are added to the scheme with
addOptional.
• Positional inputs before parameter name and value pair inputs.
• Parameter names and values in the form Name1,Value1,...,NameN,ValueN.
Pass several combinations of inputs to printPhoto, some valid and some invalid:
21-19
21 Function Arguments
printPhoto('myfile.jpg')
printPhoto(100)
printPhoto('myfile.jpg','satin')
'glossy', 'matte'
The input, 'satin', did not match any of the valid strings.
printPhoto('myfile.jpg','height',10,'width',8)
To pass a value for the nth positional input, either specify values for the previous (n – 1)
inputs or pass the input as a parameter name and value pair. For example, these function
calls assign the same values to finish (default 'glossy') and color:
printPhoto('myfile.gif','glossy','CMYK') % positional
See Also
inputParser | varargin
More About
• “Input Parser Validation Functions” on page 21-21
21-20
Input Parser Validation Functions
Validation functions must accept a single input argument, and they must either return a
scalar logical value (true or false) or error. If the validation function returns false, the
Input Parser issues an error and your function stops processing.
• Use an existing MATLAB function such as ischar or isnumeric. For example, check
that a required input named num is numeric:
p = inputParser;
checknum = @isnumeric;
addRequired(p,'num',checknum)
parse(p,'text')
• Create an anonymous function. For example, check that input num is a numeric scalar
greater than zero:
p = inputParser;
checknum = @(x) isnumeric(x) && isscalar(x) && (x > 0);
addRequired(p,'num',checknum)
parse(p,rand(3))
The value of 'num' is invalid. It must satisfy the function: @(x) isnumeric(x) && isscalar(x)
&& (x>0).
• Define your own function, typically a local function in the same file as your primary
function. For example, in a file named usenum.m, define a local function named
checknum that issues custom error messages when the input num to usenum is not a
numeric scalar greater than zero:
function usenum(num)
p = inputParser;
21-21
21 Function Arguments
addRequired(p,'num',@checknum);
parse(p,num);
function TF = checknum(x)
TF = false;
if ~isscalar(x)
error('Input is not scalar');
elseif ~isnumeric(x)
error('Input is not numeric');
elseif (x <= 0)
error('Input must be > 0');
else
TF = true;
end
usenum(-1)
See Also
inputParser | is* | validateattributes
Related Examples
• “Parse Function Inputs” on page 21-17
• “Create Function Handle” on page 13-2
More About
• “Anonymous Functions” on page 20-24
21-22
22
Before you begin debugging, make sure that your program is saved and that the program
and any files it calls exist on your search path or in the current folder.
• If you run a file with unsaved changes from within the Editor, then the file is
automatically saved before it runs.
• If you run a file with unsaved changes from the Command Window, then MATLAB
software runs the saved version of the file. Therefore, you do not see the results of
your changes.
Set Breakpoint
Set breakpoints to pause the execution of a MATLAB file so you can examine the value or
variables where you think a problem could be. You can set breakpoints using the Editor,
using functions in the Command Window, or both.
There are three different types of breakpoints: standard, conditional, and error. To add a
standard breakpoint in the Editor, click the breakpoint alley at an executable line where
you want to set the breakpoint. The breakpoint alley is the narrow column on the left side
of the Editor, to the right of the line number. You also can use the F12 key to set the
breakpoint.
Executable lines are indicated by a dash ( — ) in the breakpoint alley. For example, click
the breakpoint alley next to line 2 in the code below to add a breakpoint at that line.
22-2
Debug a MATLAB Program
If an executable statement spans multiple lines, you can set a breakpoint at each line in
that statement, even though the additional lines do not have a — (dash) in the breakpoint
alley. For example, in this code. you can set a breakpoint at all four lines:
For more information on the different types of breakpoints, see “Set Breakpoints” on page
22-8.
Run File
After setting breakpoints, run the file from the Command Window or the Editor. Running
the file produces these results:
•
The Run button changes to a Pause button.
• The prompt in the Command Window changes to K>> indicating that MATLAB is in
debug mode and that the keyboard is in control.
• MATLAB pauses at the first breakpoint in the program. In the Editor, a green arrow
just to the right of the breakpoint indicates the pause. The program does not execute
the line where the pause occurs until it resumes running. For example, here the
debugger pauses before the program executes x = ones(1,10);.
• MATLAB displays the current workspace in the Function Call Stack, on the Editor
tab in the Debug section.
If you use debugging functions from the Command Window, use dbstack to view the
Function Call Stack.
22-3
22 Debugging MATLAB Code
For more information on using the Function Call Stack, see “Select Workspace” on page
22-15
Pausing is useful if you want to check on the progress of a long running program to
ensure that it is running as expected.
Note Clicking the pause button can cause MATLAB to pause in a file outside your own
program file. Pressing the Continue button resumes normal execution without
changing the results of the file.
View the value of a variable while debugging to see whether a line of code has produced
the expected result or not. To do this, position your mouse pointer to the left of the
variable. The current value of the variable appears in a data tip.
22-4
Debug a MATLAB Program
The data tip stays in view until you move the pointer. If you have trouble getting the data
tip to appear, click the line containing the variable, and then move the pointer next to the
variable. For more information, see “Examine Values While Debugging” on page 22-15.
You can change the value of a variable while debugging to see if the new value produces
expected results. With the program paused, assign a new value to the variable in the
Command Window, Workspace browser, or Variables Editor. Then, continue running or
stepping through the program.
You can modify a section of code while debugging to test possible fixes without having to
save your changes. Usually, it is a good practice to modify a MATLAB file after you quit
debugging, and then save the modification and run the file. Otherwise, you might get
unexpected results. However, there are situations where you want to experiment during
debugging.
22-5
22 Debugging MATLAB Code
1 While your code is paused, modify a part of the file that has not yet run.
After the code evaluation is complete, stop debugging and save or undo any changes
made before continuing the debugging process.
This table describes available debugging actions and the different methods you can use to
execute them.
22-6
See Also
clicking Quit Debugging . You must end a debugging session if you want to change
and save a file, or if you want to run other programs in MATLAB.
After you quit debugging, pause indicators in the Editor display no longer appear, and the
normal >> prompt reappears in the Command Window in place of the K>>. You no longer
can access the call stack.
See Also
Related Examples
• “Set Breakpoints” on page 22-8
• “Examine Values While Debugging” on page 22-15
22-7
22 Debugging MATLAB Code
Set Breakpoints
In this section...
“Standard Breakpoints” on page 22-9
“Conditional Breakpoints” on page 22-10
“Error Breakpoints” on page 22-11
“Breakpoints in Anonymous Functions” on page 22-11
“Invalid Breakpoints” on page 22-12
“Disable Breakpoints” on page 22-13
“Clear Breakpoints” on page 22-13
Setting breakpoints pauses the execution of your MATLAB program so that you can
examine values where you think a problem might be. You can set breakpoints using the
Editor or by using functions in the Command Window.
• Standard breakpoints
• Conditional breakpoints
• Error breakpoints
You can set breakpoints only at executable lines in saved files that are in the current
folder or in folders on the search path. You can set breakpoints at any time, whether
MATLAB is idle or busy running a file.
1
From the Home tab, in the Environment section, click Preferences.
22-8
Set Breakpoints
Standard Breakpoints
A standard breakpoint pauses at a specified line in a file.
To set a standard breakpoint click the breakpoint alley at an executable line where you
want to set the breakpoint. The breakpoint alley is the narrow column on the left side of
the Editor, to the right of the line number. You also can use the F12 key to set the
breakpoint.
If you attempt to set a breakpoint at a line that is not executable, such as a comment or a
blank line, MATLAB sets it at the next executable line.
To set a standard breakpoint programmatically, use the dbstop function. For example, to
add a breakpoint at line 2 in a file named myprogram.m, type:
dbstop in myprogram at 2
22-9
22 Debugging MATLAB Code
To examine values at increments in a for loop, set the breakpoint within the loop, rather
than at the start of the loop. If you set the breakpoint at the start of the for loop, and then
step through the file, MATLAB pauses at the for statement only once. However, if you
place the breakpoint within the loop, MATLAB pauses at each pass through the loop.
Conditional Breakpoints
A conditional breakpoint causes MATLAB to pause at a specified line in a file only when
the specified condition is met. Use conditional breakpoints when you want to examine
results after some iterations in a loop.
When the Editor dialog box opens, enter a condition and click OK. A condition is any valid
MATLAB expression that returns a logical scalar value.
As noted in the dialog box, MATLAB evaluates the condition before running the line. For
example, suppose that you have a file called myprogram.m.
A yellow, conditional breakpoint icon appears in the breakpoint alley at that line.
22-10
Set Breakpoints
You also can set a conditional breakpoint programmatically using the dbstop function.
For example, to add a conditional breakpoint in myprogram.m at line 6 type:
When you run the file, MATLAB enters debug mode and pauses at the line when the
condition is met. In the myprogram example, MATLAB runs through the for loop twice
and pauses on the third iteration at line 6 when n is 4. If you continue executing, MATLAB
pauses again at line 6 on the fourth iteration when n is 5.
Error Breakpoints
An error breakpoint causes MATLAB to pause program execution and enter debug mode
if MATLAB encounters a problem. Unlike standard and conditional breakpoints, you do
not set these breakpoints at a specific line in a specific file. When you set an error
breakpoint, MATLAB pauses at any line in any file if the error condition specified occurs.
MATLAB then enters debug mode and opens the file containing the error, with the
execution arrow at the line containing the error.
To set an error breakpoint, on the Editor tab, click Run and select from these
options:
You also can set a breakpoint programmatically by using the dbstop function with a
specified condition. For example, to pause execution on all errors, type
dbstop if error
To pause execution at the first run-time error within the try portion of a try/catch
block that has a message ID of MATLAB:ls:InputsMustBeStrings, type
22-11
22 Debugging MATLAB Code
software pauses at the start of the line. Or, alternatively, you can set a breakpoint for each
anonymous function in the line.
When you add a breakpoint to a line containing an anonymous function, the Editor asks
where in the line you want to add the breakpoint. If there is more than one breakpoint in
a line, the breakpoint icon is blue, regardless of the status of any of the breakpoints on
that line.
To view information about all the breakpoints on a line, hover your pointer on the
breakpoint icon. A tooltip appears with available information. For example, in this code,
line 5 contains two anonymous functions, with a breakpoint at each one. The tooltip tells
us that both breakpoints are enabled.
When you set a breakpoint in an anonymous function, MATLAB pauses when the
anonymous function is called. A green arrow shows where the code defines the
anonymous function. A white arrow shows where the code calls the anonymous functions.
For example, in this code, MATLAB pauses the program at a breakpoint set for the
anonymous function sqr, at line 2 in a file called myanonymous.m. The white arrow
indicates that the sqr function is called from line 3.
Invalid Breakpoints
A gray breakpoint indicates an invalid breakpoint.
22-12
Set Breakpoints
• There are unsaved changes in the file. To make breakpoints valid, save the file. The
gray breakpoints become red, indicating that they are now valid.
• There is a syntax error in the file. When you set a breakpoint, an error message
appears indicating where the syntax error is. To make the breakpoint valid, fix the
syntax error and save the file.
Disable Breakpoints
You can disable selected breakpoints so that your program temporarily ignores them and
runs uninterrupted. For example, you might disable a breakpoint after you think you
identified and corrected a problem, or if you are using conditional breakpoints.
To disable a breakpoint, right-click the breakpoint icon, and select Disable Breakpoint
from the context menu.
To reenable a breakpoint, right-click the breakpoint icon and select Enable Breakpoint
from the context menu.
The X no longer appears on the breakpoint icon and program execution pauses at that
line.
To enable or disable all breakpoints in the file, select Enable All in File or Disable All
in File. These options are only available if there is at least one breakpoint to enable or
disable.
Clear Breakpoints
All breakpoints remain in a file until you clear (remove) them or until they are cleared
automatically at the end of your MATLAB session.
To clear a breakpoint, right-click the breakpoint icon and select Clear Breakpoint from
the context menu. You also can use the F12 key to clear the breakpoint.
To clear a breakpoint programmatically, use the dbclear function. For example, to clear
the breakpoint at line 6 in a file called myprogram.m, type
22-13
22 Debugging MATLAB Code
dbclear in myprogram at 6
To clear all breakpoints in the file, right-click the breakpoint alley and select Clear All in
File. You can also use the dbclear all command. For example, to clear all the
breakpoints in a file called myprogram.m, type
To clear all breakpoints in all files, including error breakpoints, right-click the breakpoint
alley and select Clear All. You also can use the dbclear all command.
Breakpoints clear automatically when you end a MATLAB session. To save your
breakpoints for future sessions, see the dbstatus function.
See Also
Related Examples
• “Debug a MATLAB Program” on page 22-2
• “Examine Values While Debugging” on page 22-15
22-14
Examine Values While Debugging
Select Workspace
To examine a variable during debugging, you must first select its workspace. Variables
that you assign through the Command Window or create using scripts belong to the base
workspace. Variables that you create in a function belong to their own function
workspace. To view the current workspace, select the Editor tab. The Function Call
Stack field shows the current workspace. Alternatively, you can use the dbstack
function in the Command Window.
To select or change the workspace for the variable you want to view, use either of these
methods:
• From the Editor tab, in the Debug section, choose a workspace from the Function
Call Stack menu list.
• From the Command Window, use the dbup and dbdown functions to select the
previous or next workspace in the Function Call Stack.
The Workspace browser displays all variables in the current workspace. The Value
column of the Workspace browser shows the current value of the variable. To see more
22-15
22 Debugging MATLAB Code
details, double-click the variable. The Variables Editor opens, displaying the content
for that variable. You also can use the openvar function to open a variable in the
Variables Editor.
Use your mouse to select the variable or equation. Right-click and select Evaluate
Selection from the context menu. The Command Window displays the value of the
variable or equation.
Note You cannot evaluate a selection while MATLAB is busy, for example, running a
file.
• View variable values as a data tip in the MATLAB Editor.
22-16
Examine Values While Debugging
To do this, position your mouse pointer over the variable. The current value of the
variable appears in a data tip. The data tip stays in view until you move the pointer. If
you have trouble getting the data tip to appear, click the line containing the variable,
and then move the pointer next to the variable.
Data tips are always enabled when debugging a file in the Editor. To view data tips
when editing a file in the Editor, enable them in your MATLAB preferences.
1
On the Home tab, in the Environment section, click Preferences. Then
select MATLAB > Editor/Debugger > Display.
2 Under General display options, select Enable datatips in edit mode.
• View variable values in the Command Window.
To see all the variables currently in the workspace, call the who function. To view the
current value of a variable, type the variable name in the Command Window. For the
example, to see the value of a variable n, type n and press Enter. The Command
Window displays the variable name and its value.
When you set a breakpoint in a function and attempt to view the value of a variable in a
parent workspace, the value of that variable might not be available. This error occurs
when you attempt to access a variable while MATLAB is in the process of overwriting it.
In such cases, MATLAB returns the following message, where x represents the variable
whose value you are trying to examine.
K>> x
Reference to a called function result under construction x.
The error occurs whether you select the parent workspace by using the dbup command
or by using Function Call Stack field in the Debug section of the Editor tab.
22-17
22 Debugging MATLAB Code
See Also
Related Examples
• “Debug a MATLAB Program” on page 22-2
• “Set Breakpoints” on page 22-8
22-18
23
MATLAB software enables you to present your MATLAB code in various ways. You can
share your code and results with others, even if they do not have MATLAB software. You
can save MATLAB output in various formats, including HTML, XML, and LaTeX. If
Microsoft Word or Microsoft PowerPoint applications are on your Microsoft Windows
system, you can publish to their formats as well.
23-2
Publish and Share MATLAB Code
This code demonstrates the Fourier series expansion for a square wave.
23-3
23 Presenting MATLAB Code
1 Create a MATLAB script or function. Divide the code into steps or sections by
inserting two percent signs (%%) at the beginning of each section.
2 Document the code by adding explanatory comments at the beginning of the file and
within each section.
Within the comments at the top of each section, you can add markup that enhances
the readability of the output. For example, the code in the preceding table includes
the following markup.
23-4
See Also
Note When you have a file containing text that has characters in a different encoding
than that of your platform, when you save or publish your file, MATLAB displays
those characters as garbled text.
3 Publish the code. On the Publish tab, click Publish.
By default, MATLAB creates a subfolder named html, which contains an HTML file
and files for each graphic that your code creates. The HTML file includes the code,
formatted comments, and output. Alternatively, you can publish to other formats,
such as PDF files or Microsoft PowerPoint presentations. For more information on
publishing to other formats, see “Specify Output File” on page 23-28.
In MATLAB Online, MATLAB publishes to either HTML or PDF, and stores the output
and supporting files in your Published folder. To allow MATLAB to open the output
automatically, enable pop-up windows in your Web browser. Results are available
publicly using a URL of the form https://matlab.mathworks.com/users/
userid/Published/filename/index.html.
The sample code that appears in the previous figure is part of the installed
documentation. You can view the code in the Editor by running this command:
edit(fullfile(matlabroot,'help','techdoc','matlab_env', ...
'examples','fourier_demo2.m'))
You also can create your own MATLAB documentation topics for viewing from the
MATLAB Help browser or the web. For more information, see “Display Custom
Documentation” on page 31-27
See Also
publish
23-5
23 Presenting MATLAB Code
More About
• “Create Live Scripts in the Live Editor” on page 19-7
• “Publishing Markup” on page 23-7
• “Output Preferences for Publishing” on page 23-27
External Websites
• Publishing MATLAB Code from the Editor video
23-6
Publishing Markup
Publishing Markup
In this section...
“Markup Overview” on page 23-7
“Sections and Section Titles” on page 23-10
“Text Formatting” on page 23-11
“Bulleted and Numbered Lists” on page 23-12
“Text and Code Blocks” on page 23-13
“External File Content” on page 23-14
“External Graphics” on page 23-15
“Image Snapshot” on page 23-17
“LaTeX Equations” on page 23-18
“Hyperlinks” on page 23-20
“HTML Markup” on page 23-23
“LaTeX Markup” on page 23-24
Markup Overview
To insert markup, you can:
• Use the formatting buttons and drop-down menus on the Publish tab to format the
file. This method automatically inserts the text markup for you.
• Select markup from the Insert Text Markup list in the right click menu.
• Type the markup directly in the comments.
The following table provides a summary of the text markup options. Refer to this table if
you are not using the MATLAB Editor, or if you do not want to use the Publish tab to
apply the markup.
• Spaces following the comment symbols (%) often determine the format of the text that
follows.
23-7
23 Presenting MATLAB Code
• Starting new markup often requires preceding blank comment lines, as shown in
examples.
• Markup only works in comments that immediately follow a section break.
% *BOLD TEXT*
% |MONOSPACED TEXT|
% Trademarks:
% TEXT(TM)
% TEXT(R)
“Bulleted and Numbered Lists” on %% Bulleted List
page 23-12 %
% * BULLETED ITEM 1
% * BULLETED ITEM 2
%
%% Numbered List
%
% # NUMBERED ITEM 1
% # NUMBERED ITEM 2
%
23-8
Publishing Markup
%% MATLAB(R) Code
%
% for i = 1:10
% disp x
% end
%
“External File Content” on page %
23-14 % <include>filename.m</include>
%
“External Graphics” on page 23- %
15 % <<FILENAME.PNG>>
%
“Image Snapshot” on page 23-17 snapnow;
“LaTeX Equations” on page 23-18 %% Inline Expression
% $x^2+e^{\pi i}$
%% Block Equation
%
% $$e^{\pi i} + 1 = 0$$
%
“Hyperlinks” on page 23-20 % <https://www.mathworks.com MathWorks>
% <matlab:FUNCTION DISPLAYED_TEXT>
“HTML Markup” on page 23-23 %
% <html>
% <table border=1><tr>
% <td>one</td>
% <td>two</td></tr></table>
% </html>
%
23-9
23 Presenting MATLAB Code
Note You can add comments in the lines immediately following the title. However, if you
want an overall document title, you cannot add any MATLAB code before the start of the
next section (a line starting with %%).
By saving the code in an Editor and clicking the Publish button on the Publish tab,
MATLAB produces the output as shown in this figure. Notice that MATLAB automatically
inserts a Contents menu from the section titles in the MATLAB file.
23-10
Publishing Markup
Text Formatting
You can mark selected text in the MATLAB comments so that they display in italic, bold,
or monospaced text when you publish the file. Simply surround the text with _, *, or | for
italic, bold, or monospaced text, respectively.
For instance, these lines display each of the text formatting syntaxes if published.
23-11
23 Presenting MATLAB Code
Trademark Symbols
If the comments in your MATLAB file include trademarked terms, you can include text to
produce a trademark symbol (™) or registered trademark symbol (®) in the output.
Simply add (R) or (TM) directly after the term in question, without any space in
between.
If you publish the file to HTML, it appears in the MATLAB web browser.
%% Two Lists
%
% * ITEM1
% * ITEM2
%
% # ITEM1
% # ITEM2
%
23-12
Publishing Markup
Preformatted text appears in monospace font, maintains white space, and does not wrap
long lines. Two spaces must appear between the comment symbol and the text of the first
line of the preformatted text.
%%
% Many people find monospaced texts easier to read:
%
% A dot product of two vectors yields a scalar.
% MATLAB has a simple command for dot products.
Executable code appears with syntax highlighting in published documents. You also can
highlight sample code. Sample code is code that appears within comments.
23-13
23 Presenting MATLAB Code
To indicate sample code, you must put three spaces between the comment symbol and the
start of the first line of code. For example, clicking the Code button on the Publish tab
inserts the following sample code in your Editor.
%%
%
% for i = 1:10
% disp(x)
% end
%
Publishing this code to HTML produces output in the MATLAB web browser.
For example, this code inserts the contents of sine_wave.m into your published output:
23-14
Publishing Markup
External Graphics
To publish an image that the MATLAB code does not generate, use text markup. By
default, MATLAB already includes code-generated graphics.
This code inserts a generic image called FILENAME.PNG into your published output.
%%
%
% <<FILENAME.PNG>>
%
MATLAB requires that FILENAME.PNG be a relative path from the output location to your
external image or a fully qualified URL. Good practice is to save your image in the same
folder that MATLAB publishes its output. For example, MATLAB publishes HTML
documents to a subfolder html. Save your image file in the same subfolder. You can
change the output folder by changing the publish configuration settings. In MATLAB
Online, save your image file to your Published folder, which is located in your root
folder.
This example shows how to insert surfpeaks.jpg into a MATLAB file for publishing.
23-15
23 Presenting MATLAB Code
saveas(surf(peaks),'html/surfpeaks.jpg');
3 Publish this MATLAB code to HTML.
%% Image Example
% This is a graphic:
%
% <<surfpeaks.jpg>>
%
The type of images you can include when you publish depends on the output type of that
document as indicated in this table. For greatest compatibility, best practice is to use the
default image format for each output type.
23-16
Publishing Markup
Output File Format Default Image Types of Images You Can Include
Format
doc png Any format that your installed version of
Microsoft Office supports.
html png All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
latex png or epsc2 All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
pdf bmp bmp and jpg.
ppt png Any format that your installed version of
Microsoft Office supports.
xml png All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
Image Snapshot
You can insert code that captures a snapshot of your MATLAB output. This is useful, for
example, if you have a for loop that modifies a figure that you want to capture after each
iteration.
The following code runs a for loop three times and produces output after every iteration.
The snapnow command captures all three images produced by the code.
for i=1:3
imagesc(magic(i))
snapnow;
end
If you publish the file to HTML, it resembles the following output. By default, the images
in the HTML are larger than shown in the figure. To resize images generated by MATLAB
23-17
23 Presenting MATLAB Code
code, use the Max image width and Max image height fields in the Publish settings
pane, as described in “Output Preferences for Publishing” on page 23-27.
LaTeX Equations
Inline LaTeX Expression
MATLAB enables you to include an inline LaTeX expression in any code that you intend to
publish. To insert an inline expression, surround your LaTeX markup with dollar sign
characters ($). The $ must immediately precede the first word of the inline expression,
and immediately follow the last word of the inline expression, without any space in
between.
Note
• All publishing output types support LaTeX expressions, except Microsoft PowerPoint.
• MATLAB publishing supports standard LaTeX math mode directives. Text mode
directives or directives that require additional packages are not supported.
23-18
Publishing Markup
If you publish the sample text markup to HTML, this is the resulting output.
MATLAB enables you to insert LaTeX symbols in blocks that are offset from the main
comment text. Two dollar sign characters ($$) on each side of an equation denote a block
LaTeX equation. Publishing equations in separate blocks requires a blank line in between
blocks.
23-19
23 Presenting MATLAB Code
Hyperlinks
Static Hyperlinks
You can insert static hyperlinks within a MATLAB comment, and then publish the file to
HTML, XML, or Microsoft Word. When specifying a static hyperlink to a web location,
include a complete URL within the code. This is useful when you want to point the reader
to a web location. You can display or hide the URL in the published text. Consider
excluding the URL, when you are confident that readers are viewing your output online
and can click the hyperlink.
Eliminating the text MathWorks after the URL produces this modified output.
23-20
Publishing Markup
Note If your code produces hyperlinked text in the MATLAB Command Window, the
output shows the HTML code rather than the hyperlink.
Dynamic Hyperlinks
You can insert dynamic hyperlinks, which MATLAB evaluates at the time a reader clicks
that link. Dynamic hyperlinks enable you to point the reader to MATLAB code or
documentation, or enable the reader to run code. You implement these links using
matlab: syntax. If the code that follows the matlab: declaration has spaces in it,
replace them with %20.
Note Dynamic links only work when viewing HTML in the MATLAB web browser.
You can specify a dynamic hyperlink to run code when a user clicks the hyperlink. For
example, this matlab: syntax creates hyperlinks in the output, which when clicked either
enable or disable recycling:
%% Recycling Preference
% Click the preference you want:
%
% <matlab:recycle('off') Disable recycling>
%
% <matlab:recycle('on') Enable recycling>
23-21
23 Presenting MATLAB Code
When you click one of the hyperlinks, MATLAB sets the recycle command accordingly.
After clicking a hyperlink, run recycle in the Command Window to confirm that the
setting is as you expect.
Dynamic Link to a File
You can specify a link to a file that you know is in the matlabroot of your reader. You do
not need to know where each reader installed MATLAB. For example, link to the function
code for publish.
%%
% See the
% <matlab:edit(fullfile(matlabroot,'toolbox','matlab','codetools','publish.m')) code>
% for the publish function.
When you click the code link, the MATLAB Editor opens and displays the code for the
publish function. On the reader's system, MATLAB issues the command (although the
command does not appear in the reader's Command Window).
Dynamic Link to a MATLAB Function Reference Page
You can specify a link to a MATLAB function reference page using matlab: syntax. For
example, suppose that your reader has MATLAB installed and running. Provide a link to
the publish reference page.
23-22
Publishing Markup
%%
% See the help for the <matlab:doc('publish') publish> function.
When you click the publish hyperlink, the MATLAB Help browser opens and displays the
reference page for the publish function. On the reader's system, MATLAB issues the
command, although the command does not appear in the Command Window.
HTML Markup
You can insert HTML markup into your MATLAB file. You must type the HTML markup
since no button on the Publish tab generates it.
Note When you insert text markup for HTML code, the HTML code publishes only when
the specified output file format is HTML.
If you publish the code to HTML, MATLAB creates a single-row table with two columns.
The table contains the values one, two, three, and four.
23-23
23 Presenting MATLAB Code
If a section produces command-window output that starts with <html> and ends with </
html>, MATLAB includes the source HTML in the published output. For example,
MATLAB displays the disp command and makes a table from the HTML code if you
publish this code:
disp('<html><table><tr><td>1</td><td>2</td></tr></table></html>')
LaTeX Markup
You can insert LaTeX markup into your MATLAB file. You must type all LaTeX markup
since no button on the Publish tab generates it.
Note When you insert text markup for LaTeX code, that code publishes only when the
specified output file format is LaTeX.
23-24
Publishing Markup
If you publish the file to LaTeX, then the Editor opens a new .tex file containing the
LaTeX markup.
% This LaTeX was auto-generated from MATLAB code.
% To make changes, update the MATLAB code and republish this document.
\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}
\begin{document}
\begin{par}
This is a table:
\end{par} \vspace{1em}
\begin{par}
\begin{tabular}{|c|c|} \hline
$n$ & $n!$ \\ \hline
1 & 1 \\
2 & 2 \\
3 & 6 \\ \hline
\end{tabular}
23-25
23 Presenting MATLAB Code
\end{par} \vspace{1em}
\end{document}
MATLAB includes any additional markup necessary to compile this file with a LaTeX
program.
See Also
More About
• “Publish and Share MATLAB Code” on page 23-2
• “Output Preferences for Publishing” on page 23-27
23-26
Output Preferences for Publishing
1 Locate the Publish tab and click the Publish button arrow .
23-27
23 Presenting MATLAB Code
The MATLAB expression pane specifies the code that executes during publishing. The
Publish settings pane contains output, figure, and code execution options. Together,
they make what MATLAB refers to as a publish configuration. MATLAB associates each
publish configuration with an .m file. The name of the publish configuration appears in
the top left pane.
Format Notes
html Publishes to an HTML document. You can use an Extensible
Stylesheet Language (XSL) file.
xml Publishes to XML document. You can use an Extensible Stylesheet
Language (XSL) file.
latex Publishes to LaTeX document. Does not preserve syntax highlighting.
You can use an Extensible Stylesheet Language (XSL) file.
23-28
Output Preferences for Publishing
Format Notes
doc Publishes to a Microsoft Word document. Does not preserve syntax
highlighting. This format is only available on Windows platforms.
ppt Publishes to a Microsoft PowerPoint document. Does not preserve
syntax highlighting. This format is only available on Windows
platforms.
pdf Publishes to a PDF document.
Note XSL files allow you more control over the appearance of the output document. For
more details, see http://docbook.sourceforge.net/release/xsl/current/doc/.
Specifying Code
By default, MATLAB executes the .m file that you are publishing. However, you can
specify any valid MATLAB code in the MATLAB expression pane. For example, if you
want to publish a function that requires input, then run the command
function(input). Additional code, whose output you want to publish, appears after the
functions call. If you clear the MATLAB expression area, then MATLAB publishes the file
without evaluating any code.
Note Publish configurations use the base MATLAB workspace. Therefore, a variable in
the MATLAB expression pane overwrites the value for an existing variable in the base
workspace.
23-29
23 Presenting MATLAB Code
Evaluating Code
Another way to affect what MATLAB executes during publishing is to set the Evaluate
code option in the Publish setting pane. This option indicates whether MATLAB
evaluates the code in the .m file that is publishing. If set to true, MATLAB executes the
code and includes the results in the output document.
Because MATLAB does not evaluate the code nor include code results when you set the
Evaluate code option to false, there can be invalid code in the file. Therefore, consider
first running the file with this option set to true.
For example, suppose that you include comment text, Label the plot, in a file, but
forget to preface it with the comment character. If you publish the document to HTML,
and set the Evaluate code option to true, the output includes an error.
Use the false option to publish the file that contains the publish function. Otherwise,
MATLAB attempts to publish the file recursively.
Including Code
You can specify whether to display MATLAB code in the final output. If you set the
Include code option to true, then MATLAB includes the code in the published output
document. If set to false, MATLAB excludes the code from all output file formats, except
HTML.
If the output file format is HTML, MATLAB inserts the code as an HTML comment that is
not visible in the web browser. If you want to extract the code from the output HTML file,
use the MATLAB grabcode function.
23-30
Output Preferences for Publishing
grabcode('sine_wave.html')
Catching Errors
You can catch and publish any errors that occur during publishing. Setting the Catch
error option to true includes any error messages in the output document. If you set
Catch error to false, MATLAB terminates the publish operation if an error occurs
during code evaluation. However, this option has no effect if you set the Evaluate code
property to false.
You can limit the number of lines of code output that is included in the output document
by specifying the Max # of output lines option in the Publish settings pane. Setting
this option is useful if a smaller, representative sample of the code output suffices.
For example, the following loop generates 100 lines in a published output unless Max #
of output lines is set to a lower value.
for n = 1:100
disp(x)
end;
23-31
23 Presenting MATLAB Code
When publishing, you can choose the image format that MATLAB uses to store any
graphics generated during code execution. The available image formats in the drop-down
list depend on the setting of the Figure capture method option. For greatest
compatibility, select the default as specified in this table.
Output File Format Default Image Types of Images You Can Include
Format
doc png Any format that your installed version of
Microsoft Office supports.
html png All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
latex png or epsc2 All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
pdf bmp bmp and jpg.
ppt png Any format that your installed version of
Microsoft Office supports.
xml png All formats publish successfully. Ensure
that the tools you use to view and process
the output files can display the output
format you specify.
You set the size of MATLAB generated images in the Publish settings pane on the Edit
Configurations dialog window. You specify the image size in pixels to restrict the width
and height of images in the output. The pixel values act as a maximum size value because
MATLAB maintains an image’s aspect ratio. MATLAB ignores the size setting for the
following cases:
• When working with external graphics as described in “External Graphics” on page 23-
15
• When using vector formats, such as .eps
23-32
Output Preferences for Publishing
Capturing Figures
You can capture different aspects of the Figure window by setting the Figure capture
method option. This option determines the window decorations (title bar, toolbar, menu
bar, and window border) and plot backgrounds for the Figure window.
This table summarizes the effects of the various Figure capture methods.
Use This Figure Capture To Get Figure Captures with These Appearance Details
Method
Window Decorations Plot Backgrounds
entireGUIWindow Included for dialog boxes; Excluded Set to white for figures; matches
for figures the screen for dialog boxes
print Excluded for dialog boxes and Set to white
figures
getframe Excluded for dialog boxes and Matches the screen plot
figures background
entireFigureWindow Included for dialog boxes and Matches the screen plot
figures background
Note Typically, MATLAB figures have the HandleVisibility property set to on. Dialog
boxes are figures with the HandleVisibility property set to off or callback. If your
results are different from the results listed in the preceding table, the
HandleVisibility property of your figures or dialog boxes might be atypical. For more
information, see HandleVisibility.
MATLAB allows you to specify custom appearance for figures it creates. If the Use new
figure option in the Publish settings pane is set to true, then in the published output,
MATLAB uses a Figure window at the default size and with a white background. If the
Use new figure option is set to false, then MATLAB uses the properties from an open
Figure window to determine the appearance of code-generated figures. This preference
does not apply to figures included using the syntax in “External Graphics” on page 23-15.
Use the following code as a template to produce Figure windows that meet your needs.
23-33
23 Presenting MATLAB Code
% Create figure
figure1 = figure('Name','purple_background',...
'Color',[0.4784 0.06275 0.8941]);
colormap('hsv');
% Create subplot
subplot(1,1,1,'Parent',figure1);
box('on');
% Create title
title({'Title'});
23-34
Output Preferences for Publishing
By publishing your file with this window open and the Use new figure option set to
false, any code-generated figure takes the properties of the open Figure window.
Note You must set the Figure capture method option to entireFigureWindow for the
final published figure to display all the properties of the open Figure window.
23-35
23 Presenting MATLAB Code
Creating a Thumbnail
You can save the first code-generated graphic as a thumbnail image. You can use this
thumbnail to represent your file on HTML pages. To create a thumbnail, follow these
steps:
1 On the Publish tab, click the Publish button drop-down arrow and select Edit
Publishing Options. The Edit Configurations dialog box opens.
2 Set the Image Format option to a bitmap format, such as .png or .jpg. MATLAB
creates thumbnail images in bitmap formats.
3 Set the Create thumbnail option to true.
MATLAB saves the thumbnail image in the folder specified by the Output folder
option in the Publish settings pane.
23-36
Output Preferences for Publishing
When the Publish settings options are set, you can follow these steps to save the
settings:
1 Click Save As when the options are set in the manner you want.
The Save Publish Settings As dialog box opens and displays the names of all the
currently defined publish settings. By default the following publish settings install
with MATLAB:
• Factory Default
You cannot overwrite the Factory Default and can restore them by selecting
Factory Default from the Publish settings list.
• User Default
Initially, User Default settings are identical to the Factory Default settings.
You can overwrite the User Default settings.
23-37
23 Presenting MATLAB Code
2 In the Settings Name field, enter a meaningful name for the settings. Then click
Save.
You can now use the publish settings with other MATLAB files.
You also can overwrite the publishing properties saved under an existing name.
Select the name from the Publish settings list, and then click Overwrite.
Together, the code in the MATLAB expression pane and the settings in the Publish
settings pane make a publish configuration that is associated with one file. These
configurations provide a simple way to refer to publish preferences for individual files.
To create a publish configuration, click the Publish button drop-down arrow on the
Publish tab, and select Edit Publishing Options. The Edit Configurations dialog box
opens, containing the default publish preferences. In the Publish configuration name
field, type a name for the publish configuration, or accept the default name. The publish
configuration saves automatically.
After saving a publish configuration, you can run it without opening the Edit
Configurations dialog box:
1 Click the Publish button drop-down arrow If you position your mouse pointer on a
publish configuration name, MATLAB displays a tooltip showing the MATLAB
expression associated with the specific configuration.
2 Select a configuration name to use for the publish configuration. MATLAB publishes
the file using the code and publish settings associated with the configuration.
You can create multiple publish configurations for a given file. You might do this to
publish the file with different values for input arguments, with different publish setting
23-38
Output Preferences for Publishing
property values, or both. Create a named configuration for each purpose, all associated
with the same file. Later you can run whichever particular publish configuration you
want.
A new name appears on the configurations list, filename_n, where the value of n
depends on the existing configuration names.
Each publish configuration is associated with a specific file. If you move or rename a file,
redefine its association. If you delete a file, consider deleting the associated
configurations, or associating them with a different file.
23-39
23 Presenting MATLAB Code
When MATLAB cannot associate a configuration with a file, the Edit Configurations dialog
box displays the file name in red and a File Not Found message. To reassociate a
configuration with another file, perform the following steps.
1
Click the Clear search button on the left pane of the Edit Configurations dialog
box.
2 Select the file for which you want to reassociate publish configurations.
3 In the right pane of the Edit Configurations dialog box, click Choose.... In the Open
dialog box, navigate to and select the file with which you want to reassociate the
configurations.
You can rename the configurations at any time by selecting a configuration from the list in
the left pane. In the right pane, edit the value for the Publish configuration name.
Note To run correctly after a file name change, you might need to change the code
statements in the MATLAB expression pane. For example, change a function call to
reflect the new file name for that function.
Each time you create or save a publish configuration using the Edit Configurations dialog
box, the Editor updates the publish_configurations.m file in your preferences folder.
(This is the folder that MATLAB returns when you run the MATLAB prefdir function.)
Although you can port this file from the preferences folder on one system to another, only
one publish_configurations.m file can exist on a system. Therefore, only move the
file to another system if you have not created any publish configurations on the second
system. In addition, because the publish_configurations.m file might contain
references to file paths, be sure that the specified files and paths exist on the second
system.
23-40
See Also
See Also
More About
• “Publish and Share MATLAB Code” on page 23-2
• “Publishing Markup” on page 23-7
23-41
24
24-2
Open and Save Files in the Editor
edit collatz.m
For special considerations on the Macintosh platform, see “Navigating Within the
MATLAB Root Folder on macOS Platforms”.
Some options for opening files are not available in MATLAB Online.
Save Files
24-3
24 Coding and Productivity Tips
After you modify a file in the Editor, an asterisk (*) follows the file name. This asterisk
indicates that there are unsaved changes to the file.
You can perform four different types of save operations, which have various effects, as
described in this table.
Some options for saving files are not available in MATLAB Online.
MathWorks recommends that you save files you create and files from MathWorks that you
edit to a folder that is not in the matlabroot folder tree, where matlabroot is the
folder returned when you type matlabroot in the Command Window. If you keep your
files in matlabroot folders, they can be overwritten when you install a new version of
MATLAB software.
At the beginning of each MATLAB session, MATLAB loads and caches in memory the
locations of files in the matlabroot folder tree. Therefore, if you:
24-4
Open and Save Files in the Editor
• Save files to matlabroot folders using an external editor, run rehash toolbox
before you use the files in the current session.
• Add or remove files from matlabroot folders using file system operations, run
rehash toolbox before you use the files in the current session.
• Modify existing files in matlabroot folders using an external editor, run clear
function-name before you use these files in the current session.
Backing Up Files
When you modify a file in the Editor, the Editor saves a copy of the file using the same file
name but with an .asv extension every 5 minutes. The backup version is useful if you
have system problems and lose changes you made to your file. In that event, you can open
the backup version, filename.asv, and then save it as filename.m to use the last good
version of filename.
Note The Live Editor and MATLAB Online do not automatically save copies of files.
To select preferences, click Preferences, and then select MATLAB > Editor/
Debugger > Backup Files on the Home tab, in the Environment section. You can then:
By default, MATLAB automatically deletes backup files when you close the Editor. It is
best to keep backup-to-file relationships clear and current. Therefore, when you
rename or remove a file, consider deleting or renaming the corresponding backup file.
• Specify the number of minutes between backup saves.
• Specify the file extension for backup files.
• Specify a location for backup files
If you edit a file in a read-only folder and the back up Location preference is Source
file directories, then the Editor does not create a backup copy of the file.
24-5
24 Coding and Productivity Tips
1
On the Home tab, in the Environment section, click Preferences.
2 Select MATLAB > Code Analyzer, and then select the Enable integrated warning
and error messages check box.
3 Set the Underlining option to Underline warnings and errors, and then click
OK.
Note Changing the continuous code checking preferences is not supported in MATLAB
Online. Continuous code checking is always enabled.
You can use continuous code checking in MATLAB code files in the Editor:
1 Open a MATLAB code file in the Editor. This example uses the sample file
lengthofline.m that ships with the MATLAB software:
open(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'))
b Save the example file to a folder to which you have write access. For the
example, lengthofline.m is saved to C:\my_MATLAB_files.
24-6
Check Code for Errors and Warnings
2 Examine the message indicator at the top of the message bar to see the Code
Analyzer messages reported for the file:
• Red indicates that syntax errors or other significant issues were detected.
• Orange indicates warnings or opportunities for improvement, but no errors, were
detected.
• Green indicates no errors, warnings, or opportunities for improvement were
detected.
In this example, the indicator is red, meaning that there is at least one error in the
file.
3 Click the message indicator to go to the next code fragment containing a message.
The next code fragment is relative to the current cursor position, viewable in the
status bar.
In the lengthofline example, the first message is at line 21. The cursor moves to
the beginning of line 21.
The code fragment for which there is a message is underlined in either red for errors
or orange for warnings and improvement opportunities.
4 View the message by moving the mouse pointer within the underlined code fragment.
24-7
24 Coding and Productivity Tips
The message opens in a tooltip and contains a Details button that provides access to
additional information by extending the message. Not all messages have additional
information.
The message indicator and underlining automatically update to reflect changes you
make, even if you do not save the file.
7 On line 27, hover over prod.
• If you know what the fix is (from previous experience), click Fix.
• If you are unfamiliar with the fix, view, and then apply it as follows:
a Right-click the highlighted code (for a single-button mouse, press Ctrl+
click), and then view the first item in the context menu.
24-8
Check Code for Errors and Warnings
• To go to the next message, click the message indicator or the next underlined
code fragment.
• To go to a line that a marker represents, click a red or orange line in the indicator
bar.
To see the first error in lengthofline, click the first red marker in the message
bar. The cursor moves to the first suspect code fragment in line 47. The Details
and Fix buttons are dimmed (or not visible if in MATLAB Online), indicating that
there is no more information about this message and there is no automatic fix.
On line 47, the message suggests a delimiter imbalance. To investigate this message,
in the Editor, move the arrow key over each of the delimiters to see if MATLAB
indicates a mismatch. For instructions on how to enable delimiter matching on arrow,
see “Set Keyboard Preferences”.
24-9
24 Coding and Productivity Tips
It might appear that there are no mismatched delimiters. However, code analysis
detects the semicolon in parentheses: data{3}(;), and interprets it as the end of a
statement. The message reports that the two statements on line 47 each have a
delimiter imbalance.
To fix the problem, in line 47, change data{3}(;) to data{3}(:). Now, the
underline no longer appears in line 47. The single change addresses the issues in
both of the messages for line 47. Because the change removed the only error in the
file, the message indicator at the top of the bar changes from red to orange,
indicating that only warnings and potential improvements remain.
After modifying the code to address all the messages, or disabling designated messages,
the message indicator becomes green. The example file with all messages addressed has
been saved as lengthofline2.m. Open the corrected example file with the command:
open(fullfile(matlabroot,'help','techdoc',...
'matlab_env', 'examples','lengthofline2.m'))
1 On the Editor window, click and select Show Code Analyzer Report.
24-10
Check Code for Errors and Warnings
Code analysis does not provide perfect information about every situation and sometimes,
you might not want to change the code based on a message. If you do not want to change
the code, and you do not want to see the indicator and message for that line, suppress
them. For the lengthofline example, in line 49, the first message is Terminate
statement with semicolon to suppress output (in functions). Adding a
semicolon to the end of a statement suppresses output and is a common practice. Code
analysis alerts you to lines that produce output, but lack the terminating semicolon. If you
want to view output from line 49, do not add the semicolon as the message suggests.
There are a few different ways to suppress (turn off) the indicators for warning and error
messages:
You cannot suppress error messages such as syntax errors. Therefore, instructions on
suppressing messages do not apply to those types of messages.
You can suppress a specific instance of a Code Analyzer message in the current file. For
example, using the code presented in “Check Code for Errors and Warnings” on page 24-
6 , follow these steps:
1 In line 49, right-click at the first underline (for a single-button mouse, press Ctrl
+click).
24-11
24 Coding and Productivity Tips
2 From the context menu, select Suppress 'Terminate statement with semicolon...'
> On This Line.
The comment %#ok<NOPRT> appears at the end of the line, which instructs MATLAB
not to check for a terminating semicolon at that line. The underline and mark in the
indicator bar for that message disappear.
3 If there are two messages on a line that you do not want to display, right-click
separately at each underline and select the appropriate entry from the context menu.
The %#ok syntax expands. For the example, in the code presented in “Check Code for
Errors and Warnings” on page 24-6, ignoring both messages for line 49 adds
%#ok<NBRAK,NOPRT>.
Even if Code Analyzer preferences are set to enable this message, the specific
instance of the message suppressed in this way does not appear because the %#ok
takes precedence over the preference setting. If you later decide you want to check
for a terminating semicolon at that line, delete %#ok<NOPRT> from the line.
You can suppress all instances of a specific Code Analyzer message in the current file. For
example, using the code presented in “Check Code for Errors and Warnings” on page 24-
6, follow these steps:
1 In line 49, right-click at the first underline (for a single-button mouse, press Ctrl
+click).
2 From the context menu, select Suppress 'Terminate statement with semicolon...'
> In This File.
The comment %#ok<*NOPRT> appears at the end of the line, which instructs MATLAB not
to check for a terminating semicolon throughout the file. All underlines and marks in the
message indicator bar that correspond to this message disappear.
If there are two messages on a line that you do not want to display anywhere in the
current file, right-click separately at each underline, and then select the appropriate entry
from the context menu. The %#ok syntax expands. For the example, in the code presented
in “Check Code for Errors and Warnings” on page 24-6, ignoring both messages for line
49 adds %#ok<*NBRAK,*NOPRT>.
Even if Code Analyzer preferences are set to enable this message, the message does not
appear because the %#ok takes precedence over the preference setting. If you later
24-12
Check Code for Errors and Warnings
decide you want to check for a terminating semicolon in the file, delete %#ok<*NOPRT>
from the line.
You can disable all instances of a Code Analyzer message in all files. For example, using
the code presented in “Check Code for Errors and Warnings” on page 24-6, follow these
steps:
1 In line 49, right-click at the first underline (for a single-button mouse, press Ctrl
+click).
2 Select Suppress 'Terminate statement with semicolon...' > In All Files.
If you know which message or messages that you want to suppress, you can disable them
directly using Code Analyzer preferences, as follows:
1
On the Home tab, in the Environment section, click Preferences.
2 Select MATLAB > Code Analyzer.
3 Search the messages to find the ones you want to suppress.
4 Clear the check box associated with each message you want to suppress in all files.
5 Click OK.
You can specify that you want certain Code Analyzer messages enabled or disabled, and
then save those settings to a file. When you want to use a settings file with a particular
file, you select it from the Code Analyzer preferences pane. That setting file remains in
effect until you select another settings file. Typically, you change the settings file when
you have a subset of files for which you want to use a particular settings file.
24-13
24 Coding and Productivity Tips
You can reuse these settings for any MATLAB file, or provide the settings file to another
user.
1
On the Home tab, in the Environment section, click Preferences.
The settings you choose are in effect for all MATLAB files until you select another set
of Code Analyzer settings.
24-14
Check Code for Errors and Warnings
1 Search the file for the %#ok directive and create a list of all the message IDs
associated with that directive.
2
On the Home tab, in the Environment section, click Preferences.
The message list now contains only the message that corresponds to that ID. If the
message is a hyperlink, click it to see an explanation and suggested action for the
message. This can provide insight into why the message is suppressed or disabled.
The following image shows how the Preferences dialog box appears when you enter
msgid:CPROP in the search field.
5
Click the button to clear the search field, and then repeat step 4 for each message
ID you found in step 1.
6 Display messages that are disabled by default and disabled in the Preferences pane
by clicking the down arrow to the right of the search field. Then, click Show
Disabled Messages.
7 Review the message associated with each message ID to understand why it is
suppressed in the code or disabled in Preferences.
24-15
24 Coding and Productivity Tips
• Sometimes, it fails to produce Code Analyzer messages where you expect them.
When provided with message, click the Detail button for additional information, which
can help you to make this determination. Error messages are almost always problems.
However, many warnings are suggestions to look at something in the code that is
unusual and therefore suspect, but might be correct in your case.
Suppress a warning message if you are certain that the message does not apply to
your situation. If your reason for suppressing a message is subtle or obscure, include a
comment giving the rationale. That way, those who read your code are aware of the
situation.
For details, see “Adjust Code Analyzer Message Indicators and Messages” on page 24-
11.
Code analysis cannot always distinguish function names from variable names. For the
following code, if the Code Analyzer message is enabled, code analysis returns the
message, Code Analyzer cannot determine whether xyz is a variable or a
function, and assumes it is a function. Code analysis cannot make a
determination because xyz has no obvious value assigned to it. However, the program
might have placed the value in the workspace in a way that code analysis cannot detect.
24-16
Check Code for Errors and Warnings
function y=foo(x)
.
.
.
y = xyz(x);
end
For example, in the following code, xyz can be a function, or can be a variable loaded
from the MAT-file. Code analysis has no way of making a determination.
function y=foo(x)
load abc.mat
y = xyz(x);
end
Variables might also be undetected by code analysis when you use the eval, evalc,
evalin, or assignin functions.
• Initialize the variable so that code analysis does not treat it as a function.
• For the load function, specify the variable name explicitly in the load command line.
For example:
function y=foo(x)
load abc.mat xyz
y = xyz(x);
end
Code analysis cannot always distinguish structures from handle objects. In the following
code, if x is a structure, you might expect a Code Analyzer message indicating that the
code never uses the updated value of the structure. If x is a handle object, however, then
this code can be correct.
function foo(x)
x.a = 3;
end
24-17
24 Coding and Productivity Tips
If some built-in functions are overloaded in a class or on the path, Code Analyzer
messages might apply to the built-in function, but not to the overloaded function you are
calling. In this case, suppress the message on the line where it appears or suppress it for
the entire file.
For information on suppressing messages, see “Adjust Code Analyzer Message Indicators
and Messages” on page 24-11.
Code analysis has a limited ability to determine the type of variables and the shape of
matrices. Code analysis might produce messages that are appropriate for the most
common case, such as for vectors. However, these messages might be inappropriate for
less common cases, such as for matrices.
Code Analyzer has limited capabilities to check class definitions with superclasses. For
example, Code Analyzer cannot always determine if the class is a handle class, but it can
sometimes validate custom attributes used in a class if the attributes are inherited from a
superclass. When analyzing class definitions, Code Analyzer tries to use information from
the superclasses but often cannot get enough information to make a certain
determination.
Most class methods must contain at least one argument that is an object of the same class
as the method. But it does not always have to be the first argument. When it is, code
analysis can determine that an argument is an object of the class you are defining, and it
can do various checks. For example, it can check that the property and method names
exist and are spelled correctly. However, when code analysis cannot determine that an
object is an argument of the class you are defining, then it cannot provide these checks.
24-18
Check Code for Errors and Warnings
—when you change the setting from the Editor, it effectively changes this preference.
However, if the dialog box is open at the time you modify the setting in the Editor, you will
not see the changes reflected in the Preferences dialog box. Whether you change the
setting from the Editor or from the Preferences dialog box, it applies to the Editor and to
the Code Analyzer Report.
1
On the Home tab, in the Environment section, click Preferences.
The settings txt file, which you can create as described in “Save and Reuse Code
Analyzer Message Settings” on page 24-13, includes the status of this setting.
24-19
24 Coding and Productivity Tips
Indenting Code
Indenting code makes reading statements such as while loops easier. To set and apply
indenting preferences to code in the Editor:
1
On the Home tab, in the Environment section, click Preferences.
If you clear this option, you can manually apply indenting by selecting the lines in the
Editor to indent, right-clicking, and then selecting Smart Indent from the context
menu.
5 Do one of the following:
• If you chose any language other than MATLAB in step 2, click OK.
• If you chose MATLAB in step 2, select a Function indenting format, and then
click OK. Function indent formats are:
• Classic — The Editor aligns the function code with the function declaration.
• Indent nested functions — The Editor indents the function code within a
nested function.
• Indent all functions — The Editor indents the function code for both
main and nested functions.
24-20
Improve Code Readability
Note Indenting preferences are not supported for TLC, VHDL, or Verilog.
Regardless of whether you apply indenting automatically or manually, you can move
selected lines further to the left or right, by doing one of the following:
•
On the Editor tab, in the Edit section, click , , or . For live scripts and
functions, this functionality is available on the Live Editor tab, in the Code section.
• Pressing the Tab key or the Shift+Tab key, respectively.
This works differently if you select the Editor/Debugger Tab preference for Emacs-
style Tab key smart indenting—when you position the cursor in any line or select a
group of lines and press Tab, the lines indent according to smart indenting practices.
24-21
24 Coding and Productivity Tips
1
On the Home tab, in the Environment section, click Preferences.
Note This limit is a visual cue only and does not prevent text from exceeding the limit. To
wrap comment text at a specified column number automatically, adjust the settings in the
Comment formatting section under MATLAB > Editor/Debugger > Language in the
Preferences dialog box.
To see the entire list of constructs, select Editor/Debugger > Code Folding in the
Preferences dialog box.
24-22
Improve Code Readability
To expand or collapse code, click the plus or minus sign that appears to the left of
the construct in the Editor. You also use the Ctrl+Shift+. (period) and Ctrl+. (period)
keyboard shortcuts, or use the code folding buttons in the View tab.
To expand or collapse all of the code in a file, place your cursor anywhere within the file,
right-click, and then select Code Folding > Expand All or Code Folding > Fold All
from the context menu. You also can use the Ctrl+Shift+, (comma) and Ctrl+,
(comma) keyboard shortcuts, or the code folding buttons in the View tab.
You can view code that is currently folded by positioning the pointer over its ellipsis .
The code appears in a tooltip. Viewing folded code in a tooltip is not supported in
MATLAB Online.
The following image shows the tooltip that appears when you place the pointer over the
ellipsis on line 23 of lenghtofline.m when a for loop is folded.
If you print a file with one or more collapsed constructs, those constructs are expanded in
the printed version of the file.
Code Folding Behavior for Functions that Have No Explicit End Statement
If you enable code folding for functions and a function in your code does not end with an
explicit end statement, you see the following behavior:
24-23
24 Coding and Productivity Tips
• If a line containing only comments appears at the end of such a function, then the
Editor does not include that line when folding the function. This is because MATLAB
does not include trailing white space and comments in a function definition that has no
explicit end statement. In MATLAB Online, the Editor does include the line when
folding the function.
“Code Folding Enabled for Function Code Only” on page 24-25 illustrates this
behavior. Line 13 is excluded from the fold for the foo function.
• If a fold for a code section overlaps the function code, then the Editor does not show
the fold for the overlapping section.
The three figures that follow illustrate this behavior. The first two figures, “Code
Folding Enabled for Function Code Only” on page 24-25 and “Code Folding Enabled
for Sections Only” on page 24-26 illustrate how the code folding appears when you
enable it for function code only and then section only, respectively. The last figure,
“Code Folding Enabled for Both Functions and Sections” on page 24-27, illustrates
the effects when code folding is enabled for both. Because the fold for section 3 (lines
11–13) overlaps the fold for function foo (lines 4–12), the Editor does not display the
fold for section 3.
24-24
Improve Code Readability
24-25
24 Coding and Productivity Tips
24-26
Improve Code Readability
24-27
24 Coding and Productivity Tips
Refactoring is also available in the Editor in MATLAB Online. To refactor a selected area
of code in the MATLAB Online Editor, on the Editor tab, in the Edit section, click the
code refactoring button . Then, select from the available options.
24-28
Find and Replace Text in Files
1 Within the current file, select the text you want to find.
2
On the Editor or Live Editor tab, in the Navigate section, click Find , and then
select Find....
To find the previous occurrence of selected text (find backwards) in the current file, click
Find Previous on the Find & Replace dialog box.
24-29
24 Coding and Productivity Tips
the same name, but are in different scopes on page 20-11, highlighting one does not
cause the other to highlight.
1 In a file open in the Editor, click an instance of the variable you want to find
throughout the file. MATLAB indicates all occurrences of that variable within the file
by:
If a code analyzer indicator and a variable indicator appear on the same line in a
file, the marker for the variable takes precedence.
2 Hover over a marker in the indicator bar to see the line it represents.
3 Click a marker in the indicator bar to navigate to that occurrence of the variable.
4 Replace an instance of a function or variable by editing the occurrence at a line to
which you have navigated.
This image shows an example of how the Editor looks with variable highlighting enabled.
In the image, the variable i appears highlighted in sky blue, and the indicator bar
contains three variable markers.
24-30
Find and Replace Text in Files
To disable automatic highlighting, go to the Home tab and in the Environment section,
click Preferences. In MATLAB > Colors > Programming Tools, clear the
Automatically highlight option.
function foo(m)
Input or output variable name in a function Rename y or m in:
declaration
function y = foo(m)
24-31
24 Coding and Productivity Tips
As you rename such a function or variable, a tooltip opens if there is more than one
reference to that variable or function in the file. The tooltip indicates that MATLAB will
rename all instances of the function or variable in the file when you press Shift + Enter.
Typically, multiple references to a function appear when you use nested functions or local
functions.
24-32
Find and Replace Text in Files
Live Editor tab, in the Navigate section, click Find to open and use the Find &
Replace dialog box.
specified text. On the Editor or Live Editor tab, in the File section, click Find Files
to open the Find Files dialog box. For details, see “Find Files and Folders”.
Go To Location in File
You can go to a specific location in a file, such as a line number or function definition, set
bookmarks, navigate backward and forward within the file, and open a file or variable
from within a file.
24-33
24 Coding and Productivity Tips
This table summarizes the steps for navigating to a specific location within a file open in
the Editor and Live Editor.
Go To Steps Notes
Line Number 1 On the Editor or Live Editor tab, None
24-34
Find and Replace Text in Files
Go To Steps Notes
1 On the Editor or Live Editor tab, For more information, see “Divide Your
File into Code Sections” on page 18-7.
in the Navigate section, click
Go To .
2 Under Sections, select the title of
the code section to which you
want to navigate.
1 In the Current Folder browser,
click the name of the file that is
Code Section
open in the Editor.
2 Click the up arrow at the
bottom of Current Folder browser
to open the detail panel.
3 In the detail panel, double-click
24-35
24 Coding and Productivity Tips
Go To Steps Notes
Method 1 In the Current Folder browser, For more information, see “Methods in
click the name of the file that is Class Design”.
open in the Editor.
2 Click the up arrow at the
bottom of Current Folder browser
to open the detail panel.
3 In the detail panel, double-click
Note The Details Panel and Bookmarks are not available in the Live Editor or in MATLAB
Online.
Set Bookmarks
You can set a bookmark at any line in a file in the Editor so you can quickly navigate to
the bookmarked line. This is particularly useful in long files. For example, suppose while
working on a line, you want to look at another part of the file, and then return. Set a
bookmark at the current line, go to the other part of the file, and then use the bookmark
to return.
To set a bookmark:
24-36
Find and Replace Text in Files
To clear a bookmark, position the cursor anywhere on the line. Click Go To and
select Set/Clear under Bookmarks.
To access lines in a file in the same sequence that you previously navigated or edited
Backward and forward navigation is not supported in the Live Editor or in MATLAB
Online.
1
Click .
2
Click .
3 Edit a line or navigate to another line using the list of features described in “Navigate
to a Specific Location” on page 24-34.
You can still go to the lines preceding the interruption point in the sequence, but you
cannot go to any lines after that point. Any lines you edit or navigate to after interrupting
the sequence are added to the sequence after the interruption point.
For example:
1 Open a file.
2 Edit line 2, line 4, and line 6.
3
Click to return to line 4, and then to return to line 2.
4
Click to return to lines 4 and 6.
5
Click to return to line 1.
6 Edit at 3.
24-37
24 Coding and Productivity Tips
This interrupts the sequence. You can no longer use to return to lines 4 and 6.
You can open a function, file, variable, or Simulink model from within a file in the Editor.
Position the cursor on the name, and then right-click and select Open selection from the
context menu. Based on what the selection is, the Editor performs a different action, as
described in this table.
Item Action
Local function Navigates to the local function within the current file, if that
file is a MATLAB code file. If no function by that name exists in
the current file, the Editor runs the open function on the
selection, which opens the selection in the appropriate tool.
Text file Opens in the Editor.
Figure file (.fig) Opens in a figure window.
MATLAB variable that is Opens in the Variables Editor.
in the current workspace
Model Opens in Simulink.
24-38
Add Reminders to Files
This sample TODO/FIXME Report shows a file containing the text TODO, FIXME, and
NOTE. The search is case insensitive.
Note You cannot run reports when the path is a UNC (Universal Naming Convention)
path; that is, a path that starts with \\. Instead, use an actual hard drive on your
system, or a mapped network drive.
2 On the Current Folder browser, click , and then select Reports > TODO/FIXME
Report.
• TODO
• FIXME
24-39
24 Coding and Productivity Tips
You can then enter any text in this field, including a regular expression on page 2-
64. For example, you can enter NOTE, tbd, or re.*check.
Specifying custom text or which lines to search for is not supported in MATLAB
Online.
4 Run the report on the files in the current folder, by clicking Rerun This Report.
The window refreshes and lists all lines in the MATLAB files within the specified
folder that contain the text you selected in step 1. Matches are not case sensitive.
If you want to run the report on a folder other than the one currently specified in the
report window, change the current folder. Then, click Run Report on Current
Folder.
To open a file in the Editor at a specific line, click the line number in the report. Then you
can change the file, as needed.
Suppose you have a file, area.m, in the current folder. The code for area.m appears in
the image that follows.
When you run the TODO/FIXME report on the folder containing area.m, with the text
TODO and FIXME selected and the text NOTE specified and selected, the report lists:
9 and rectangle. (todo)
24-40
Add Reminders to Files
• Line 9 as a match for the text TODO. The report includes lines that have the selected
text regardless of its placement within a comment.
• Lines 14 and 17 as a match for the text FIXME. The report matches selected text in the
file regardless of their casing.
• Line 21 as a match for the text NOTE. The report includes lines that have text as
specified in the text field, assuming that you select the text field.
24-41
24 Coding and Productivity Tips
1 In the Current Folder browser, navigate to the folder that contains the files you want
to check.
To use the lengthofline.m example shown in this documentation, save the file to
the current folder, or to a folder for which you have write access. This example saves
the file to the current folder, C:\my_MATLAB_files.
copyfile(fullfile(matlabroot,'help','techdoc','matlab_env','examples','lengthofline.m'))
2 In the Current Folder browser, click , and then select Reports > Code Analyzer
Report.
The report displays in the MATLAB Web Browser, showing those files identified as
having potential problems or opportunities for improvement.
24-42
MATLAB Code Analyzer Report
3 For each message in the report, review the suggestion and your code. Click the line
number to open the file in the Editor at that line, and change the file based on the
message. Use the following general advice:
• If you are unsure what a message means or what to change in the code, click the
link in the message if one appears. For details, see “Check Code for Errors and
Warnings” on page 24-6.
• If the message does not contain a link, and you are unsure what a message means
or what to do, search for related topics in the Help browser. For examples of
messages and what to do about them, including specific changes to make for the
example, lengthofline.m, see “Changing Code Based on Code Analyzer
Messages” on page 24-44.
• The messages do not provide perfect information about every situation and in
some cases, you might not want to change anything based on the message. For
details, see “Understand the Limitations of Code Analysis” on page 24-16.
• If there are certain messages or types of messages you do not want to see, you
can suppress them. For details, see “Adjust Code Analyzer Message Indicators and
Messages” on page 24-11.
4 After modifying the file, save it. Consider saving the file to a different name if you
made significant changes that might introduce errors. Then you can refer to the
24-43
24 Coding and Productivity Tips
original file, if needed, to resolve problems with the updated file. Use the
Compare button on the Editor or Live Editor tab to help you identify the changes
you made to the file. For more information, see “Compare and Merge Text”.
5 Run and debug the file or files again to be sure that you have not introduced any
inadvertent errors.
6 If the report is displaying, click Rerun This Report to update the report based on
the changes you made to the file. Ensure that the messages are gone, based on the
changes you made to the files. To rerun the report in MATLAB Online, in the Current
Folder browser, click , and then select Reports > Code Analyzer Report.
• Open the file in the Editor and click the Details button in the tooltip, as shown in the
image following this list. An extended message opens. However, not all messages have
extended messages.
• Use the Help browser Search pane to find documentation about terms presented in
the messages.
The following image shows a tooltip with a Details button. The orange line under the
equals (=) sign indicates a tooltip displays if you hover over the equals sign. The orange
highlighting indicates that an automatic fix is available.
24-44
MATLAB Code Analyzer Report
• Access the Code Analyzer Report for a file from the Profiler detail report.
• Run the checkcode function, which analyzes the specified file and displays messages
in the Command Window.
• Run the mlintrpt function, which runs checkcode and displays the messages in the
Web Browser.
• Use automatic code checking while you work on a file in the Editor. See “Automatically
Check Code in the Editor — Code Analyzer” on page 24-6.
24-45
24 Coding and Productivity Tips
1 In the Current Folder browser, navigate to the folder that contains the code files you
want to analyze.
2 In the Current Folder browser, either click or right-click the white space. Then
select Reports > Code Compatibility Report.
The report displays in the MATLAB Web Browser, showing potential compatibility
issues. For example:
24-46
MATLAB Code Compatibility Report
3 Update your code to resolve the syntax errors for each file listed in the Syntax
Errors section. Syntax errors result in code that does not run. While most likely the
code did not run properly in previous releases, syntax errors impact compatibility
analysis. For example, Parse error at '}': usage might be invalid MATLAB syntax.
4 For each functionality listed in the report, review the issue description and your code.
Messages include the line numbers to help locate the issue in your code. To open the
file in the Editor at that line, click the line number. Then change the file based on the
message. If you are unsure what a message means or what to change in the code,
click the Documentation link associated with the message.
Each functionality listed in the report displays a recommended action. You also can
use the following general advice:
24-47
24 Coding and Productivity Tips
The Code Compatibility Report also includes information about the checks performed
on your code and the list of files that MATLAB analyzed for code compatibility.
Programmatic Use
When you generate a Code Compatibility Report through the current folder browser,
MATLAB analyzes code in the current working folder and subfolders. However, if you
generate a report programmatically, you can specify particular files to analyze or to
exclude subfolders from analysis. To generate a report programmatically, use one of the
following methods.
• To generate a report that opens in the MATLAB® Web Browser programmatically, use
the codeCompatibilityReport function.
• To return a CodeCompatibilityAnalysis object that contains the report
information, use the analyzeCodeCompatibility function. You can then display a
report for the stored object using the codeCompatibilityReport function.
See Also
CodeCompatibilityAnalysis | analyzeCodeCompatibility |
codeCompatibilityReport
24-48
25
Programming Utilities
1 Type clear functions to clear all functions from memory (see Note below).
Note clear functions does not clear functions locked by mlock. If you have
locked functions (which you can check using inmem) unlock them with munlock, and
then repeat step 1.
2 Execute the function you want to check. Note that the function arguments you choose
to use in this step are important, because you can get different results when calling
the same function with different arguments.
3 Type inmem to display all program files that were used when the function ran. If you
want to see what MEX-files were used as well, specify an additional output:
[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
fList
fList =
'C:\work\myFun.m'
The only required program file, is the function file itself, myFun.
25-2
Identify Program Dependencies
{pList.Name}'
ans =
'MATLAB'
'Image Processing Toolbox'
The file, myFun.m, requires both MATLAB and the Image Processing Toolbox.
• Which files in the folder are required by other files in the folder
• If any files in the current folder will fail if you delete a file
• If any called files are missing from the current folder
• Files in the toolbox/matlab folder because every MATLAB user has those files.
Therefore, if you use a function file that shadows a built-in function file, MATLAB
excludes both files from the list.
• Files called from anonymous functions.
• The superclass for a class file.
• Files called from eval, evalc, run, load, function handles, and callbacks.
MATLAB does not resolve these files until run time, and therefore the Dependency
Report cannot discover them.
• Some method files.
The Dependency Report finds class constructors that you call in a MATLAB file.
However, any methods you execute on the resulting object are unknown to the report.
These methods can exist in the classdef file, as separate method files, or files
belonging to superclass or superclasses of a method file.
• The search path when you run the report is the same as when you run the files in the
folder. (That is, the current folder is at the top of the search path.)
25-3
25 Programming Utilities
• The files in the folder for which you are running the report do not change the search
path or otherwise manipulate it.
• The files in the folder do not load variables, or otherwise create name clashes that
result in different program elements with the same name.
Note Do not use the Dependency Report to determine which MATLAB code files someone
else needs to run a particular file. Instead use the
matlab.codetools.requiredFilesAndProducts function.
1 Use the Current Folder pane to navigate to the folder containing the files for which
you want to produce a Dependency Report.
Note You cannot run reports when the path is a UNC (Universal Naming Convention)
path; that is, a path that starts with \\. Instead, use an actual hard drive on your
system, or a mapped network drive.
2 On the Current Folder pane, click , and then select Reports > Dependency
Report.
• To see a list of all MATLAB code files (children) called by each file in the folder
(parent), select Show child functions.
The report indicates where each child function resides, for example, in a specified
toolbox. If the report specifies that the location of a child function is unknown, it
can be because:
The report limits the parent (calling) functions to functions in the current folder.
25-4
Identify Program Dependencies
• To include local functions in the report, select Show subfunctions. The report
lists local functions directly after the main function and highlights them in gray.
4 Click Run Report on Current Folder.
The following image shows a Dependency Report. It indicates that chirpy.m calls two
files in Signal Processing Toolbox™ and one in Image Processing Toolbox. It also shows
that go.m calls mobius.m, which is in the current folder.
The list of files in the folder on which you ran the Dependency Report. Click a link in
this column to open the file in the Editor.
• Children
25-5
25 Programming Utilities
Click a link in this column to open the MATLAB file listed in the same row, and go to
the first reference to the called function. For instance, suppose your Dependency
Report appears as shown in the previous image. Clicking \images\images\erode.m
opens chirpy.m and places the cursor at the first line that references erode. In other
words, it does not open erode.m.
• Multiple class methods
Because the report is a static analysis, it cannot determine run-time data types and,
therefore, cannot identify the particular class methods required by a file. If multiple
class methods match a referenced method, the Dependency Report inserts a question
mark link next to the file name. The question mark appears in the following image.
Click the question mark link to list the class methods with the specified name that
MATLAB might use. MATLAB lists almost all the method files on the search path that
match the specified method file (in this case, freqresp.m). Do not be concerned if the
list includes methods of classes and MATLAB built-in functions that are unfamiliar to
you.
It is not necessary for you to determine which file MATLAB will use. MATLAB
determines which method to use depending on the object that the program calls at run
time.
25-6
Protect Your Source Code
• Deploy as P-code on page 25-7 — Convert some or all of your source code files to a
content-obscured form called a P-code file (from its .p file extension), and distribute
your application code in this format. When MATLAB P-codes a file, the file is
obfuscated not encrypted. While the content in a .p file is difficult to understand, it
should not be considered secure. It is not recommended that you P-code files to
protect your intellectual property.
MATLAB does not support converting live scripts or live functions to P-code files.
• Compile into binary format on page 25-8 — Compile your source code files using the
MATLAB Compiler to produce a standalone application. Distribute the latter to end
users of your application.
Note Because users of P-code files cannot view the MATLAB code, consider providing
diagnostics to enable a user to proceed in the event of an error.
To generate a P-code file, enter the following command in the MATLAB Command
Window:
pcode file1 file2, ...
The command produces the files, file1.p, file2.p, and so on. To convert all .m source
files residing in your current folder to P-code files, use the command:
25-7
25 Programming Utilities
pcode *.m
See the pcode function reference page for a description of all syntaxes for generating P-
code files.
You invoke the resulting P-code file in the same way you invoke the MATLAB .m source
file from which it was derived. For example, to invoke file myfun.p, type
myscript;
When you call a P-code file, MATLAB gives it execution precedence over its
corresponding .m source file. This is true even if you happen to change the source code at
some point after generating the P-code file. Remember to remove the .m source file
before distributing your code.
P-code files are designed to be independent of the release under which they were created
and the release in which they are used (backward and forward compatibility). New and
deprecated MATLAB features can be a problem, but it is the same problem that would
exist if you used the original MATLAB input file. To fix errors of this kind in a P-code file,
fix the corresponding MATLAB input file and create a new P-code file.
P-code files built using MATLAB Version 7.4 and earlier have a different format than those
built with more recent versions of MATLAB. These older P-code files do not run in
MATLAB 8.6 (R2015b) or later. Rebuild any P-code files that were built with MATLAB 7.4
or earlier using a more recent version of MATLAB, and then redistribute them as
necessary.
25-8
Protect Your Source Code
To build a standalone application for your MATLAB application, develop and debug your
application following the usual procedure for MATLAB program files. Then, generate the
executable file or files following the instructions in “Create Standalone Application from
MATLAB” (MATLAB Compiler).
25-9
25 Programming Utilities
Use matlab: syntax to create a hyperlink in the Command Window that runs one or more
functions. For example, you can use disp to display the word Hypotenuse as an
executable hyperlink as follows:
disp('<a href="matlab:a=3; b=4;c=hypot(a,b)">Hypotenuse</a>')
Clicking the hyperlink executes the three commands following matlab:, resulting in
c =
5
Executing the link creates or redefines the variables a, b, and c in the base workspace.
The argument to disp is an <a href> HTML hyperlink. Include the full hypertext text,
from '<a href= to </a>' within a single line, that is, do not continue long text on a new
line. No spaces are allowed after the opening < and before the closing >. A single space is
required between a and href.
You cannot directly execute matlab: syntax. That is, if you type
matlab:a=3; b=4;c=hypot(a,b)
you receive an error, because MATLAB interprets the colon as an array operator in an
illegal context:
??? matlab:a=3; b=4;c=hypot(a,b)
|
Error: The expression to the left of the equals sign
is not a valid target for an assignment.
You do not need to use matlab: to display a live hyperlink to the Web. For example, if
you want to link to an external Web page, you can use disp, as follows:
disp('<a href="http://en.wikipedia.org/wiki/Hypotenuse">Hypotenuse</a>')
The result in the Command Window looks the same as the previous example, but instead
opens a page at en.wikipedia.org:
25-10
Create Hyperlinks that Run Functions
Hypotenuse
x = 0:1:8;
y = sin(x);
plot(x,y)
x = -2*pi:pi/16:2*pi;
25-11
25 Programming Utilities
Click the hyperlink, Plot x,y again and it changes the current value of x back to 0:1:8.
The code that matlab: runs when you click the Plot x,y defines x in the base
workspace.
The Command Window displays the links that follow. Depending on which link you click,
MATLAB sets state to 0 or 1.
Some symbols might not be interpreted correctly and you might need to use the ASCII
value for the symbol. For example, an alternative way to run the previous statement is to
use ASCII 62 instead of the greater than symbol:
disp('<a href="matlab:str=[''Value '' char(62) '' 0'']">Positive</a>')
25-12
Create and Share Toolboxes
In this section...
“Create Toolbox” on page 25-13
“Share Toolbox” on page 25-21
You can package MATLAB files to create a toolbox to share with others. These files can
include MATLAB code, data, apps, examples, and documentation. When you create a
toolbox, MATLAB generates a single installation file (.mltbx) that enables you or others
to install your toolbox.
Create Toolbox
To create a toolbox installation file:
1
In the Environment section of the Home tab, select Package Toolbox from the
Add-Ons menu.
2
In the Package a Toolbox dialog box, click the button and select your toolbox
folder. It is good practice to create the toolbox package from the folder level above
your toolbox folder. The .mltbx toolbox file contains information about the path
settings for your toolbox files and folders. By default, any of the included folders and
files that are on your path when you create the toolbox appear on their paths after
the end users install the toolbox.
3 In the dialog box, add the following information about your toolbox.
Toolbox Description
Informatio
n Field
Toolbox Enter the toolbox name, if necessary. By default, the toolbox name
Name is the name of the toolbox folder. The Toolbox Name becomes
the .mltbx file name.
Version Enter the toolbox version number in the Major.Minor.Bug.Build
format. Bug and Build are optional.
25-13
25 Programming Utilities
Toolbox Description
Informatio
n Field
Author Enter contact information for the toolbox author. To save the
Name, contact information, click Set as default contact.
Email, and
Company
Toolbox To select an image that represents your toolbox, click Select
Image toolbox image.
Summary Enter the toolbox summary and description. It is good practice to
and keep the Summary text brief and to add detail to the Description
Description text.
4 To ensure MATLAB detects the expected components, review the toolbox contents.
The following sections of the Package a Toolbox dialog box appear after you select a
toolbox folder.
Package a Description
Toolbox
Dialog Box
Section
Toolbox List of the folders and files contained in your toolbox. The listed
Files and files and folders are only those files that are located in the top level
Folders of the toolbox folder. You cannot navigate through the folders in the
Toolbox Packaging dialog box.
25-14
Create and Share Toolboxes
Package a Description
Toolbox
Dialog Box
Section
Requireme Add-ons — List of add-ons required for your toolbox. Selected add-
nts ons are downloaded and installed when the toolbox is installed.
MATLAB auto-populates this list with the add-ons it thinks the
toolbox requires and selects them all by default. You can choose to
omit any add-ons you do not want to install with your toolbox.
25-15
25 Programming Utilities
Package a Description
Toolbox
Dialog Box
Section
Installation of Additional Software — List of additional software ZIP
files that are installed on the user's system when they install a
toolbox.
mysoftwarelocation = 'C:\InstalledSoftware\mysoftware\'
25-16
Create and Share Toolboxes
Package a Description
Toolbox
Dialog Box
Section
with this code:
mysoftwarelocation = mytoolbox.getInstallationLocation('mysoftware')
25-17
25 Programming Utilities
Package a Description
Toolbox
Dialog Box
Section
Examples, Examples—Published MATLAB examples associated with your
Apps, and toolbox. To include .m and .mlx files as examples, click the Add
Document examples button, select your code file, and click Publish HTML.
ation MATLAB publishes the code to HTML and places the output files in
the html folder.
25-18
Create and Share Toolboxes
Package a Description
Toolbox
Dialog Box
Section
Apps—Published MATLAB installable apps associated with your
toolbox. The Package a Toolbox tool recognizes apps (.mlapp files)
and app installer files (.mlappinstall files) and includes them in
your toolbox.
Users of your toolbox can view the Getting Started Guide through
the Options menu for the toolbox in the Add-On Manager. For more
information, see “Get and Manage Add-Ons”.
25-19
25 Programming Utilities
Package a Description
Toolbox
Dialog Box
Section
Help Browser Integration—Custom documentation associated with
your toolbox. For the Package a Toolbox tool to recognize custom
documentation, include an info.xml file to identify your
documentation files. If you use the builddocsearchdb function to
build the documentation database before packaging your toolbox,
you can include the generated helpsearch subfolder in your
toolbox. The info.xml file and the helpsearch folder allow
recipients to access your documentation through the
Supplemental Software link at the bottom of the Help browser
home page. For more information, see “Display Custom
Documentation” on page 31-27.
• To save your toolbox, click Package at the top of the Package a Toolbox dialog
box. Packaging your toolbox generates a .mltbx file in your current MATLAB
folder.
• To save your toolbox and share it on MATLAB Central File Exchange, select
Package and Share from the Package menu at the top of the Package a Toolbox
dialog box. This option generates a .mltbx file in your current MATLAB folder
and opens a web page for your toolbox submission to File Exchange. MATLAB
populates the File Exchange submission form with information about the toolbox.
Review and submit the form to share your toolbox on File Exchange.
When you create a toolbox, MATLAB generates a .prj file that contains information
about the toolbox and saves it frequently. It is good practice to save this
associated .prj file so that you can quickly create future revisions of your toolbox.
25-20
See Also
Share Toolbox
To share your toolbox with others, give them the .mltbx file. All files you added when
you packaged the toolbox are included in the .mltbx file. When the end users install your
toolbox, they do not need to be concerned with the MATLAB path or other installation
details. The .mltbx file manages these details for end users.
For information on installing, uninstalling, and viewing information about toolboxes, see
“Get and Manage Add-Ons”.
You can share your toolbox with others by attaching the .mltbx file to an email message,
or using any other method you typically use to share files—such as uploading to MATLAB
Central File Exchange. If you upload your toolbox to File Exchange, your users can
download the toolbox from within MATLAB. For more information, see “Get and Manage
Add-Ons”.
Alternatively, you can upload your toolbox to File Exchange when you package it. Select
Package and Share from the Package menu at the top of the Package a Toolbox dialog
box.
Note While .mltbx files can contain any files you specify, MATLAB Central File
Exchange places additional limitations on submissions. If your toolbox contains any of the
following, it cannot be submitted to File Exchange:
• MEX-files.
• Other binary executable files, such as DLLs or ActiveX® controls. (Data and image files
are typically acceptable.)
See Also
matlab.addons.toolbox.installToolbox |
matlab.addons.toolbox.installedToolboxes |
matlab.addons.toolbox.packageToolbox |
matlab.addons.toolbox.toolboxVersion |
matlab.addons.toolbox.uninstallToolbox | publish
25-21
25 Programming Utilities
Related Examples
• “Get and Manage Add-Ons”
• “Display Custom Examples” on page 31-37
• “Package Apps From the MATLAB Toolstrip”
• “Display Custom Documentation” on page 31-27
25-22
26
In this section...
“Introduction to Argument Validation” on page 26-2
“Where to Use Argument Validation” on page 26-3
“arguments Block Syntax” on page 26-3
“Examples of Argument Validation” on page 26-7
“Kinds of Arguments” on page 26-9
“Required and Optional Positional Arguments” on page 26-9
“Repeating Arguments” on page 26-11
“Name-Value Arguments” on page 26-14
“Name-Value Arguments from Class Properties” on page 26-17
“Argument Validation in Class Methods” on page 26-19
“Order of Argument Validation” on page 26-20
“Avoiding Class and Size Conversions” on page 26-21
“nargin in Argument Validation” on page 26-23
“Restrictions on Variable and Function Access” on page 26-25
The function argument validation syntax simplifies the process of defining optional,
repeating, and name-value arguments. Also, it enables you to define default values in a
consistent way.
26-2
Function Argument Validation
In local and private functions, and in private or protected methods, the caller is aware of
input requirements so these types of functions can be called with valid arguments.
You cannot use argument validation syntax in nested functions, abstract methods, or
handle class destructor methods. For more information on argument validation in
methods, see “Argument Validation in Class Methods” on page 26-19.
You can use multiple arguments blocks in a function, but all blocks must occur before
any code that is not part of an arguments block.
The highlighted area in the following code shows the syntax for argument validation.
26-3
26 Function Argument Validation
The function argument declaration can include any of these kinds of restrictions:
You can define a default value for the input argument in the function validation
declaration for that argument. Ensure that the default value satisfies the declared
restrictions for that argument.
Size
Validation size is the dimensions of the input argument, specified with non-negative
integer numbers or colons (:). A colon indicates that any length is allowed in that
dimension. You cannot use expressions for dimensions. The value assigned to the
argument in the function call must be compatible with the specified size or MATLAB
throws an error.
MATLAB indexed assignment rules apply to size specifications. For example, a 1-by-1
value is compatible with the size specified as (5,3) because MATLAB applies scalar
expansion. Also, MATLAB row-column conversion applies so that a size specified as
(1,:)can accept a size of 1-by-n and n-by-1.
26-4
Function Argument Validation
Class
Validation class is the name of a single class. The value assigned to the function input
must be of the specified class or convertible to the specified class. Use any MATLAB class
or externally defined class that is supported by MATLAB, except Java, COM classes, and
MATLAB classes defined before MATLAB software Version 7.6 (class definitions that do
not use the classdef keyword).
• char - input must be of class char, or a value that MATLAB can convert to a char,
such as string.
• double numeric value of any precision
• cell a cell array
• A user-defined class, such as an enumeration class, can restrict inputs to more specific
values and enables you to control what conversions are supported.
• If you do not specify a class, then any class is allowed unless restricted by validation
functions.
Validation Functions
During the validation process, MATLAB passes the argument value to each validation
function listed for that argument. MATLAB calls each function from left to right and
throws the first error encountered.
The value passed to the validation functions is the result of any conversion made by the
class and size specifications.
26-5
26 Function Argument Validation
Default Value
An argument default value can be any constant or expression that satisfies the size, class,
and validation function requirements. Specifying a default value in an argument
declaration makes the argument optional. MATLAB uses the default value when the
argument is not included in the function call. Default value expressions are evaluated
each time the default is used.
Note Because MATLAB validates the default value only when the function is called
without a value for the argument, an invalid default value causes an error only when the
function is called without that argument.
Validation Sequence
Arguments are validated from top to bottom in the arguments block. MATLAB validates
each part of an argument declaration in a specific order. First the class is validated, then
the size. The result of class and size validations are passed to the validation functions.
Each step is optional depending on whether class, size, and validation functions are in the
argument declaration.
Both class validation and size validation can change the value of an input argument
because of standard MATLAB conversion rules. Therefore, the validated value in the
function body can be different from the value passed when calling the function. The
conversion rules are derived from the rules that MATLAB applies for indexed assignment
of the form:
A(indices) = value
MATLAB can determine that the left side value has requirements for class and size and, in
certain cases, can convert the right side value to be of the required class and size.
For related information, see “Avoiding Class and Size Conversions” on page 26-21.
26-6
Function Argument Validation
% Function code
...
end
• A is a string scalar.
• B is a 1-by-any length vector of doubles.
• C is a 2-by-2 cell array.
Value Conversion
The following function illustrates how inputs can be converted to match the classes
specified in the arguments block. The SpeedEnum class is an enumeration class created
to define the values allowed for the third input argument.
function forwardSpeed(a,b,c)
arguments
a double
b char
c SpeedEnum
end
% Function code
disp(class(a))
disp(class(b))
disp(class(c))
end
26-7
26 Function Argument Validation
Full (100)
Half (50)
Stop (0)
end
end
This call to the function uses input values that MATLAB can convert to the declared types.
The actual argument types within the function are displayed as output.
forwardSpeed(int8(4),"A string",'full')
double
char
SpeedEnum
Validation functions can restrict input arguments in more specific ways. You can use
predefined validation functions for many common kinds of validation and you can define
your own validation function to satisfy specific requirements.
For example, this function specifies the following validations using mustBeNumeric,
mustBeReal, mustBeMember, and the local function mustBeEqualSize.
26-8
Function Argument Validation
Avoid using function argument validation within custom validation functions. For more
information about defining validation functions and for a list of predefined validation
functions, see “Argument Validation Functions” on page 26-27.
Kinds of Arguments
Function argument validation can declare four kinds of arguments. Functions can define
any of these kinds of arguments, but the arguments must be defined in the following
order:
1 Required positional arguments
2 Optional positional arguments
3 Repeating positional arguments
4 Optional name-value arguments
Positional arguments in the arguments block are required when calling the function,
unless the argument defines a default value. Specifying a default value in the argument
declaration makes a positional argument optional because MATLAB can use the default
value when no value is passed in the function call.
The default value can be a constant or an expression that produces a result that satisfies
the argument declaration. The expression can refer to the arguments that are declared
before it in the arguments block, but not arguments that are declared after it.
MATLAB evaluates the default value expression only when the argument is not included
in the function call.
All optional arguments must be positioned after all required arguments in the arguments
block. For example, in this argument block, maxlen and minlen have default values, and
are therefore optional.
function myFunction(x,y,maxval,minval)
arguments
26-9
26 Function Argument Validation
x (1,:) double
y (1,:) double
maxval (1,1) double = max(max(x),max(y))
minval (1,1) double = min(min(x),min(y))
end
% Function code
....
end
An optional positional argument becomes required when its position must be filled in the
function call to identify arguments that come after it. That is, if you want to specify a
value for minlen, you must specify a value for maxlen.
MATLAB lets you ignore input arguments by passing a tilde character (~) in place of the
argument. You can define a function that ignores unused positional arguments by adding
a tilde character (~) in the arguments block corresponding to the position of the
argument in the function signature. Add a tilde character (~) for each ignored argument
in the function signature.
Ignored arguments cannot have default values or specify class, size, or validation
functions.
The tilde character (~) is treated as an optional input argument unless it is followed by a
required positional argument. For example, in this function the tilde character (~)
represents an optional argument.
function c = f(~)
arguments
~
end
% Function code
end
26-10
Function Argument Validation
c = f
c = f(2)
In the following function, the tilde character (~) represents a required argument.
function c = f(~,x)
arguments
~
X
end
% Function code
...
end
c = f(2,3)
For more information on calling functions with ignored inputs, see “Ignore Function
Inputs” on page 21-13.
Repeating Arguments
Repeating arguments are positional arguments that can be specified repeatedly as input
arguments. Declare repeating arguments in an arguments block that includes the
Repeating attribute.
arguments (Repeating)
arg1
arg2
...
end
Functions can have only one Repeating arguments block, which can contain one or
more repeating arguments.
A function that defines a Repeating arguments block can be called with zero or more
occurrences of all the arguments in the block. If a call to a function includes repeating
arguments, then all arguments in the block must be included for each repetition.
26-11
26 Function Argument Validation
Repeating arguments cannot specify default values to make the argument optional.
However, you can call the function without including the repeating arguments.
Functions must declare repeating arguments after positional arguments and before name-
value arguments. You cannot specify name-value arguments within a Repeating block.
For information on name-value arguments, see “Name-Value Arguments” on page 26-14.
In the function, each repeating argument becomes a cell array containing the same
number of elements as the number of repeats passed in the function call. The validation is
applied to each element of the cell array. If the function is called with zero occurrences of
this argument, the cell array has a size of 1-by-0. That is, it is empty.
For example, this function declares a block of three repeating arguments, x, y, and
options.
% Function code
% Return cell arrays
xCell = x;
yCell = y;
optionCell = option;
end
You can call the function with no inputs or multiples of three inputs. MATLAB creates a
cell array for each argument containing all the values passes for that argument. This call
to fRepeat passes two sets of the three repeating arguments.
[xCell,yCell,optionCell] = fRepeat(1,2,"linear",3,4,"cubic")
xCell =
{[1]} {[3]}
yCell =
26-12
Function Argument Validation
{[2]} {[4]}
optionCell =
{["linear"]} {["cubic"]}
The following function accepts repeating arguments for x and y inputs in a Repeating
arguments block. In the body of the function, the values specified as repeating
arguments are available in the cell arrays x and y. This example interleaves the values of
x and y to match the required input to the plot function: plot(x1,y1,…).
function myPlotRepeating(x,y)
arguments(Repeating)
x (1,:) double
y (1,:) double
end
% Function code
% Interleave x and y
z = reshape([x;y],1,[]);
Using varargin with functions that use argument validation is not recommended. If you
use varargin to support legacy code, it must be the only argument in a Repeating
arguments block.
26-13
26 Function Argument Validation
If varargin is restricted in size or class in the repeating arguments block, then the
restrictions apply to all values in varargin.
For example, this function defines two required positional arguments and varargin as
the repeating argument.
% Function code
...
end
Name-Value Arguments
Name-value arguments associate a name with a value that is passed to the function.
Name-value arguments:
Declare name-value arguments in an arguments block using dot notation to define the
fields of a structure. For example, the structure named NameValueArgs defines two
name-value arguments, Name1 and Name2. You can use any valid MATLAB identifier as
the structure name.
arguments
NameValueArgs.Name1
NameValueArgs.Name2
end
26-14
Function Argument Validation
function myFunction(NameValueArgs)
Call the function using the field names in the name-value structure, passed as a string or
character vector.
myFunction('Name1',value1,'Name2',value2)
The name of the structure used in the function signature is the name of the structure in
the function workspace that contains the names and values passed to the function.
% Function code
result = NameValueArgs.Name1 * NameValueArgs.Name2;
end
r = myFunction('Name1',3,'Name2',7)
r =
21
You can specify a default value for each name. If you do not specify a default value and
the function is called without that name-value argument, then that field is not present in
the name-value structure. If no name-value arguments are passed to the function,
MATLAB creates the structure, but it has no fields.
To determine what name-value arguments have been passed in the function call, use the
isfield function.
For example, the following function defines two required positional arguments (width
and height) and two name-value arguments (LineStyle and LineWidth). In this
example, the options structure has two fields (LineStyle and LineWidth) containing
either the default values or values specified as name-value arguments when the function
is called.
26-15
26 Function Argument Validation
function myRectangle(width,height,options)
arguments
width double
height double
options.LineStyle (1,1) string = "-"
options.LineWidth (1,1) {mustBeNumeric} = 1
end
% Function code
...
end
myRectangle(4,5)
myRectangle(4,5,"LineStyle",":","LineWidth",2)
myRectangle(4,5,"LineWidth",2,"LineStyle",":")
myRectangle(4,5,"LineStyle",":")
myRectangle(4,5,"LineWidth",2)
If the function defines repeating arguments, then you must declare the name-value
arguments in a separate arguments block that follows the repeating arguments block.
For example, this function accepts two repeating arguments, x and y. After specifying all
repeats of x and y, you can specify a name-value pair that assigns the value lin or log to
the PlotType name.
To determine if the function call includes the PlotType argument, use the isfield
function to check for the PlotType field in the scale structure.
function linLog(x,y,scale)
arguments(Repeating)
x (1,:) double
y (1,:) double
end
arguments
scale.PlotType (1,1) string
end
z = reshape([x;y],1,[]);
if isfield(scale,"PlotType")
if scale.PlotType == "lin"
plot(z{:})
elseif scale.PlotType =="log"
26-16
Function Argument Validation
loglog(z{:})
end
end
end
Function argument blocks can contain multiple name-value structures. However, the field
names must be unique among all structures. This function has two name-value structures:
lineOptions and fillOptions. These structures cannot have the same field names.
% Function Code
...
end
26-17
26 Function Argument Validation
defined by a class (that is, all properties with public SetAccess), use the following syntax
in an arguments block.
structName.?ClassName
A function can use the "structName.? ClassName" syntax only once. Therefore, a
function can define only one name-value structure that gets its field names from a class,
even if using different classes and structure names.
If the class places restrictions on values that you can assign to the property by using
property validation, then the function applies the validation to the individual name-value
arguments. For information on property validation, see “Validate Property Values”.
For example, this function has two required arguments, x and y and accepts any public
property name and value for the matlab.graphics.chart.primitive.Bar class.
function myBar(x,y,propArgs)
arguments
x (:,:) double
y (:,:) double
propArgs.?matlab.graphics.chart.primitive.Bar
end
propertyCell = namedargs2cell(propArgs);
bar(x,y,propertyCell{:})
end
Call the function with the required inputs and any settable property name-value pairs.
x = [1,2,3;4,5,6];
y = x.^2;
myBar(x,y)
myBar(x,y,'FaceColor','magenta','BarLayout','grouped')
You can override the class property validation by redefining the property name with a
specific name-value argument in the arguments block.
structName.?ClassName
structName.PropertyName (dim1,dim2,...) ClassName {fcn1,fcn2,...}
The specific name-value argument validation overrides the validation defined by class for
the individually specified property name.
For example, the following function defines name-value arguments as the properties of
the matlab.graphics.chart.primitive.Bar class. Also, it overrides the property
name FaceColor to allow only these specific values: 'red', or 'blue'.
26-18
Function Argument Validation
This function converts the name-value structure to a cell array containing interleaved
names and values using the namedargs2cell function.
function myBar(x,y,propArgs)
arguments
x (:,:) double
y (:,:) double
propArgs.?matlab.graphics.chart.primitive.Bar
propArgs.FaceColor {mustBeMember(propArgs.FaceColor,{'red','blue'})} = "blue"
end
propertyCell = namedargs2cell(propArgs);
bar(x,y,propertyCell{:})
end
Call the function using the two required arguments, x and y. Optionally pass any name-
value pairs supported by the bar function and a value for FaceColor that can be either
red or blue. Other values for FaceColor are not allowed.
x = [1,2,3;4,5,6];
y = x.^2;
myBar(x,y)
myBar(x,y,'FaceColor','red','BarLayout','grouped')
If a classdef file includes method prototypes for methods defined in separate files,
include the arguments blocks in the separate files defining the method.
Subclass methods do not inherit function argument validation. Subclass methods that
override superclass methods should use the same function argument validation in the
subclass method as a good practice.
Handle class destructor methods cannot use argument validation. A method named
delete in a handle subclass that includes an arguments block is not treated as a
26-19
26 Function Argument Validation
destructor (not called by MATLAB when the object is destroyed). For more information on
class destructor methods, see “Handle Class Destructor”.
Validated values can be different from the original values passed as inputs when the
function is called. For example, this function declares the inputs as class uint32 values.
The third input declaration assigns a default value equal to the product of the first two
inputs.
function c = f(a, b,c)
arguments
a uint32
b uint32
c uint32 = a .* b
end
% Function code
...
end
Calling the function with inputs that are a different numeric class (for example, double)
results in a conversion to uint32.
c = f(1.8,1.5)
Because the optional input argument c is not specified in the function call, MATLAB
evaluates the default value and assigns it to c after converting a and b to uint32 values.
In this case, the conversion results in a value of 2 for both inputs. Therefore, the product
of a times b is four.
c =
uint32
26-20
Function Argument Validation
If you specify a value for the third input, then the function assigns a value to c and does
not evaluate the default value expression.
c = f(1.8,1.5,25)
c =
uint32
25
When an argument value that is passed to a function does not match the class and size
required by the validation, MATLAB converts the value to the declared class and size
when conversion is possible. However, there can be situations where conversion is not the
desired behavior. For example, a char or string can be converted to a double, scalar
expansion can change size to satisfy size restrictions, or a column vector can be
converted to a row vector.
For example, this function restricts the first input to a two-dimensional array of any size
that is of class double. The second input must be a 5-by-3 array of any class.
function f(a, b)
arguments
a (:,:) double
b (5,3)
end
% Function code
...
end
26-21
26 Function Argument Validation
Because of standard MATLAB type conversion and scalar expansion, you can call this
function with the following inputs and not receive validation errors. By default, MATLAB
converts the elements of the character vector to their equivalent numeric value and
applies scalar expansion to create a 5-by-3 array from the scalar value 144.
f('character vector',144)
Using specialized validation functions can provide more specific input argument
validation. For example, this function defines two specialized validation functions that it
uses in place of the class and size specifications for the first and second arguments. These
local functions enable you to avoid input value conversions.
function fCustomValidators(a, b)
arguments
a {mustBeA(a,'double'), mustBeDims(a,2)}
b {mustBeSize(b,[5,3])}
end
% Function code
...
end
function mustBeSize(input,sizeDims)
% Test for specific size
if ~isequal(size(input),sizeDims)
error(['Input must be of size ',num2str(sizeDims)])
end
end
function mustBeDims(input,numDims)
% Test for number of dimensions
if ~isequal(length(size(input)),numDims)
error(['Input must have ',num2str(numDims),' dimensions.'])
end
end
26-22
Function Argument Validation
The mustBeSize and mustBeDims validation functions enforce the strict declarations for
the input arguments.
fCustomValidators('character vector',144)
In this call, the number of dimensions of the first input are wrong and the validation
function returns a custom error message.
fCustomValidators(ones(2,2,4),144)
The mustBeSize validator function restricts the second input to a specific dimension,
which is provided in the error message.
fCustomValidators(ones(2,2),144)
Repeating arguments are positional arguments and therefore the number of repeating
arguments passed to the function when called is included in the value returned by
nargin.
Use nargin to determine if optional positional arguments are passed to the function
when called. For example, this function declares three positional arguments and a name-
value argument. Here is how the function determines what arguments are passed when it
is called.
• nargin determines if the optional positional argument c is passed to the function with
a switch block.
• isfield determines if the name-value argument for Format is passed to the function.
function result = fNargin(a, b, c, namedargs)
arguments
26-23
26 Function Argument Validation
a (1,1) double
b (1,1) double
c (1,1) double = 1
namedargs.Format (1,:) char
end
% Function code
switch nargin
case 2
result = a + b;
case 3
result = a^c + b^c;
end
if isfield(namedargs,'Format')
format(namedargs.Format);
end
end
result = fNargin(3,4)
result =
result = fNargin(3,4,7.62)
result =
4.3021e+04
result = fNargin(3,4,7.62,'Format','bank')
result =
43020.56
26-24
Function Argument Validation
The only variables visible to validator functions and default value expressions are the
input variables already declared. In this function, the default value of c is derived from a
and b.
function c = f(a,b,c)
arguments
a uint32
b uint32
c uint32 = a * b
end
% Function code
...
end
However, you cannot refer to input variables not yet declared in an arguments block. For
example, using this declaration for argument a in the previous function is not valid
because b and c have not been declared yet.
arguments
a uint32 = b * c
b uint32
c uint32
end
Argument validation expressions can reference only previously declared, and therefore
validated, arguments.
Validation functions and default values for name-value arguments cannot access other
name-value arguments.
Any references to previously declared arguments must be visible in the text of validation
functions and default values. To ensure code transparency, do not use functions that
26-25
26 Function Argument Validation
interact with the function workspace. Specifically, do not use nested functions or any of
the functions listed in the following table in the arguments block.
These restrictions apply only within the arguments block and do not apply to variables or
functions in the body of the function.
See Also
arguments | namedargs2cell
Related Examples
• “Input and Output Arguments”
• “Argument Validation Functions” on page 26-27
• “Validate Property Values”
26-26
Argument Validation Functions
26-27
26 Function Argument Validation
Creating your own validation function is useful when you want to provide specific
validation that is not available using the MATLAB validation functions. You can create
them as local functions within the function file or place them on the MATLAB path. To
26-28
See Also
avoid a confluence of error messages, do not use function argument validation within
user-defined validation functions.
For example, the ImgData function uses a local function to define a validator that
restricts the Data input to a specific range of numeric values. The mustBeNumeric
validator is an existing function.
function ImgData(Data)
arguments
Data {mustBeNumeric, mustBeInRange(Data,[0,255])}
end
% Function code
imagesc(Data)
end
The Data input argument can be a numeric array of any size with values in the range 0–
255. If values do not satisfy these requirements, MATLAB throws an error.
ImgData(randi(300,10,10))
Error using ImgData
Invalid input argument at position 1. Value assigned to Data is not in range 0...255
See Also
More About
• “Function Argument Validation” on page 26-2
26-29
26 Function Argument Validation
Function input arguments are variables in the function workspace whose values come
from the calling code or command-line users. When functions are widely used by others, it
is important to check that the input values match the values expected by the code in the
function.
Argument checking allows the function to provide more useful information when input
values are unexpected and the function is unable to perform as intended. MATLAB
provides several solutions to simplify the process of checking and processing function
inputs.
An effective way to implement these common patterns is to declare the arguments using a
function arguments block, as described in “Function Argument Validation” on page 26-2.
This syntax is new as of release R2019b and does not work in earlier releases.
validateattributes
The validateattributes function enables you to verify that the inputs to a function
conform to a set of requirements. Call validateattributes for each input argument
with parameters specifying argument requirements.
26-30
See Also
inputParser
For complex function signatures, an inputParser object can be used programmatically
to express requirements on input arguments. An input parser parses and validates a set of
inputs.
See Also
arguments | inputParser | validateattributes
More About
• “Function Argument Validation” on page 26-2
• “Check Function Inputs with validateattributes” on page 21-14
• “Parse Function Inputs” on page 21-17
26-31
26 Function Argument Validation
X = zeros(1,10);
for ii = 1:10
X(ii) = randi(9,1);
end
However, in the following call to the eval function, MATLAB cannot recognize the
variables in the statement that is passed to eval because the input is a character string.
X = zeros(1,10);
for ii = 1:10
eval('X(ii) = randi(9,1);')
end
Before executing this code, MATLAB sees a call to the eval function with one argument,
which is the character vector 'X(ii) = randi(9,1);'.
To be transparent, code must refer to variable names explicitly so that MATLAB can
identify the variables by inspection. Using the eval function with the character vector
'X(ii) = randi(9,1);' means that MATLAB must execute the code before being able
to identify X and ii.
26-32
Transparency in MATLAB Code
Here is a partial list of functions and coding that you cannot use with transparent variable
access.
Passing a variable to a function using the command form is not transparent because it is
equivalent to passing the argument as a character string. For example, these calls to the
clear function are both nontransparent.
clear X
clear('X')
If code creates workspace variables, but MATLAB can identify these new variables only
after executing the code, then this code does not have transparent variable access.
For example, MATLAB cannot determine what variables are loaded from the MAT file, so
this statement is nontransparent.
load foo.mat
However, code that assigns the loaded variable to a name explicitly is transparent
because MATLAB can recognize that the name on the left-hand side refers to a workspace
variable. For example, this statement loads the variable X from the MAT file into the
workspace in a variable named X.
X = load('foo.mat','X');
Access to variables must be transparent within the workspace. For example, code cannot
use the evalin, or assignin functions to create variables in another workspace from a
workspace that requires transparency.
26-33
26 Function Argument Validation
See Also
More About
• “Function Argument Validation” on page 26-2
• “Variables”
26-34
Software Development
35
27
Error Handling
Overview
No matter how carefully you plan and test the programs you write, they may not always
run as smoothly as expected when executed under different conditions. It is always a
good idea to include error checking in programs to ensure reliable operation under all
conditions.
In the MATLAB software, you can decide how your programs respond to different types of
errors. You may want to prompt the user for more input, display extended error or
warning information, or perhaps repeat a calculation using default values. The error-
handling capabilities in MATLAB help your programs check for particular error conditions
and execute the appropriate code depending on the situation.
When MATLAB detects a severe fault in the command or program it is running, it collects
information about what was happening at the time of the error, displays a message to help
the user understand what went wrong, and terminates the command or program. This is
called throwing an exception. You can get an exception while entering commands at the
MATLAB command prompt or while executing your program code.
Evaluate the error message MATLAB has displayed. Most error messages attempt to
explain at least the immediate cause of the program failure. There is often sufficient
information to determine the cause and what you need to do to remedy the situation.
27-2
Exception Handling in a MATLAB Application
If the function in which the error occurred is implemented as a MATLAB program file, the
error message should include a line that looks something like this:
surf
The text includes the name of the function that threw the error (surf, in this case) and
shows the failing line number within that function's program file. Click the line number;
MATLAB opens the file and positions the cursor at the location in the file where the error
originated. You may be able to determine the cause of the error by examining this line
and the code that precedes it.
You can use the MATLAB Debugger to step through the failing code. Click the underlined
error text to open the file in the MATLAB Editor at or near the point of the error. Next,
click the hyphen at the beginning of that line to set a breakpoint at that location. When
you rerun your program, MATLAB pauses execution at the breakpoint and enables you to
step through the program code. The command dbstop on error is also helpful in
finding the point of error.
See the documentation on “Debug a MATLAB Program” on page 22-2 for more
information.
Some of the things you might want to do in the catch block are:
27-3
27 Error Handling
When you reach the end of the catch block, you can either continue executing the
program, if possible, or terminate it.
• Saves information about what went wrong and what code was executing at the time of
the error.
• Gathers any other pertinent information about the error.
• Instructs MATLAB to throw the exception.
27-4
Capture Information About Exceptions
In this section...
“Overview” on page 27-5
“The MException Class” on page 27-5
“Properties of the MException Class” on page 27-6
“Methods of the MException Class” on page 27-14
Overview
When MATLAB throws an exception, it captures information about the cause in a data
structure called an MException object. This object is an instance of the MATLAB
MException class. You can access the MException object by catching the exception
before your program aborts and accessing the object constructed for this particular error
via the catch command. When throwing an exception in response to an error in your own
code, you will have to create a new MException object and store information about the
error in that object.
This section describes the MException class and objects constructed from that class.
See “Properties of the MException Class” on page 27-6 for a full description of these
properties.
27-5
27 Error Handling
Object Constructor
Any code that detects an error and throws an exception must also construct an
MException object in which to record and transfer information about the error. The
syntax of the MException constructor is
ME = MException(identifier, message)
that is enclosed in single quotes, and message is text, also enclosed in single quotes, that
describes the error. The output ME is the resulting MException object.
If you are responding to an exception rather than throwing one, you do not have to
construct an MException object. The object has already been constructed and populated
by the code that originally detected the error.
27-6
Capture Information About Exceptions
Exception” on page 27-19 and “Throw an Exception” on page 27-16. All are read-only;
their values cannot be changed.
If you call the surf function with no inputs, MATLAB throws an exception. If you catch
the exception, you can see the properties of the MException object structure. (This
example uses try/catch in an atypical fashion. See the section on “The try/catch
Statement” on page 27-19 for more information on using try/catch).
try
surf
catch ME
ME
end
Run this at the command line and MATLAB returns the contents of the MException
object:
ME =
identifier: 'MATLAB:narginchk:notEnoughInputs'
message: 'Not enough input arguments.'
cause: {}
stack: [1×1 struct]
Correction: []
The stack field shows the filename, function, and line number where the exception was
thrown:
ME.stack
ans =
file: 'matlabroot\toolbox\matlab\graph3d\surf.m'
name: 'surf'
line: 54
27-7
27 Error Handling
The cause and Correction fields are empty in this case. Each field is described in more
detail in the sections that follow.
Message Identifiers
A message identifier is a tag that you attach to an error or warning statement that makes
that error or warning uniquely recognizable by MATLAB. You can use message identifiers
with error reporting to better identify the source of an error, or with warnings to control
any selected subset of the warnings in your programs.
The message identifier is a read-only character vector that specifies a component and a
mnemonic label for an error or warning. The format of a simple identifier is
component:mnemonic
A colon separates the two parts of the identifier: component and mnemonic. If the
identifier uses more than one component, then additional colons are required to separate
them. A message identifier must always contain at least one colon.
MATLAB:rmpath:DirNotFound
MATLAB:odearguments:InconsistentDataType
Simulink:actionNotTaken
TechCorp:OpenFile:notFoundInPath
Both the component and mnemonic fields must adhere to the following syntax rules:
There is no length limitation to either the component or mnemonic. The identifier can
also be an empty character vector.
Component Field
The component field specifies a broad category under which various errors and warnings
can be generated. Common components are a particular product or toolbox name, such as
MATLAB or Control, or perhaps the name of your company, such as TechCorp in the
preceding example.
27-8
Capture Information About Exceptions
You can also use this field to specify a multilevel component. The following statement has
a three-level component followed by a mnemonic label:
TechCorp:TestEquipDiv:Waveform:obsoleteSyntax
The component field enables you to guarantee the uniqueness of each identifier. Thus,
while the internal MATLAB code might use a certain warning identifier like
MATLAB:InconsistentDataType, that does not preclude you from using the same
mnemonic, as long as you precede it with a unique component. For example,
warning('TechCorp:InconsistentDataType', ...
'Value %s is inconsistent with existing properties.' ...
sprocketDiam)
Mnemonic Field
The mnemonic field is normally used as a tag relating to the particular message. For
example, when reporting an error resulting from the use of ambiguous syntax, a simple
component and mnemonic such as the following might be appropriate:
MATLAB:ambiguousSyntax
For example,
ME = MException('AcctError:NoClient', ...
'Client name not recognized.');
ME.identifier
ans =
AcctError:NoClient
When responding to an exception, you can extract the message identifier from the
MException object as shown here. Using the surf example again,
try
surf
catch ME
id = ME.identifier
27-9
27 Error Handling
end
id =
MATLAB:narginchk:notEnoughInputs
An error message in MATLAB is a read-only character vector issued by the program code
and returned in the MException object. This message can assist the user in determining
the cause, and possibly the remedy, of the failure.
When throwing an exception, compose an appropriate error message and save it to the
MException object at the time you construct the object using the syntax
ME = MException(identifier, text)
If your message requires formatting specifications, like those available with the sprintf
function, use this syntax for the MException constructor:
For example,
S = 'Accounts'; f1 = 'ClientName';
ME = MException('AcctError:Incomplete', ...
'Field ''%s.%s'' is not defined.', S, f1);
ME.message
ans =
Field 'Accounts.ClientName' is not defined.
When responding to an exception, you can extract the error message from the
MException object as follows:
try
surf
catch ME
msg = ME.message
end
msg =
Not enough input arguments.
27-10
Capture Information About Exceptions
The stack field of the MException object identifies the line number, function, and
filename where the error was detected. If the error occurs in a called function, as in the
following example, the stack field contains the line number, function name, and filename
not only for the location of the immediate error, but also for each of the calling functions.
In this case, stack is an N-by-1 array, where N represents the depth of the call stack.
That is, the stack field displays the function name and line number where the exception
occurred, the name and line number of the caller, the caller's caller, etc., until the top-
most function is reached.
When throwing an exception, MATLAB stores call stack information in the stack field.
You cannot write to this field; access is read-only.
For example, suppose you have three functions that reside in two separate files:
mfileA.m
=========================
.
.
42 function A1(x, y)
43 B1(x, y);
mfileB.m
=========================
.
.
8 function B1(x, y)
9 B2(x, y)
.
.
26 function B2(x, y)
27 .
28 .
29 .
30 .
31 % Throw exception here
Catch the exception in variable ME and then examine the stack field:
for k=1:length(ME.stack)
ME.stack(k)
27-11
27 Error Handling
end
ans =
file: 'C:\matlab\test\mfileB.m'
name: 'B2'
line: 31
ans =
file: 'C:\matlab\test\mfileB.m'
name: 'B1'
line: 9
ans =
file: 'C:\matlab\test\mfileA.m'
name: 'A1'
line: 43
In some situations, it can be important to record information about not only the one
command that caused execution to stop, but also other exceptions that your code caught.
You can save these additional MException objects in the cause field of the primary
exception.
This example attempts to assign an array D to variable X. If the D array does not exist, the
code attempts to load it from a MAT-file and then retries assigning it to X. If the load fails,
a new MException object (ME3) is constructed to store the cause of the first two errors
(ME1 and ME2):
try
X = D(1:25)
catch ME1
try
filename = 'test200';
load(filename);
X = D(1:25)
catch ME2
ME3 = MException('MATLAB:LoadErr', ...
'Unable to load from file %s', filename);
ME3 = addCause(ME3, ME1);
ME3 = addCause(ME3, ME2);
27-12
Capture Information About Exceptions
end
end
ME3.cause
ans =
[1x1 MException]
[1x1 MException]
ME3.cause{:}
ans =
identifier: 'MATLAB:UndefinedFunction'
message: 'Undefined function or method 'D' for input
arguments of type 'double'.'
stack: [0x1 struct]
cause: {}
ans =
identifier: 'MATLAB:load:couldNotReadFile'
message: 'Unable to read file test204: No such file or
directory.'
stack: [0x1 struct]
cause: {}
The Correction
Some exceptions may have a fix that can be suggested when the exception occurs. If you
are throwing an exception, you can suggest a fix by creating a
matlab.lang.correction.AppendArgumentsCorrection,
matlab.lang.correction.ConvertToFunctionNotationCorrection, or
matlab.lang.correction.ReplaceIdentifierCorrection object and adding it to
the Correction field of the exception.
You must use the following syntax when adding a fix to the Correction field:
primaryException = addCorrection(baseException, exceptionCorrection)
27-13
27 Error Handling
This example creates a function hello that requires one input argument. If the function
is called without inputs, MATLAB produces an error and suggests the input argument
"world" as a fix.
function hello(audience)
if nargin < 1
me = MException('MATLAB:notEnoughInputs', 'Not enough input arguments.');
aac = matlab.lang.correction.AppendArgumentsCorrection('"world"');
me = addCorrection(me, aac);
throw(me)
end
fprintf("Hello, %s!\n", audience)
end
When you call the function without an argument, MATLAB suggests a fix.
hello
27-14
Capture Information About Exceptions
27-15
27 Error Handling
Throw an Exception
When your program detects a fault that will keep it from completing as expected or will
generate erroneous results, you should halt further execution and report the error by
throwing an exception. The basic steps to take are:
1 Detect the error. This is often done with some type of conditional statement, such as
an if or try/catch statement that checks the output of the current operation.
2 Construct an MException object to represent the error. Add a message identifier and
error message to the object when calling the constructor.
3 If there are other exceptions that may have contributed to the current error, you can
store the MException object for each in the cause field of a single MException
that you intend to throw. Use the addCause function for this.
4 If there is fix that can be suggested for the current error, you can add it to the
Correction field of the MException that you intend to throw. Use the
addCorrection function for this.
5 Use the throw or throwAsCaller function to have MATLAB issue the exception. At
this point, MATLAB stores call stack information in the stack field of the
MException, exits the currently running function, and returns control to either the
keyboard or an enclosing catch block in a calling function.
Create a function, indexIntoArray, that indexes into a specified array using a specified
index. The function catches any errors that MATLAB throws and creates an exception that
provides general information about the error. When it catches an error, it detects whether
the error involves the number of inputs or the specified index. If it does, the function adds
additional exceptions with more detailed information about the source of the failure, and
suggests corrections when possible.
function indexIntoArray(A, idx)
27-16
Throw an Exception
throw(baseException);
end
try
assert(isnumeric(idx),'MYFUN:notNumeric', ...
'Indexing array is not numeric.')
catch causeException
baseException = addCause(baseException,causeException);
end
If you call the function without specifying an index, the function throws a detailed error
and suggests a correction:
Caused by:
Not enough input arguments.
If you call the function with a nonnumeric index array that is too large, the function
throws a detailed error.
27-17
27 Error Handling
Caused by:
Error using indexIntoArray (line 25)
Indexing array is not numeric.
Indexing array is too large.
27-18
Respond to an Exception
Respond to an Exception
In this section...
“Overview” on page 27-19
“The try/catch Statement” on page 27-19
“Suggestions on How to Handle an Exception” on page 27-21
Overview
The MATLAB software, by default, terminates the currently running program when an
exception is thrown. If you catch the exception in your program, however, you can
capture information about what went wrong and deal with the situation in a way that is
appropriate for the particular condition. This requires a try/catch statement.
A try/catch statement looks something like the following pseudocode. It consists of two
parts:
• A try block that includes all lines between the try and catch statements.
• A catch block that includes all lines of code between the catch and end statements.
try
Perform one ...
or more operations
A catch ME
Examine error info in exception object ME
Attempt to figure out what went wrong
Either attempt to recover, or clean up and abort
end
B Program continues
The program executes the statements in the try block. If it encounters an error, it skips
any remaining statements in the try block and jumps to the start of the catch block
27-19
27 Error Handling
(shown here as point A). If all operations in the try block succeed, then execution skips
the catch block entirely and goes to the first line following the end statement (point B).
Specifying the try, catch, and end commands and also the code of the try and catch
blocks on separate lines is recommended. If you combine any of these components on the
same line, separate them with commas:
Note You cannot define nested functions within a try or catch block.
On execution, your code enters the try block and executes each statement as if it were
part of the regular program. If no errors are encountered, MATLAB skips the catch block
entirely and continues execution following the end statement. If any of the try
statements fail, MATLAB immediately exits the try block, leaving any remaining
statements in that block unexecuted, and enters the catch block.
The catch command marks the start of a catch block and provides access to a data
structure that contains information about what caused the exception. This is shown as the
variable ME in the preceding pseudocode. This data structure is an object of the MATLAB
MException class. When an exception occurs, MATLAB constructs an instance of this
class and returns it in the catch statement that handles that error.
You are not required to specify any argument with the catch statement. If you do not
need any of the information or methods provided by the MException object, just specify
the catch keyword alone.
The MException object is constructed by internal code in the program that fails. The
object has properties that contain information about the error that can be useful in
determining what happened and how to proceed. The MException object also provides
access to methods that enable you to respond to the exception. See the section on “The
MException Class” on page 27-5 to find out more about the MException class.
27-20
Respond to an Exception
Having entered the catch block, MATLAB executes the statements in sequence. These
statements can attempt to
The catch block often ends with a rethrow command. The rethrow causes MATLAB to
exit the current function, keeping the call stack information as it was when the exception
was first thrown. If this function is at the highest level, that is, it was not called by
another function, the program terminates. If the failing function was called by another
function, it returns to that function. Program execution continues to return to higher level
functions, unless any of these calls were made within a higher-level try block, in which
case the program executes the respective catch block.
More information about the MException class is provided in the section “Capture
Information About Exceptions” on page 27-5.
• The first if statement checks whether the function is called with an input argument. If
no input argument is specified, the program throws an error and suggests an input
argument to correct the error.
• The try block attempts to open and read the file. If either the open or the read fails,
the program catches the resulting exception and saves the MException object in the
variable ME1.
• The catch block checks to see if the specified file could not be found. If so, the
program allows for the possibility that a common variation of the filename extension
(e.g., jpeg instead of jpg) was used, by retrying the operation with a modified
extension. This is done using a try/catch statement nested within the original try/
catch.
function d_in = read_image(filename)
27-21
27 Error Handling
% Did the read fail because the file could not be found?
if strcmp(idSegLast,'InvalidFid') && ...
~exist(filename,'file')
This example illustrates some of the actions that you can take in response to an
exception.
• Compare the identifier field of the MException object to possible causes of the
error. In this case, the function checks whether the identifier ends in 'InvalidFid',
indicating a file could not be found.
• Use a nested try/catch statement to retry the operation with improved input. In this
case, the function retries the open and read operations using a known variation of the
filename extension.
27-22
Respond to an Exception
Cleaning up any unwanted results of the error is also advisable. For example, close
figures that remained open after the error occurred.
27-23
27 Error Handling
Overview
A good programming practice is to make sure that you leave your program environment
in a clean state that does not interfere with any other program code. For example, you
might want to
MATLAB provides the onCleanup function for this purpose. This function, when used
within any program, establishes a cleanup routine for that function. When the function
terminates, whether normally or in the event of an error or Ctrl+C, MATLAB
automatically executes the cleanup routine.
The following statement establishes a cleanup routine cleanupFun for the currently
running program:
cleanupObj = onCleanup(@cleanupFun);
When your program exits, MATLAB finds any instances of the onCleanup class and
executes the associated function handles. The process of generating and activating
function cleanup involves the following steps:
1 Write one or more cleanup routines for the program under development. Assume for
now that it takes only one such routine.
27-24
Clean Up When Functions Complete
You can declare any number of cleanup routines for a program file. Each call to
onCleanup establishes a separate cleanup routine for each cleanup object returned.
If, for some reason, the object returned by onCleanup persists beyond the life of your
program, then the cleanup routine associated with that object is not run when your
function terminates. Instead, it will run whenever the object is destroyed (e.g., by clearing
the object variable).
Your cleanup routine should never rely on variables that are defined outside of that
routine. For example, the nested function shown here on the left executes with no error,
whereas the very similar one on the right fails with the error, Undefined function or
variable 'k'. This results from the cleanup routine's reliance on variable k which is
defined outside of the nested cleanup routine:
function testCleanup function testCleanup
k = 3; k = 3;
myFun obj = onCleanup(@myFun);
function myFun function myFun
fprintf('k is %d\n', k) fprintf('k is %d\n', k)
end end
end end
MATLAB closes the file with identifier fid when function openFileSafely terminates:
function openFileSafely(fileName)
fid = fopen(fileName, 'r');
27-25
27 Error Handling
c = onCleanup(@()fclose(fid));
s = fread(fid);
.
.
.
end
functionThatMayError;
end % c executes cd(currentFolder) here.
This example extends the MATLAB path to include files in the toolbox\images folders, and
then displays a figure from one of these folders. After the figure displays, the cleanup
routine restore_env closes the figure and restores the path to its original state.
function showImageOutsidePath(imageFile)
fig1 = figure;
imgpath = genpath([matlabroot '\toolbox\images']);
27-26
Clean Up When Functions Complete
pause(2)
Run the function as shown here. You can verify that the path has been restored by
comparing the length of the path before and after running the function:
origLen = length(path);
showImageOutsidePath('greens.jpg')
Opening the figure greens.jpg
Closing the figure
Restoring the path
currLen = length(path);
currLen == origLen
ans =
1
The details of that handle are then contained within the object returned by the
onCleanup function:
cleanupObj = onCleanup(@()restore_env(fig1, imgpath));
You can access these details using the task property of the cleanup object as shown here.
(Modify the showImageOutsidePath function by adding the following code just before
the comment line that says, “% This is the cleanup routine.”)
disp ' Displaying information from the function handle:'
task = cleanupObj.task;
fun = functions(task)
wsp = fun.workspace{2,1}
fprintf('\n');
pause(2);
27-27
27 Error Handling
Run the modified function to see the output of the functions command and the contents
of one of the workspace cells:
showImageOutsidePath('greens.jpg')
The following program cleans up if an error occurs, but not in response to Ctrl+C:
function cleanupByCatch
try
pause(10);
catch
disp(' Collecting information about the error')
disp(' Executing cleanup tasks')
end
Unlike the try/catch statement, the onCleanup function responds not only to a normal
exit from your program and any error that might be thrown, but also to Ctrl+C. This next
example replaces the try/catch with onCleanup:
27-28
Clean Up When Functions Complete
function cleanupByFunc
obj = onCleanup(@()...
disp(' Executing cleanup tasks'));
pause(10);
onCleanup in Scripts
onCleanup does not work in scripts as it does in functions. In functions, the cleanup
object is stored in the function workspace. When the function exits, this workspace is
cleared thus executing the associated cleanup routine. In scripts, the cleanup object is
stored in the base workspace (that is, the workspace used in interactive work done at the
command prompt). Because exiting a script has no effect on the base workspace, the
cleanup object is not cleared and the routine associated with that object does not execute.
To use this type of cleanup mechanism in a script, you would have to explicitly clear the
object from the command line or another script when the first script terminates.
27-29
27 Error Handling
Issue Warnings
You can issue a warning to flag unexpected conditions detected when running a program.
The warning function prints a warning message to the command line. Warnings differ
from errors in two significant ways:
Use the warning function in your code to generate a warning message during execution.
Specify the message as the input argument to the warning function:
warning('Input must be text')
For example, you can insert a warning in your code to verify the software version:
function warningExample1
if ~strncmp(version, '7', 1)
warning('You are using a version other than v7')
end
Throw Errors
You can throw an error to flag fatal problems within the program. Use the error function
to print error messages to the command line. After displaying the message, MATLAB
stops the execution of the current program.
For example, suppose you construct a function that returns the number of combinations
of k elements from n elements. Such a function is nonsensical if k > n; you cannot
choose 8 elements if you start with just 4. You must incorporate this fact into the function
to let anyone using combinations know of the problem:
27-30
Issue Warnings and Errors
combinations(4,8)
For example, this warning uses %s and %d to mark where to insert the values of variables
arrayname and arraydims:
If you execute this command with arrayname = 'A' and arraydims = 3, MATLAB
responds:
Adding run-time parameters to your warnings and errors can clarify the problems within
a program. Consider the function combinations from “Throw Errors” on page 27-30.
You can throw a much more informative error using run-time parameters:
27-31
27 Error Handling
If this function receives invalid arguments, MATLAB throws an error message and stops
the program:
combinations(6,9)
Enable or disable warnings with identifiers. Use an identifying text argument with the
warning function to attach a unique tag to a message:
warning(identifier_text,message_text)
For example, you can add an identifier tag to the previous MATLAB warning about which
version of software is running:
minver = '7';
if ~strncmp(version,minver,1)
warning('MYTEST:VERCHK','Running a version other than v%s',minver)
end
Adding an identifier to an error message allows for negative testing. However, adding and
recovering more information from errors often requires working with MException
objects.
See Also
MException | lastwarn | warndlg | warning
Related Examples
• “Suppress Warnings” on page 27-34
• “Restore Warnings” on page 27-37
• “Capture Information About Exceptions” on page 27-5
• “Exception Handling in a MATLAB Application” on page 27-2
27-32
See Also
More About
• “Message Identifiers” on page 27-8
27-33
27 Error Handling
Suppress Warnings
Your program might issue warnings that do not always adversely affect execution. To
avoid confusion, you can hide warning messages during execution by changing their
states from 'on' to 'off'.
To suppress specific warning messages, you must first find the warning identifier. Each
warning message has a unique identifier. To find the identifier associated with a MATLAB
warning, reproduce the warning. For example, this code reproduces a warning thrown if
MATLAB attempts to remove a nonexistent folder:
rmpath('folderthatisnotonpath')
Note If this statement does not produce a warning message, use the following code to
temporarily enable the display of all warnings, and then restore the original warning
state:
w = warning ('on','all');
rmpath('folderthatisnotonpath')
warning(w)
To obtain information about the most recently issued warning, use the warning or
lastwarn functions. This code uses the query state to return a data structure containing
the message identifier and the current state of the last warning:
w = warning('query','last')
w =
identifier: 'MATLAB:rmpath:DirNotFound'
state: 'on'
27-34
Suppress Warnings
Using the lastwarn function, you can retrieve the last warning message, regardless of
its display state:
lastwarn
ans =
Continuing the example from the previous section, turn the warning
'MATLAB:rmpath:DirNotFound' off, and repeat the operation.
warning('off',id)
rmpath('folderthatisnotonpath')
warning('on',id)
rmpath('folderthatisnotonpath')
Tip Turn off the most recently invoked warning with warning('off','last').
The term all refers only to those warnings that have been issued or modified during your
current MATLAB session. Modified warning states persist only through the current
session. Starting a new session restores the default settings.
Use the identifier 'all' to represent the group of all warnings. View the state of all
warnings with either syntax:
27-35
27 Error Handling
warning('query','all')
warning
warning('on','all')
warning('query','all')
To disable all warnings and verify the state, use this syntax:
warning('off','all')
warning
See Also
Related Examples
• “Restore Warnings” on page 27-37
• “Change How Warnings Display” on page 27-40
27-36
Restore Warnings
Restore Warnings
MATLAB allows you to save the on-off warning states, modify warning states, and
restore the original warning states. This is useful if you need to temporarily turn off some
warnings and later reinstate the original settings.
The following statement saves the current state of all warnings in the structure array
called orig_state:
orig_state = warning;
To restore the original state after any warning modifications, use this syntax:
warning(orig_state);
You also can save the current state and toggle warnings in a single command. For
example, the statement, orig_state = warning('off','all'); is equivalent to the
commands:
orig_state = warning;
warning('off','all')
warning('query','Control:parameterNotSymmetric')
orig_state = warning('off','Control:parameterNotSymmetric')
orig_state =
identifier: 'Control:parameterNotSymmetric'
state: 'on'
27-37
27 Error Handling
warning
The default warning state is 'on'. Warnings not set to the default are
off Control:parameterNotSymmetric
warning(orig_state)
warning('query','Control:parameterNotSymmetric')
w(1) = warning('off','MATLAB:rmpath:DirNotFound');
w(2) = warning('off','MATLAB:singularMatrix');
w(3) = warning('off','Control:parameterNotSymmetric');
warning
The default warning state is 'on'. Warnings not set to the default are
off Control:parameterNotSymmetric
off MATLAB:rmpath:DirNotFound
off MATLAB:singularMatrix
2 Restore the three warnings to their the original state, and query all warnings:
warning(w)
warning
You do not need to store information about the previous warning states in an array,
but doing so allows you to restore warnings with one command.
27-38
See Also
orig_state = warning('on','all');
2 Restore your warnings to the previous state:
warning(orig_state)
See Also
onCleanup | warning
Related Examples
• “Suppress Warnings” on page 27-34
• “Clean Up When Functions Complete” on page 27-24
27-39
27 Error Handling
• prev_state does not contain information about the backtrace or verbose modes
in the statement, prev_state = warning('query','all').
• A mode change affects all enabled warnings.
For example, you can turn on all warnings, disable backtrace, and enable verbose
warnings:
warning on all
warning off backtrace
warning on verbose
27-40
Change How Warnings Display
the file name and line number where the warning occurred. For example, you can enable
backtrace and disable verbose:
warning on backtrace
warning off verbose
Running a command that produces an error displays a hyperlink with a line number:
27-41
27 Error Handling
try
try block...
catch
catch block...
end
If an error occurs within the try block, MATLAB skips any remaining commands in the
try block and executes the commands in the catch block. If no error occurs within
try block, MATLAB skips the entire catch block.
For example, a try/catch statement can prevent the need to throw errors. Consider the
combinations function that returns the number of combinations of k elements from n
elements:
MATLAB throws an error whenever k > n. You cannot construct a set with more
elements, k, than elements you possess, n. Using a try/catch statement, you can avoid the
error and execute this function regardless of the order of inputs:
27-42
See Also
C1 = robust_combine(8,4)
C2 = robust_combine(4,8)
C1 =
70
C2 =
70
Optionally, you can capture more information about errors if a variable follows your
catch statement:
catch MExc
MExc is an MException class object that contains more information about the thrown
error. To learn more about accessing information from MException objects, see
“Exception Handling in a MATLAB Application” on page 27-2.
See Also
MException | onCleanup
27-43
28
Program Scheduling
Overview
The MATLAB software includes a timer object that you can use to schedule the execution
of MATLAB commands. This section describes how you can create timer objects, start a
timer running, and specify the processing that you want performed when a timer fires. A
timer is said to fire when the amount of time specified by the timer object elapses and the
timer object executes the commands you specify.
You use timer object properties to specify this information. To learn about all the
properties supported by the timer object, see timer and set. You can also set timer
object properties when you create them, in step 1.
3 Start the timer object.
After you create the timer object, you must start it, using either the start or
startat function.
4 Delete the timer object when you are done with it.
After you are finished using a timer object, you should delete it from memory. See
delete for more information.
Note The specified execution time and the actual execution of a timer can vary because
timer objects work in the MATLAB single-threaded execution environment. The length of
this time lag is dependent on what other processing MATLAB is performing. To force the
execution of the callback functions in the event queue, include a call to the drawnow
function in your code. The drawnow function flushes the event queue.
28-2
See Also
After creating the timer object, the example uses the start function to start the timer
object. (The additional commands in this example are included to illustrate the timer but
are not required for timer operation.)
t = timer('TimerFcn', 'stat=false; disp(''Timer!'')',...
'StartDelay',10);
start(t)
stat=true;
while(stat==true)
disp('.')
pause(1)
end
See Also
timer
28-3
28 Program Scheduling
More About
• “Timer Callback Functions” on page 28-5
• “Handling Timer Queuing Conflicts” on page 28-10
28-4
Timer Callback Functions
Note Callback function execution might be delayed if the callback involves a CPU-
intensive task such as updating a figure.
The following diagram shows when the events occur during execution of a timer object
and give the names of the timer object properties associated with each event. For
example, to associate MATLAB commands with a start event, assign a value to the
StartFcn callback property. Error callbacks can occur at any time.
28-5
28 Program Scheduling
This example creates a timer object that displays a greeting after 5 seconds. The example
specifies the value of the TimerFcn callback property directly, putting the commands in a
character vector.
28-6
Timer Callback Functions
t = timer('TimerFcn',@(x,y)disp('Hello World!'),'StartDelay',5);
Note When you specify the callback commands directly as the value of the callback
function property, the commands are evaluated in the MATLAB workspace.
Instead of specifying MATLAB commands directly as the value of a callback property, you
can put the commands in a MATLAB program file and specify the file as the value of the
callback property.
When you create a callback function, the first two arguments must be a handle to the
timer object and an event structure. An event structure contains two fields: Type and
Data. The Type field contains a character vector that identifies the type of event that
caused the callback. The value of this field can be any of the following: 'StartFcn',
'StopFcn', 'TimerFcn', or 'ErrorFcn'. The Data field contains the time the event
occurred.
In addition to these two required input arguments, your callback function can accept
application-specific arguments. To receive these input arguments, you must use a cell
array when specifying the name of the function as the value of a callback property. For
more information, see “Specifying the Value of Callback Function Properties” on page 28-
8.
This example implements a simple callback function that displays the type of event that
triggered the callback and the time the callback occurred. To illustrate passing
application-specific arguments, the example callback function accepts as an additional
argument a character vector and includes this text in the display output. To see this
function used with a callback property, see “Specifying the Value of Callback Function
Properties” on page 28-8.
function my_callback_fcn(obj, event, text_arg)
event_type = event.Type;
event_time = datestr(event.Data.time);
28-7
28 Program Scheduling
The following table shows the syntax for several sample callback functions and describes
how you call them.
This example illustrates several ways you can specify the value of timer object callback
function properties, some with arguments and some without. To see the code of the
callback function, my_callback_fcn, see “Example: Writing a Callback Function” on
page 28-7:
28-8
See Also
start(t)
delete(t)
See Also
timer
More About
• “Handling Timer Queuing Conflicts” on page 28-10
28-9
28 Program Scheduling
In this section...
“Drop Mode (Default)” on page 28-10
“Error Mode” on page 28-11
“Queue Mode” on page 28-13
For example, suppose you create a timer with a period of 1 second, but a callback that
requires at least 1.6 seconds, as shown here for mytimer.m.
function mytimer()
t = timer;
t.Period = 1;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @mytimer_cb;
t.BusyMode = 'drop';
t.TasksToExecute = 5;
t.UserData = tic;
start(t)
end
function mytimer_cb(h,~)
timeStart = toc(h.UserData)
pause(1.6);
timeEnd = toc(h.UserData)
end
28-10
Handling Timer Queuing Conflicts
This table describes how the timer manages the execution queue.
Approximate Action
Elapsed Time
(Seconds)
0 Start the first execution of the callback.
1 Attempt to start the second execution of the callback. The first
execution is not complete, but the execution queue is empty. The
timer adds the callback to the queue.
1.6 Finish the first callback execution, and start the second. This action
clears the execution queue.
2 Attempt to start the third callback execution. The second execution is
not complete, but the queue is empty. The timer adds the callback to
the queue.
3 Attempt to start the fourth callback execution. The third callback is in
the execution queue, so the timer drops this execution of the function.
3.2 Finish the second callback and start the third, clearing the execution
queue.
4 Attempt to start another callback execution. Because the queue is
empty, the timer adds the callback to the queue. This is the fifth
attempt, but only the fourth instance that will run.
4.8 Finish the third execution and start the fourth instance, clearing the
queue.
5 Attempt to start another callback. An instance is running, but the
execution queue is empty, so the timer adds it to the queue. This is
the fifth instance that will run.
6 Do nothing: the value of the TasksToExecute property is 5, and the
fifth instance to run is in the queue.
6.4 Finish the fourth callback execution and start the fifth.
8 Finish the fifth callback execution.
Error Mode
The 'error' mode for the BusyMode property is similar to the 'drop' mode: In both
modes, the timer allows only one instance of the callback in the execution queue.
28-11
28 Program Scheduling
However, in 'error' mode, when the queue is nonempty, the timer calls the function
that you specify using the ErrorFcn property, and then stops processing. The currently
running callback function completes, but the callback in the queue does not execute.
For example, modify mytimer.m (described in the previous section) so that it includes an
error handling function and sets BusyMode to 'error'.
function mytimer()
t = timer;
t.Period = 1;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @mytimer_cb;
t.ErrorFcn = @myerror;
t.BusyMode = 'error';
t.TasksToExecute = 5;
t.UserData = tic;
start(t)
end
function mytimer_cb(h,~)
timeStart = toc(h.UserData)
pause(1.6);
timeEnd = toc(h.UserData)
end
function myerror(h,~)
disp('Reached the error function')
end
This table describes how the timer manages the execution queue.
Approximate Action
Elapsed Time
(Seconds)
0 Start the first execution of the callback.
1 Attempt to start the second execution of the callback. The first
execution is not complete, but the execution queue is empty. The
timer adds the callback to the queue.
28-12
See Also
Approximate Action
Elapsed Time
(Seconds)
1.6 Finish the first callback execution, and start the second. This action
clears the execution queue.
2 Attempt to start the third callback execution. The second execution is
not complete, but the queue is empty. The timer adds the callback to
the queue.
3 Attempt to start the fourth callback execution. The third callback is in
the execution queue. The timer does not execute the third callback,
but instead calls the error handling function.
3.2 Finish the second callback and start the error handling function.
Queue Mode
If you specify 'queue', the timer object waits until the currently executing callback
function finishes before queuing the next execution of the timer callback function.
In 'queue' mode, the timer object tries to make the average time between executions
equal the amount of time specified in the Period property. If the timer object has to wait
longer than the time specified in the Period property between executions of the timer
function callback, it shortens the time period for subsequent executions to make up the
time.
See Also
timer
More About
• “Timer Callback Functions” on page 28-5
28-13
29
Performance
For additional details about the performance of your code, such as function call
information and execution time of individual lines of code, use the MATLAB Profiler. For
more information, see “Profile to Improve Performance” on page 29-5.
Time Functions
To measure the time required to run a function, use the timeit function. The timeit
function calls the specified function multiple times, and returns the median of the
measurements. It takes a handle to the function to be measured and returns the typical
execution time, in seconds. Suppose that you have defined a function,
computeFunction, that takes two inputs, x and y, that are defined in your workspace.
You can compute the time to execute the function using timeit.
29-2
Measure Performance of Your Program
tic
% The program section to time.
toc
Sometimes programs run too fast for tic and toc to provide useful data. If your code is
faster than 1/10 second, consider measuring it running in a loop, and then average to find
the time for a single run.
The cputime function measures the total CPU time and sums across all threads. This
measurement is different from the wall-clock time that timeit or tic/toc return, and
could be misleading. For example:
• The CPU time for the pause function is typically small, but the wall-clock time
accounts for the actual time that MATLAB execution is paused. Therefore, the wall-
clock time might be longer.
• If your function uses four processing cores equally, the CPU time could be
approximately four times higher than the wall-clock time.
• Time a significant enough portion of code. Ideally, the code you are timing should take
more than 1/10 second to run.
• Put the code you are trying to time into a function instead of timing it at the command
line or inside a script.
• Unless you are trying to measure first-time cost, run your code multiple times. Use the
timeit function.
• Avoid clear all when measuring performance. For more information, see the clear
function.
• Assign your output to a variable instead of letting it default to ans.
29-3
29 Performance
See Also
profile | tic | timeit | toc
Related Examples
• “Profile to Improve Performance” on page 29-5
• “Techniques to Improve Performance” on page 29-14
• MATLAB Performance Measurement White Paper on MATLAB Central File Exchange
29-4
Profile to Improve Performance
What Is Profiling?
Profiling is a way to measure where a program spends time. After you identify which
functions are consuming the most time, you can evaluate them for possible performance
improvements. Also, you can profile your code as a debugging tool. For example,
determining which lines of code MATLAB does not run can help you develop test cases
that exercise that code. If you get an error in the file when profiling, you can see what ran
and what did not to help you isolate the problem.
Tip Code that is prematurely optimized can be unnecessarily complex without providing
a significant gain in performance. Make your first implementation as simple as possible.
Then, if speed is an issue, use profiling to identify bottlenecks.
You can profile your code using the MATLAB Profiler. The Profiler is a user interface
based on the results returned by the profile function. If you are profiling code that runs
in parallel, for best results use the Parallel Computing Toolbox™ parallel profiler. For
details, see “Profiling Parallel Code” (Parallel Computing Toolbox).
29-5
29 Performance
Consider keeping a copy of your first detail report as a basis for comparison. After
you change your code, you can run the Profiler again and compare the reports.
4 Determine whether there are changes you can make to those lines of code to improve
performance.
For example, if you have a load statement within a loop, you might be able to move
the load statement outside the loop so that it is called only once.
5 Implement the potential performance improvements in your code. Save the files, and
run clear all. Run the Profiler again and compare the results to the original
report.
If you profile the identical code twice, you can get slightly different results each time
due to inherent time fluctuations that are not dependent on your code.
6 To continue improving the performance of your code, repeat these steps.
When your code spends most of its time on calls to a few built-in functions, you have
probably optimized the code as much as possible.
For example, you can run the Lotka-Volterra example, which is provided with
MATLAB:
If, in the current MATLAB session, you previously profiled the statement, select it
from the Run this code list. MATLAB automatically starts profiling the code, and you
can skip to step 4.
29-6
Profile to Improve Performance
While the Profiler is running, the Profile time indicator is green and the number of
seconds it reports increases. The Profile time indicator appears at the top right of
the Profiler window.
When the Profiler finishes, the Profile time indicator turns black and shows the
length of time the Profiler ran. The statements you profiled display as having been
executed in the Command Window.
This time is not the actual time that your statements took to run. It is the time
elapsed from when you clicked Start Profiling until the profiling stops. If the time
reported is very different from what you expected (for example, hundreds of seconds
for a simple statement), you might have profiled longer than necessary. This time
does not match the time reported in Profile Summary report statistics, which is based
on performance clock time by default. To view profile statistics using a different
type of clock, use the profile function instead of the Profiler.
4 When profiling is complete, the Profile Summary report appears in the Profiler
window. For more information, see “Profile Summary Report” on page 29-8.
1 In the Profiler, click Start Profiling. Make sure that no code appears in the Run this
code field.
2 In the Command Window, enter and run the statements you want to profile.
3 After running all the statements, click Stop Profiling in the Profiler, and view the
Profile Summary report.
You can run the Profiler for a user interface, such as the Filter Design and Analysis tool
included with Signal Processing Toolbox. Or, you can profile an interface you created,
such as one built using GUIDE.
29-7
29 Performance
1 In the Profiler, click Start Profiling. Make sure that no code appears in the Run this
code field.
2 Start the user interface.
3 Use the interface. When you finish, click Stop Profiling in the Profiler, and view the
Profile Summary report.
Note To exclude the user interface startup process in the profile, reverse steps 1 and 2.
In other words, start the user interface before you click Start Profiling.
29-8
Profile to Improve Performance
Column Description
Function List of all the functions called by the profiled code. Initially the functions
Name appear in order of time they took to process.
Calls Number of times the profiled code called the function.
Total Time Total time spent in a function, including all accessed child functions, in
seconds. The time for a function includes time spent in child functions.
The Profiler itself takes some time, which is included in the results. The
total time can be zero for files whose run time is inconsequential.
Self Time Total time in seconds spent in a function, excluding time spent in any
child functions. Self time also includes some overhead resulting from the
process of profiling.
Total Time Graphic display showing self time compared to total time.
Plot
To open the Profile Detail report, click a function name in the Profile Summary report. To
return to the Profile Summary report from the Profile Detail report, click in the toolbar
of the Profile window.
29-9
29 Performance
To specify which sections the Profile Detail Report includes, select the check boxes at the
top of the report, and click the Refresh button. Use the check boxes to select from these
options.
29-10
See Also
See Also
profile
More About
• “Measure Performance of Your Program” on page 29-2
• “Techniques to Improve Performance” on page 29-14
• “Use Profiler to Determine Code Coverage” on page 29-12
29-11
29 Performance
To determine how much of a file MATLAB executed when you profiled it, run the Coverage
Report.
1 Profile your MATLAB code file. For more information, see “Profile to Improve
Performance” on page 29-5 or the profile function.
2 Ensure that the Profiler is not currently profiling.
• In the Profiler, a Stop Profiling button displays if the Profiler is running. If the
Profiler is running, click the Stop Profiling button.
• At the command prompt, check the Profiler status using profile status. If the
ProfilerStatus is 'on', stop the Profiler by typing profile off.
3 Use the Current Folder browser to navigate to the folder containing the profiled code
file.
Note You cannot run reports when the path is a UNC (Universal Naming Convention)
path; that is, a path that starts with \\. Instead, use an actual hard drive on your
system, or a mapped network drive.
4 On the Current Folder browser, click , and then select Reports > Coverage
Report.
The Profiler Coverage Report opens, providing a summary of coverage for the
profiled file. In the following image, the profiled file is lengthofline2.m.
29-12
See Also
5 Click the Coverage link to see the Profile Detail Report for the file.
See Also
profile
More About
• “Profile to Improve Performance” on page 29-5
• “Measure Performance of Your Program” on page 29-2
• “Techniques to Improve Performance” on page 29-14
29-13
29 Performance
Environment
Be aware of background processes that share computational resources and decrease the
performance of your MATLAB code.
Code Structure
While organizing your code:
29-14
Techniques to Improve Performance
• Place independent operations outside loops — If code does not evaluate differently
with each for or while loop iteration, move it outside of the loop to avoid redundant
computations.
• Create new variables if data type changes — Create a new variable rather than
assigning data of a different type to an existing variable. Changing the class or array
shape of an existing variable takes extra time to process.
• Use short-circuit operators — Use short-circuiting logical operators, && and || when
possible. Short-circuiting is more efficient because MATLAB evaluates the second
operand only when the result is not fully determined by the first operand. For more
information, see Logical Operators: Short Circuit.
• Avoid global variables — Minimizing the use of global variables is a good programming
practice, and global variables can decrease performance of your MATLAB code.
• Avoid overloading built-ins — Avoid overloading built-in functions on any standard
MATLAB data classes.
• Avoid using “data as code” — If you have large portions of code (for example, over 500
lines) that generate variables with constant values, consider constructing the variables
and saving them, for example, in a MAT-file or .csv file. Then you can load the
variables instead of executing code to generate them.
• Avoid clearing more code than necessary. Do not use clear all programmatically.
For more information, see clear.
• Avoid functions that query the state of MATLAB such as inputname, which, whos,
exist(var), and dbstack. Run-time introspection is computationally expensive.
• Avoid functions such as eval, evalc, evalin, and feval(fname). Use the function
handle input to feval whenever possible. Indirectly evaluating a MATLAB expression
from text is computationally expensive.
• Avoid programmatic use of cd, addpath, and rmpath, when possible. Changing the
MATLAB path during run time results in code recompilation.
29-15
29 Performance
See Also
More About
• “Measure Performance of Your Program” on page 29-2
• “Profile to Improve Performance” on page 29-5
• “Preallocation” on page 29-17
• “Vectorization” on page 29-19
• “Graphics Performance”
29-16
Preallocation
Preallocation
for and while loops that incrementally increase the size of a data structure each time
through the loop can adversely affect performance and memory use. Repeatedly resizing
arrays often requires MATLAB to spend extra time looking for larger contiguous blocks of
memory, and then moving the array into those blocks. Often, you can improve code
execution time by preallocating the maximum amount of space required for the array.
The following code displays the amount of time needed to create a scalar variable, x, and
then to gradually increase the size of x in a for loop.
tic
x = 0;
for k = 2:1000000
x(k) = x(k-1) + 5;
end
toc
If you preallocate a 1-by-1,000,000 block of memory for x and initialize it to zero, then the
code runs much faster because there is no need to repeatedly reallocate memory for the
growing data structure.
tic
x = zeros(1, 1000000);
for k = 2:1000000
x(k) = x(k-1) + 5;
end
toc
Use the appropriate preallocation function for the kind of array you want to initialize:
29-17
29 Performance
A = int8(zeros(100));
This statement preallocates a 100-by-100 matrix of int8, first by creating a full matrix of
double values, and then by converts each element to int8. Creating the array as int8
values saves time and memory. For example:
A = zeros(100, 'int8');
See Also
Related Examples
• “Reshaping and Rearranging Arrays”
• “Preallocate Memory for Cell Array” on page 12-18
• “Access Data Using Categorical Arrays” on page 8-30
• “Preallocate Arrays of Graphics Objects”
• “Construct Object Arrays”
More About
• “Techniques to Improve Performance” on page 29-14
29-18
Vectorization
Vectorization
In this section...
“Using Vectorization” on page 29-19
“Array Operations” on page 29-20
“Logical Array Operations” on page 29-22
“Matrix Operations” on page 29-23
“Ordering, Setting, and Counting Operations” on page 29-24
“Functions Commonly Used in Vectorization” on page 29-26
Using Vectorization
MATLAB is optimized for operations involving matrices and vectors. The process of
revising loop-based, scalar-oriented code to use MATLAB matrix and vector operations is
called vectorization. Vectorizing your code is worthwhile for several reasons:
This code computes the sine of 1,001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
t = 0:.01:10;
y = sin(t);
29-19
29 Performance
The second code sample usually executes faster than the first and is a more efficient use
of MATLAB. Test execution speed on your system by creating scripts that contain the code
shown, and then use the tic and toc functions to measure their execution time.
This code computes the cumulative sum of a vector at every fifth element:
x = 1:10000;
ylength = (length(x) - mod(length(x),5))/5;
y(1:ylength) = 0;
for n= 5:5:length(x)
y(n/5) = sum(x(1:n));
end
Using vectorization, you can write a much more concise MATLAB process. This code
shows one way to accomplish the task:
x = 1:10000;
xsums = cumsum(x);
y = xsums(5:5:length(x));
Array Operations
Array operators perform the same operation for all elements in the data set. These types
of operations are useful for repetitive calculations. For example, suppose you collect the
volume (V) of various cones by recording their diameter (D) and height (H). If you collect
the information for just one cone, you can calculate the volume for that single cone:
V = 1/12*pi*(D^2)*H;
Now, collect information on 10,000 cones. The vectors D and H each contain 10,000
elements, and you want to calculate 10,000 volumes. In most programming languages,
you need to set up a loop similar to this MATLAB code:
for n = 1:10000
V(n) = 1/12*pi*(D(n)^2)*H(n));
end
With MATLAB, you can perform the calculation for each element of a vector with similar
syntax as the scalar case:
% Vectorized Calculation
V = 1/12*pi*(D.^2).*H;
29-20
Vectorization
Note Placing a period (.) before the operators *, /, and ^, transforms them into array
operators.
Array operators also enable you to combine matrices of different dimensions. This
automatic expansion of size-1 dimensions is useful for vectorizing grid creation, matrix
and vector operations, and more.
Suppose that matrix A represents test scores, the rows of which denote different classes.
You want to calculate the difference between the average score and individual scores for
each class. Using a loop, the operation looks like:
A = [97 89 84; 95 82 92; 64 80 99;76 77 67;...
88 59 74; 78 66 87; 55 93 85];
mA = mean(A);
B = zeros(size(A));
for n = 1:size(A,2)
B(:,n) = A(:,n) - mA(n);
end
A more direct way to do this is with A - mean(A), which avoids the need of a loop and is
significantly faster.
devA = A - mean(A)
devA =
18 11 0
16 4 8
-15 2 15
-3 -1 -17
9 -19 -10
-1 -12 3
-24 15 1
Even though A is a 7-by-3 matrix and mean(A) is a 1-by-3 vector, MATLAB implicitly
expands the vector as if it had the same size as the matrix, and the operation executes as
a normal element-wise minus operation.
The size requirement for the operands is that for each dimension, the arrays must either
have the same size or one of them is 1. If this requirement is met, then dimensions where
one of the arrays has size 1 are expanded to be the same size as the corresponding
29-21
29 Performance
dimension in the other array. For more information, see “Compatible Array Sizes for Basic
Operations” on page 2-31.
Another area where implicit expansion is useful for vectorization is if you are working
with multidimensional data. Suppose you want to evaluate a function, F, of two variables,
x and y.
To evaluate this function at every combination of points in the x and y vectors, you need
to define a grid of values. For this task you should avoid using loops to iterate through the
point combinations. Instead, if one of the vectors is a column and the other is a row, then
MATLAB automatically constructs the grid when the vectors are used with an array
operator, such as x+y or x-y. In this example, x is a 21-by-1 vector and y is a 1-by-16
vector, so the operation produces a 21-by-16 matrix by expanding the second dimension of
x and the first dimension of y.
x = (-2:0.2:2)'; % 21-by-1
y = -1.5:0.2:1.5; % 1-by-16
F = x.*exp(-x.^2-y.^2); % 21-by-16
In cases where you want to explicitly create the grids, you can use the meshgrid and
ndgrid functions.
For example, suppose while collecting data from 10,000 cones, you record several
negative values for the diameter. You can determine which values in a vector are valid
with the >= operator:
D = [-0.2 1.0 1.5 3.0 -1.0 4.2 3.14];
D >= 0
ans =
0 1 1 1 0 1 1
You can directly exploit the logical indexing power of MATLAB to select the valid cone
volumes, Vgood, for which the corresponding elements of D are nonnegative:
29-22
Vectorization
MATLAB allows you to perform a logical AND or OR on the elements of an entire vector
with the functions all and any, respectively. You can throw a warning if all values of D
are below zero:
if all(D < 0)
warning('All values of diameter are negative.')
return
end
MATLAB can also compare two vectors with compatible sizes, allowing you to impose
further restrictions. This code finds all the values where V is nonnegative and D is greater
than H:
To aid comparison, MATLAB contains special values to denote overflow, underflow, and
undefined operators, such as Inf and NaN. Logical operators isinf and isnan exist to
help perform logical tests for these special values. For example, it is often useful to
exclude NaN values from computations:
xvalid =
2 -1 0 3 2 11 4 Inf
Note Inf == Inf returns true; however, NaN == NaN always returns false.
Matrix Operations
When vectorizing code, you often need to construct a matrix with a particular size or
structure. Techniques exist for creating uniform matrices. For instance, you might need a
5-by-5 matrix of equal elements:
A = ones(5,5)*10;
29-23
29 Performance
v = 1:5;
A = repmat(v,3,1)
A =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
The function repmat possesses flexibility in building matrices from smaller matrices or
vectors. repmat creates matrices by repeating an input matrix:
A = repmat(1:3,5,2)
B = repmat([1 2; 3 4],2,2)
A =
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
B =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
A number of different ways exist for finding the redundant elements of a vector. One way
involves the function diff. After sorting the vector elements, equal adjacent elements
produce a zero entry when you use the diff function on that vector. Because diff(x)
29-24
Vectorization
produces a vector that has one fewer element than x, you must add an element that is not
equal to any other element in the set. NaN always satisfies this condition. Finally, you can
use logical indexing to choose the unique elements in the set:
x = [2 1 2 2 3 1 3 2 1 3];
x = sort(x);
difference = diff([x,NaN]);
y = x(difference~=0)
y =
1 2 3
Alternatively, you could accomplish the same operation by using the unique function:
y=unique(x);
However, the unique function might provide more functionality than is needed and slow
down the execution of your code. Use the tic and toc functions if you want to measure
the performance of each code snippet.
Rather than merely returning the set, or subset, of x, you can count the occurrences of an
element in a vector. After the vector sorts, you can use the find function to determine
the indices of zero values in diff(x) and to show where the elements change value. The
difference between subsequent indices from the find function indicates the number of
occurrences for a particular element:
x = [2 1 2 2 3 1 3 2 1 3];
x = sort(x);
difference = diff([x,max(x)+1]);
count = diff(find([1,difference]))
y = x(find(difference))
count =
3 4 3
y =
1 2 3
29-25
29 Performance
The find function does not return indices for NaN elements. You can count the number of
NaN and Inf values using the isnan and isinf functions.
count_nans = sum(isnan(x(:)));
count_infs = sum(isinf(x(:)));
29-26
See Also
See Also
More About
• “Array Indexing”
• “Techniques to Improve Performance” on page 29-14
• “Array vs. Matrix Operations” on page 2-25
External Websites
• MathWorks Newsletter: Matrix Indexing in MATLAB
29-27
30
Memory Usage
The numeric class you should use in MATLAB depends on your intended actions. The
default class double gives the best precision, but requires 8 bytes per element of
memory to store. If you intend to perform complicated math such as linear algebra, you
must use a floating-point class such as a double or single. The single class requires
only 4 bytes. There are some limitations on what you can do with the single class, but
most MATLAB Math operations are supported.
If you just need to carry out simple arithmetic and you represent the original data as
integers, you can use the integer classes in MATLAB. The following is a list of numeric
classes, memory requirements (in bytes), and the supported operations.
30-2
Strategies for Efficient Use of Memory
takes about 104 bytes per array. This overhead only becomes an issue when you have a
large number (e.g., hundreds or thousands) of small mxArrays (e.g., scalars). The whos
command lists the memory used by variables, but does not include this overhead.
Because simple numeric arrays (comprising one mxArray) have the least overhead, you
should use them wherever possible. When data is too complex to store in a simple array
(or matrix), you can use other data structures.
Cell arrays are comprised of separate mxArrays for each element. As a result, cell arrays
with many small elements have a large overhead.
Structures require a similar amount of overhead per field (see “Array Headers” on page
30-15). Structures with many fields and small contents have a large overhead and should
be avoided. A large array of structures with numeric scalar fields requires much more
memory than a structure with fields containing large numeric arrays.
Also note that while MATLAB stores numeric arrays in contiguous memory, this is not the
case for structures and cell arrays.
When reading data from a binary file with fread, it is a common error to specify only the
class of the data in the file, and not the class of the data MATLAB uses once it is in the
workspace. As a result, the default double is used even if you are reading only 8-bit
values. For example,
If your data contains many zeros, consider using sparse arrays, which store only nonzero
elements. The following example compares sparse and full storage requirements:
30-3
30 Memory Usage
You can see that this array requires only about 4 KB to be stored as sparse, but
approximately 8 MB as a full matrix. In general, for a sparse double array with nnz
nonzero elements and ncol columns, the memory required is:
Note that MATLAB supports most, but not all, mathematical operations on sparse arrays.
Avoid creating large temporary variables, and also make it a practice to clear temporary
variables when they are no longer needed. For example, this code creates an array of
zeros stored as a temporary variable A, and then converts A to single-precision:
A = zeros(1e6,1);
As = single(A);
A = zeros(1e6,1,'single');
Using the repmat function, array preallocation and for loops are other ways to work on
non-double data without requiring temporary storage in memory.
When working with large data sets, be aware that MATLAB makes a temporary copy of an
input variable if the called function modifies its value. This temporarily doubles the
memory required to store the array, which causes MATLAB to generate an error if
sufficient memory is not available.
30-4
Strategies for Efficient Use of Memory
One way to use less memory in this situation is to use nested functions. A nested function
shares the workspace of all outer functions, giving the nested function access to data
outside of its usual scope. In the example shown here, nested function setrowval has
direct access to the workspace of the outer function myfun, making it unnecessary to
pass a copy of the variable in the function call. When setrowval modifies the value of A,
it modifies it in the workspace of the calling function. There is no need to use additional
memory to hold a separate array for the function being called, and there also is no need
to return the modified value of A:
function myfun
A = magic(500);
setrowval(400, 0);
disp('The new value of A(399:401,1:10) is')
A(399:401,1:10)
end
If your program generates very large amounts of data, consider writing the data to disk
periodically. After saving that portion of the data, use the clear function to remove the
variable from memory and continue with the data generation.
When you are working with a very large data set repeatedly or interactively, clear the old
variable first to make space for the new variable. Otherwise, MATLAB requires temporary
storage of equal size before overriding the variable. For example,
30-5
30 Memory Usage
clear a
a = rand(100e6,1) % New 800 MB array
See Also
More About
• “Resolve “Out of Memory” Errors” on page 30-7
30-6
Resolve “Out of Memory” Errors
MATLAB has built-in protection against creating arrays that are too large. By default,
MATLAB can use up to 100% of the RAM (not including virtual memory) of your computer
to allocate memory for arrays, and if an array would exceed that threshold, then MATLAB
returns an error. For example, this statement attempts to create an array with an
unreasonable size:
A = rand(1e6,1e6);
Error using rand
Requested 1000000x1000000 (7450.6GB) array exceeds maximum array size
preference. Creation of arrays greater than this limit may take a long
time and cause MATLAB to become unresponsive. See array size limit or
preference panel for more information.
See “Workspace and Variable Preferences” for information on adjusting this array size
limit. If you turn off the array size limit, then MATLAB returns a different error:
A = rand(1e6,1e6);
No matter how you run into memory limits, there are several resolutions available
depending on your goals. The techniques discussed in “Strategies for Efficient Use of
Memory” on page 30-2 can help you optimize the available memory you have, including:
If you are already using memory efficiently and the problem persists, then the remaining
sections of this page contain possible solutions.
30-7
30 Memory Usage
automatically handling all of the data chunking and processing in the background. There
are two primary ways you can leverage tall arrays:
1 If you have a large array that fits in memory, but you run out of memory when you try
to perform calculations, you can cast the array to a tall array:
B = tall(A)
This method enables you work with large arrays that can fit in memory, but that
consume too much memory to allow for copies of the data during calculations. For
example, if you have 8GB of RAM and a 5GB matrix, casting the matrix to a tall array
enables you to perform calculations on the matrix without running out of memory.
See “Convert In-Memory Arrays to Tall Arrays” for an example of this usage.
2 If you have file or folder-based data, you can create a datastore and then create a
tall array on top of the datastore:
ds = datastore('path/to/data.csv');
tt = tall(ds);
This method gives you the full power of tall arrays in MATLAB: the data can have any
number of rows and MATLAB does not run out of memory. And because datastore
works with both local and remote data locations, the data you work with does not
need to be on the computer you use to analyze it. See “Work with Remote Data” for
more information.
30-8
Resolve “Out of Memory” Errors
problem when importing from sources such as a database, where you can explicitly
search for elements matching a query. But this is a common problem with loading large
flat text or binary files.
The datastore function enables you to work with large data sets incrementally. This
function underpins “Tall Arrays for Out-of-Memory Data” and “Distributed Arrays”
(Parallel Computing Toolbox), but you can use it for other purposes as well. Datastores
are useful any time you want to load small portions of a data set into memory at a time.
To create a datastore, you need to provide the name of a file or a directory containing a
collection of files with similar formatting. For example, with a single file:
ds = datastore('path/to/file.csv')
ds = datastore('path/to/folder/')
You can also use the wildcard character * to select all files of a specific type, as in:
ds = datastore('data/*.csv')
Datastores support a wide variety of common file formats (tabular data, images,
spreadsheets, and so on). See “Select Datastore for File Format or Application” for more
information.
Aside from datastores, MATLAB also has several other functions to load parts of files,
such as the matfile function to load portions of MAT-files. This table summarizes partial
loading functions by file type.
30-9
30 Memory Usage
Most systems enable you to control the size of your swap file. The steps involved depend
on your operating system:
• Windows Systems — Use the Windows Control Panel to change the size of the virtual
memory paging file on your system. For more information, refer to the Windows help.
• Linux® Systems — Change your swap space by using the mkswap and swapon
commands. For more information, at the Linux prompt type man followed by the
command name.
30-10
See Also
There is no interface for directly controlling the swap space on macOS systems.
64-bit operating systems support a process limit of 8 terabytes. On Linux systems, see the
ulimit command to view and set user limits including virtual memory.
Using -nojvm comes with a penalty in that you lose many features that rely on the Java
software, including the entire development environment. Starting MATLAB with the -
nodesktop option does not save any substantial amount of memory.
See Also
memory
Related Examples
• “Strategies for Efficient Use of Memory” on page 30-2
• “Large Files and Big Data”
• “Work with Remote Data”
30-11
30 Memory Usage
30-12
How MATLAB Allocates Memory
In this section...
“Memory Allocation for Arrays” on page 30-13
“Data Structures and Memory” on page 30-17
Note Any information on how the MATLAB software handles data internally is subject to
change in future releases.
If you add new elements to an existing array, MATLAB expands the existing array in
memory in a way that keeps its storage contiguous. This usually requires finding a new
block of memory large enough to hold the expanded array. MATLAB then copies the
contents of the array from its original location to this new block in memory, adds the new
elements to the array in this block, and frees up the original array location in memory.
30-13
30 Memory Usage
If you remove elements from an existing array, MATLAB keeps the memory storage
contiguous by removing the deleted elements, and then compacting its storage in the
original memory location.
Working with Large Data Sets
If you are working with large data sets, you need to be careful when increasing the size of
an array to avoid getting errors caused by insufficient memory. If you expand the array
beyond the available contiguous memory of its original location, MATLAB must make a
copy of the array and set this copy to the new value. During this operation, there are two
copies of the original array in memory. This temporarily doubles the amount of memory
required for the array and increases the risk of your program running out of memory
during execution. It is better to preallocate sufficient memory for the largest potential
size of the array at the start. See “Preallocation” on page 29-17.
Copying Arrays
Internally, multiple variables can point to the same block of data, thus sharing that array's
value. When you copy a variable to another variable (e.g., B = A), MATLAB makes a copy
of the array reference, but not the array itself. As long as you do not modify the contents
of the array, there is no need to store more than one copy of it. If you do modify any
elements of the array, MATLAB makes a copy of the array and then modifies that copy.
This example uses the memory function to demonstrate how MATLAB handles copying
arrays. memory is available only on Windows systems.
Start by creating a simple script memUsed.m to display how much memory is being used
by your MATLAB process. Put these two lines of code in the script.
Get an initial reading of how much memory is being used by your MATLAB process:
A = magic(2000);
memUsed
30-14
How MATLAB Allocates Memory
ans =
327.6349e+006
Make a copy of array A in B. As there is no need to have two copies of the array data,
MATLAB only makes a copy of the array reference. This requires no significant additional
memory:
B = A;
memUsed
ans =
327.6349e+006
Now modify B by making it one half its original size (that is, set 1000 rows to empty). This
requires that MATLAB make a copy of at least the first 1000 rows of the A array, and
assign that copy to B:
B(1001:2000,:) = [];
format short; size(B)
ans =
1000 2000
Check the memory used again. Even though B is significantly smaller than it was
originally, the amount of memory used by the MATLAB process has increased by about 16
MB (1/2 of the 32 MB originally required for A) because B could no longer remain as just
a reference to A:
Array Headers
When you assign an array to a variable, MATLAB also stores information about the array
(such as class and dimensions) in a separate piece of memory called a header. For most
arrays, the memory required to store the header is insignificant. There is a small
advantage to storing large data sets in a small number of large arrays as opposed to a
large number of small arrays. This is because the former configuration requires fewer
array headers.
Structure and Cell Arrays
For structures and cell arrays, MATLAB creates a header not only for each array, but also
for each field of the structure and for each cell of a cell array. Because of this, the amount
30-15
30 Memory Usage
of memory required to store a structure or cell array depends not only on how much data
it holds, but also on how it is constructed.
For example, take a scalar structure array S1 having fields R, G, and B. Each field of size
100-by-50 requires one array header to describe the overall structure, one header for
each unique field name, and one header per field for the 1-by-1 structure array. This
makes a total of seven array headers for the entire data structure:
S1.R(1:100,1:50)
S1.G(1:100,1:50)
S1.B(1:100,1:50)
On the other hand, take a 100-by-50 structure array S2 in which each element has scalar
fields R, G, and B. In this case, you need one array header to describe the overall
structure, one for each unique field name, and one per field for each of the 5,000
elements of the structure, making a total of 15,004 array headers for the entire data
structure:
S2(1:100,1:50).R
S2(1:100,1:50).G
S2(1:100,1:50).B
Even though S1 and S2 contain the same amount of data, S1 uses significantly less space
in memory. Not only is less memory required, but there is a corresponding speed benefit
to using the S1 format, as well.
See “Cell Arrays” and “Structures” under “Data Structures and Memory” on page 30-17.
Memory Usage Reported By the whos Function
The whos function displays the amount of memory consumed by any variable. For reasons
of simplicity, whos reports only the memory used to store the actual data. It does not
report storage for the array header, for example.
Function Arguments
MATLAB handles arguments passed in function calls in a similar way. When you pass a
variable to a function, you are actually passing a reference to the data that the variable
represents. As long as the input data is not modified by the function being called, the
variable in the calling function and the variable in the called function point to the same
location in memory. If the called function modifies the value of the input data, then
MATLAB makes a copy of the original array in a new location in memory, updates that
30-16
How MATLAB Allocates Memory
copy with the modified value, and points the input variable in the called function to this
new array.
In the example below, function myfun modifies the value of the array passed into it.
MATLAB makes a copy in memory of the array pointed to by A, sets variable X as a
reference to this new array, and then sets one row of X to zero. The array referenced by A
remains unchanged:
A = magic(500);
myfun(A);
function myfun(X)
X(400,:) = 0;
If the calling function needs the modified value of the array it passed to myfun, you need
to return the updated array as an output of the called function, as shown here for variable
A:
A = magic(500);
A = myfun(A);
sprintf('The new value of A is %d', A)
function Y = myfun(X)
X(400,:) = 0;
Y = X;
Numeric Arrays
MATLAB requires 1, 2, 4, or 8 bytes to store 8-bit, 16-bit, 32-bit, and 64-bit signed and
unsigned integers, respectively. For floating-point numbers, MATLAB uses 4 or 8 bytes for
single and double types. To conserve memory when working with numeric arrays,
MathWorks recommends that you use the smallest integer or floating-point type that
contains your data without overflowing. For more information, see “Numeric Types”.
30-17
30 Memory Usage
Complex Arrays
MATLAB stores complex data as separate real and imaginary parts. If you make a copy of
a complex array variable, and then modify only the real or imaginary part of the array,
MATLAB creates an array containing both real and imaginary parts.
Sparse Matrices
It is best to store matrices with values that are mostly zero in sparse format. Sparse
matrices can use less memory and might also be faster to manipulate than full matrices.
You can convert a full matrix to sparse format using the sparse function.
Compare two 1000-by-1000 matrices: X, a matrix of doubles with 2/3 of its elements equal
to zero; and Y, a sparse copy of X. The following example shows that the sparse matrix
requires approximately half as much memory:
whos
Name Size Bytes Class
Cell Arrays
In addition to data storage, cell arrays require a certain amount of additional memory to
store information describing each cell. This information is recorded in a header, and there
is one header for each cell of the array. You can determine the amount of memory
required for a cell array header by finding the number of bytes consumed by a 1-by-1 cell
that contains no data, as shown below for a 32-bit system:
whos A
Name Size Bytes Class Attributes
A 1x1 60 cell
In this case, MATLAB shows the number of bytes required for each header in the cell
array on a 32-bit system to be 60. This is the header size that is used in all of the 32-bit
examples in this section. For 64-bit systems, the header size is assumed to be 112 bytes in
this documentation. You can find the correct header size on a 64-bit system using the
method just shown for 32 bits.
30-18
How MATLAB Allocates Memory
To predict the size of an entire cell array, multiply the number you have derived for the
header by the total number of cells in the array, and then add to that the number of bytes
required for the data you intend to store in the array:
So a 10-by-20 cell array that contains 400 bytes of data would require 22,800 bytes of
memory on a 64-bit system:
Note While numeric arrays must be stored in contiguous memory, structures and cell
arrays do not.
The following 4-by-1 cell array records the brand name, screen size, price, and on-sale
status for three laptop computers:
On a 32-bit system, the cell array header alone requires 60 bytes per cell:
4 cells * 60 bytes per cell = 240 bytes for the cell array
Calculate the memory required to contain the data in each of the four cells:
Add the two, and then compare your result with the size returned by MATLAB:
whos Laptops
30-19
30 Memory Usage
Structures
S.A = [];
B = whos('S');
B.bytes - 60
ans =
64
On a 64-bit computer system, a 4-by-5 structure Clients with fields Address and Phone
uses 4,608 bytes just for the structure:
To that sum, you must add the memory required to hold the data assigned to each field. If
you assign a 25-character vector to Address and a 12-character vector to Phone in each
element of the 4-by-5 Clients array, you use 1480 bytes for data:
Add the two and you see that the entire structure consumes 6,088 bytes of memory.
Example 1 – Memory Allocation for a Structure Array
Compute the amount of memory that would be required to store the following 6-by-5
structure array having the following four fields on a 32-bit system:
A = int8(ones(5,8,6));
B = single(1:500);
C = uint16(magic(30));
30-20
How MATLAB Allocates Memory
for m=1:6
for n=1:5
s(m,n)=s(1,1);
end
end
Calculate the amount of memory required for the structure itself, and then for the data it
contains:
Add the two, and then compare your result with the size returned by MATLAB:
whos s
Name Size Bytes Class Attributes
30-21
30 Memory Usage
MATLAB does not provide a way to define a reference to a value, as in languages like C+
+. Instead, MATLAB allows multiple output as well as multiple input parameters so that
you know what values are going into a function and what values are coming out of the
function.
Copy-on-Write
If a function does not modify an input argument, MATLAB does not make a copy of the
values contained in the input variable.
A = rand(1e7,1);
B = f1(A);
The function f1 multiplies each element in the input array X by 1.1 and assigns the
result to the variable Y.
function Y = f1(X)
Y = X.*1.1; % X is a shared copy of A
end
Because the function does not modify the input values, the local variable X and the
variable A in the caller's workspace share the data. After f1 executes, the values assigned
to A have not changed. The variable B in the caller's workspace contains the result of the
30-22
Avoid Unnecessary Copies of Data
element-wise multiplication. The input is passed by value. However, no copy is made when
calling f1.
The function f2 does modify its local copy of the input variable, causing the local copy to
be unshared with input A. The value of X in the function is now an independent copy of
the input variable A in the caller's workspace. When f2 returns the result to the caller's
workspace, the local variable X is destroyed.
A = rand(1e7,1);
B = f2(A);
function Y = f2(X)
X = X.*1.1; % X is an independent copy of A
Y = X; % Y is a shared copy of X
end
You can use the value returned from a function as an input argument to another function.
For example, use the rand function to create the input for the function f2 directly.
B = f2(rand(1e7,1));
The only variable holding the value returned by rand is the temporary variable X in the
workspace of the function f2. There is no shared or independent copy of these values in
the caller's workspace. Directly passing function outputs saves the time and memory
required to create a copy of the input values in the called function. This approach makes
sense when the input values are not used again.
Assigning In-Place
You can assign the output of a function to the same variable as the input when you do not
need to preserve the original input values.
A = f2(A);
Reassignment to the same variable name follows the copy-on-write behavior described
previously: modifying the input variable values results in a temporary copy of those
values. However, MATLAB can apply memory optimizations under certain conditions.
30-23
30 Memory Usage
Because the call to the local function, A = fLocal(A), assigns the output to the variable
A, MATLAB does not need to preserve the original value of A during execution of the
function. Within fLocal, the input X has the only copy of the value originally held by A.
Therefore, modifications made to X inside fLocal do not result in a copy of the data. The
assignment, X = X.*1.1, modifies X in place, without allocating a new array for the
result of the multiplication.
function memoryOptimization
A = rand(1e7,1);
A = fLocal(A);
end
function X = fLocal(X)
X = X.*1.1;
end
Eliminating the copy in the local function saves memory and improves execution speed for
large arrays. However, MATLAB cannot apply memory optimization when it is possible to
use the variable after the function throws an error. Therefore, this optimization is not
applied in scripts, on the command line, in calls to eval, or to code inside try/catch
blocks.
Also, MATLAB does not apply memory optimization when the original variable can be
accessed directly during execution of the called function. For example, if fLocal was a
nested function, MATLAB could not apply the optimization because variables can be
shared with the parent function.
Finally, MATLAB does not apply memory optimization when the assigned variable is
declared as global or persistent.
When calling functions, you know that the input variables are not modified in the caller's
workspace. Therefore, you do not need to make copies of inputs inside a function or at a
call site just to guard against the possibility that these values might be modified. Only the
variables assigned to returned values are modified.
30-24
See Also
Also, you avoid the possibility of corrupting workspace variables if an error occurs within
a function that has been passed a variable by reference.
Handle Objects
There are special kinds of objects called handles. All variables that hold copies of the
same handle can access and modify the same underlying object. Handle objects are useful
in specialized circumstances where an object represents a physical object such as a
window, plot, device, or person rather than a mathematical object like a number or
matrix.
Handle objects derive from the handle class, which provides functionality such as events
and listeners, destructor methods, and support for dynamic properties.
For more information about values and handles, see “Comparison of Handle and Value
Classes” and “Which Kind of Class to Use”.
See Also
handle
Related Examples
• “Handle Object Behavior”
• “Avoid Copies of Arrays in MEX Functions”
• “Strategies for Efficient Use of Memory” on page 30-2
30-25
31
For example, create a class definition file named someClass.m with several properties
and methods, as shown.
classdef someClass
% someClass Summary of this class goes here
% Detailed explanation goes here
properties
One % First public property
Two % Second public property
end
properties (Access=private)
Three % Do not show this property
end
methods
function obj = someClass
% Summary of constructor
end
function myMethod(obj)
% Summary of myMethod
disp(obj)
end
end
methods (Static)
function myStaticMethod
% Summary of myStaticMethod
end
end
end
31-2
Create Help for Classes
View the help text and the details from the class definition using the doc command.
doc someClass
31-3
31 Custom Help and Documentation
Classes
Create help text for classes by including comments on lines immediately after the
classdef statement in a file. For example, create a file named myClass.m, as shown.
classdef myClass
% myClass Summary of myClass
% This is the first line of the description of myClass.
% Descriptions can include multiple lines of text.
%
% myClass Properties:
% a - Description of a
% b - Description of b
%
% myClass Methods:
% doThis - Description of doThis
% doThat - Description of doThat
properties
a
b
end
methods
function obj = myClass
end
function doThis(obj)
end
function doThat(obj)
end
end
end
Lists and descriptions of the properties and methods in the initial comment block are
optional. If you include comment lines containing the class name followed by
Properties or Methods and a colon (:), then MATLAB creates hyperlinks to the help for
the properties or methods.
View the help text for the class in the Command Window using the help command.
help myClass
31-4
Create Help for Classes
myClass Properties:
a - Description of a
b - Description of b
myClass Methods:
doThis - Description of doThis
doThat - Description of doThat
Methods
Create help for a method by inserting comments immediately after the function definition
statement. For example, modify the class definition file myClass.m to include help for the
doThis method.
function doThis(obj)
% doThis Do this thing
% Here is some help text for the doThis method.
%
% See also DOTHAT.
disp(obj)
end
View the help text for the method in the Command Window using the help command.
Specify both the class name and method name, separated by a dot.
help myClass.doThis
Properties
• Insert comment lines above the property definition. Use this approach for multiline
help text.
• Add a single-line comment next to the property definition.
31-5
31 Custom Help and Documentation
Comments above the definition have precedence over a comment next to the definition.
For example, modify the property definitions in the class definition file myClass.m.
properties
a % First property of myClass
View the help for properties in the Command Window using the help command. Specify
both the class name and property name, separated by a dot.
help myClass.a
help myClass.b
Enumerations
Like properties, there are two ways to create help for enumerations:
• Insert comment lines above the enumeration definition. Use this approach for
multiline help text.
• Add a single-line comment next to the enumeration definition.
Comments above the definition have precedence over a comment next to the definition.
31-6
Create Help for Classes
View the help in the Command Window using the help command. Specify both the class
name and enumeration member, separated by a dot.
help myEnumeration.uno
help myEnumeration.dos
Events
Like properties and enumerations, there are two ways to create help for events:
• Insert comment lines above the event definition. Use this approach for multiline help
text.
• Add a single-line comment next to the event definition.
Comments above the definition have precedence over a comment next to the definition.
methods
function fireEventAlpha(h)
notify(h,'Alpha')
end
function fireEventBeta(h)
notify(h,'Beta')
31-7
31 Custom Help and Documentation
end
end
end
View the help in the Command Window using the help command. Specify both the class
name and event, separated by a dot.
help hasEvents.Alpha
help hasEvents.Beta
See Also
doc | help
More About
• “Role of Classes in MATLAB”
• “User-Defined Classes”
31-8
Check Which Programs Have Help
In the Help Report, you specify a set of help components for which you want to search,
such as examples or See Also lines. For each file searched, MATLAB displays the help
text for the components it finds. Otherwise, MATLAB displays a highlighted message to
indicate that the component is missing.
To generate a Help Report, in the Current Folder browser, navigate to the folder you want
to check, click , and then select Reports > Help Report. The Help Report displays in
the MATLAB web browser.
Note You cannot run reports when the path is a UNC (Universal Naming Convention)
path; that is, a path that starts with \\. Instead, use an actual hard drive on your system,
or a mapped network drive.
31-9
31 Custom Help and Documentation
If the programs listed after See also are on the search path,
then the help command generates hyperlinks to the help for
those programs. The Help Report indicates when a program in
the See also line is not on the path.
Copyright Check for a comment line in the file that begins with the word
Copyright. When there is a copyright line, the report also
checks whether the end year is current. The date check requires
that the copyright line includes either a single year (such as
2012) or a range of years with no spaces (such as 2001-2012).
31-10
See Also
See Also
Related Examples
• “Add Help for Your Program” on page 20-6
• “Create Help Summary Files — Contents.m” on page 31-12
31-11
31 Custom Help and Documentation
Contents.m files contain only comment lines. The first two lines are headers that
describe the folder. Subsequent lines list the program files in the folder, along with their
descriptions. Optionally, you can group files and include category descriptions. For
example, view the functions available in the codetools folder:
help codetools
Commands for creating and debugging code
MATLAB Version 9.3 (R2017b) 24-Jul-2017
Directory tools
mlintrpt - Run mlint for file or folder, reporting results in browser
visdiff - Compare two files (text, MAT, or binary) or folders
...
If you do not want others to see a summary of your program files, place an empty
Contents.m file in the folder. An empty Contents.m file causes help foldername to
report No help found for foldername. Without a Contents.m file, the help and
doc commands display a generated list of all program files in the folder.
31-12
Create Help Summary Files — Contents.m
Report is to check that an existing Contents.m file is up-to-date. However, it also checks
whether Contents.m exists, and can generate a new file based on the contents of the
folder. Follow these steps to create a file:
1 In the Current Folder browser, navigate to the folder that contains your program
files.
2 Click , and then select Reports > Contents Report.
3 In the report, where prompted to make a Contents.m file, click yes. The new file
includes the names of all program files in the folder, using the description line (the
first nonempty comment line) whenever it is available.
4 Open the generated file in the Editor, and modify the file so that the second comment
line is in this form:
Do not include any spaces in the date. This comment line enables the ver function to
detect the version information.
Note MATLAB does not include live scripts or functions when creating a Contents
Report.
1 In the Current Folder browser, navigate to the folder that contains the Contents.m
file.
2 Click , and then select Reports > Contents Report.
Note You cannot run reports when the path is a UNC (Universal Naming Convention)
path; that is, a path that starts with \\. Instead, use an actual hard drive on your system,
or a mapped network drive.
31-13
31 Custom Help and Documentation
You can make all the suggested changes by clicking fix all, or open the file in the Editor
by clicking edit Contents.m.
See Also
doc | help | ver
31-14
Customize Code Suggestions and Completions
For MATLAB to detect the function signature information, you must place
functionSignatures.json in the folder that contains the function code. You can
define signatures for multiple functions in the same file.
The functionSignatures.json file contains a single JSON object. JSON uses braces to
define objects, and refers to objects as collections of name and value pairs. Since these
terms are overloaded in context of function signatures, "property" is used instead of
"name." The JSON object in functionSignatures.json contains an optional schema
version and a list of function objects. Each function object contains a list of signature
objects, and each signature object contains an array of argument objects. JSON uses
brackets to define arrays.
To specify the optional schema version use _schemaVersion as the first property and
the version number as its value. Specify the version number as a JSON string in the
31-15
31 Custom Help and Documentation
Function Objects
To define information for a function, create a property that is the same as the function
name. Its value is a signature object on page 31-16.
{
"functionName1": { signatureObj1 },
"functionName2": { signatureObj2 }
}
To define information for a class method or package function, use the full name of the
function or method. Example properties are "MyClass.myMethod" or
"myPackage.myFunction". You can define multiple function signatures for the same
function by defining multiple function objects with the same property (function name). For
more information, see “Multiple Signatures” on page 31-24.
Signature Objects
A signature object defines the input and output arguments and supported platforms for
the function. The value of each property, except for the platforms property, is an array
of argument objects on page 31-17.
{
"functionName1":
{
"inputs": [ argumentObj1, argumentObj2 ]
}
}
31-16
Customize Code Suggestions and Completions
Argument Objects
Argument objects define the information for each of the input and output arguments.
{
"functionName1":
{
"inputs":
[
{"name":"in1", "kind":"required", "type":["numeric"]},
{"name":"in2", "kind":"required", "type":["numeric","integer","scalar"]}
]
}
}
The order that the inputs appear in the JSON file is significant. For example, in a call to
the functionName1 function, in1 must appear before in2.
31-17
31 Custom Help and Documentation
The name of the input or output argument, specified as a JSON string. This property and
value is required. The name property does not need to match the argument name in the
source code, but it is a best practice for it to match any help or documentation.
Example: "name":"myArgumentName"
The kind of argument, specified as a JSON string with one of the following values.
MATLAB uses the value of the kind property to determine if and when to display the
arguments within the function signature.
Value Description
required Argument is required, and its location is relative to other required
arguments in the signature object.
ordered Argument is optional, and its location is relative to the required and
preceding optional arguments in the signature object.
namevalue Argument is an optional name-value pair. Name-value pair arguments
occur at the end of a function signature, but the pairs can be specified
in any order.
Arguments that are required and ordered appear first in the function signature, and
are followed by any namevalue arguments.
required, ordered, and namevalue arguments are most common. You can also specify
the following values for kind.
• positional – Argument is optional if it occurs at the end of the argument list, but
becomes required to specify a subsequent positional argument. Any positional
arguments must appear with the required and ordered arguments, before any
namevalue arguments.
• flag – Argument is an optional, constant string, typically used as a switch. For
example, 'ascend' or 'descend'. Flags occur at the end of a function signature. All
flag arguments must appear before any namevalue arguments.
• properties – Argument is optional and is used to specify public, settable properties
of a different MATLAB class. Indicate the class using the argument object type
property. In code suggestions, these properties appear as name-value pairs. Any
properties arguments must be the last argument in the signature.
31-18
Customize Code Suggestions and Completions
Class or attributes of the argument, specified as a JSON string, list, or list of lists.
The type property can define what class the argument is and what attributes the
argument must have.
• To match one class or attribute, use a single JSON string. For example, if an argument
must be numeric, then specify "type":"numeric".
• To match all classes or attributes, use a list of JSON strings. For example, if an
argument must be both numeric and positive, then specify "type":["numeric",
">=0"].
• To match any of multiple classes or attributes, use a list of lists of JSON strings. For
the inner lists, MATLAB uses a logical AND of the values. For the outer list, MATLAB
uses a logical OR of the values. For example, if an argument must be either a positive
number or a containers.Map object, then specify "type":[["numeric",
">=0"],["containers.Map"]].
31-19
31 Custom Help and Documentation
31-20
Customize Code Suggestions and Completions
"<expression"
"<=expression"
@(args) Must satisfy the function handle. For a value to satisfy the
expression function handle, the handle must evaluate to true.
Indicator that an argument can be specified multiple times, specified as a JSON true or
false (without quotes). The default is false. If specified as true, the argument or set of
arguments (tuple) can be specified multiple times. A required repeating argument must
appear one or more times, and an optional repeating argument can appear zero or more
times.
Example: "repeating":true
For more complicated function signatures, the following properties are available for each
argument object.
List of platforms that support the argument, specified as a JSON string. The default is all
platforms. Elements of the list must match an archstr returned from the computer
function. The list can be inclusive or exclusive, but not both.
Definition of a set of arguments that must always appear together, specified as a list of
argument objects. This property is only used to define sets of multiple repeating
31-21
31 Custom Help and Documentation
arguments. For these function signatures, define tuples and set the repeating property
to true.
Definition of a set of sets of arguments that cannot be used together, specified as a list of
argument objects. This property is used to provide information about functions with
multiple function signatures. However, typically it is easier to define multiple function
signatures using multiple function objects. For more information, see “Multiple
Signatures” on page 31-24.
Create a function whose signature you will describe in a JSON file in later steps. The
following function accepts:
myFunc is presented to demonstrate code suggestions and does not include argument
checking.
% myFunc Example function
% This function is called with any of these syntaxes:
%
% myFunc(in1, in2) accepts 2 required arguments.
% myFunc(in1, in2, in3) also accepts an optional 3rd argument.
% myFunc(___, NAME, VALUE) accepts one or more of the following name-value pair
% arguments. This syntax can be used in any of the previous syntaxes.
% * 'NAME1' with logical value
% * 'NAME2' with 'Default', 'Choice1', or 'Choice2'
function myFunc(reqA,reqB,varargin)
% Initialize default values
NV1 = true;
NV2 = 'Default';
posA = [];
if nargin > 3
if rem(nargin,2)
posA = varargin{1};
V = varargin(2:end);
else
V = varargin;
end
for n = 1:2:size(V,2)
31-22
Customize Code Suggestions and Completions
switch V{n}
case 'Name1'
NV1 = V{n+1};
case 'Name2'
NV2 = V{n+1}
otherwise
error('Error.')
end
end
end
end
In the same folder as myFunc, create the following function signature description in a file
called functionSignatures.json. The input names do not match the names in the
body of myFunc, but are consistent with the help text.
{
"_schemaVersion": "1.0.0",
"myFunc":
{
"inputs":
[
{"name":"in1", "kind":"required", "type":["numeric"], "purpose":"ID of item"},
{"name":"in2", "kind":"required", "type":["numeric"], "purpose":"# Items"},
{"name":"in3", "kind":"ordered", "type":["numeric"], "purpose":"Input Value"},
{"name":"Name1", "kind":"namevalue", "type":["logical","scalar"],"purpose":"Option"},
{"name":"Name2", "kind":"namevalue", "type":["char", "choices={'Default','Choice1','Choice2'}"]}
]
}
}
MATLAB uses this function signature description to inform code suggestions and
completion.
To experiment with code suggestions, start to call the function from a live script and
observe the suggestions. For example, the names and purposes from the JSON file appear.
MATLAB indicates when arguments are optional and if there are multiple suggestions
available (such as the third positional argument or a name-value pair). Name-value pairs
options are listed.
When adding a name-value pair argument to the function call, MATLAB presents the
choices from the JSON file. Since 'Name1' is defined as a logical scalar, MATLAB
31-23
31 Custom Help and Documentation
populates the choices automatically (true or false). MATLAB takes the three values for
the 'Name2' argument from the JSON file.
Multiple Signatures
If a function has many syntaxes, it can be helpful for code suggestions to group syntaxes
as multiple function signatures (regardless of the implementation of the function). To
provide code suggestions and completions for multiple signatures, create multiple
function objects with the same property in the JSON file.
Consider the following function that follows different code paths depending on the class
of the second input. This function is presented as an example for code suggestions, and,
therefore, does not perform any computations or error checking.
function anotherFunc(arg1,arg2,arg3)
switch class(arg2)
case 'double'
% Follow code path 1
case {'char','string'}
% Follow code path 2
otherwise
error('Invalid syntax.')
end
end
From a code suggestions perspective, consider the function as having two function
signatures. The first signature accepts two required numeric values. The second
signature accepts a required numeric, followed by a character or string, and finally a
required numeric. To define multiple function signatures, define multiple function objects
in the JSON file with the same property (function name).
{
"_schemaVersion": "1.0.0",
31-24
See Also
"anotherFunc":
{
"inputs":
[
{"name":"input1", "kind":"required", "type":["numeric"]},
{"name":"input2", "kind":"required", "type":["numeric"]}
]
},
"anotherFunc":
{
"inputs":
[
{"name":"input1", "kind":"required", "type":["numeric"]},
{"name":"input2", "kind":"required", "type":[["char"],["string"]]},
{"name":"input3", "kind":"required", "type":["numeric"]}
]
}
}
See Also
validateFunctionSignaturesJSON
More About
• “Check Syntax as You Type”
31-25
31 Custom Help and Documentation
External Websites
• https://www.json.org/
31-26
Display Custom Documentation
Overview
If you create a toolbox that works with MathWorks products, even if it only contains a few
functions, you can include custom documentation in the form of HTML help files. Custom
documentation for your toolbox can include figures, diagrams, screen captures,
equations, and formatting to make your toolbox help more usable.
• HTML help files — These files contain your custom documentation information.
• info.xml file — This file enables MATLAB to find and identify your HTML help files.
• helptoc.xml file — This file contain the Table of Contents for your documentation
that displays in the Contents pane of the Help browser. This file must be stored in the
folder that contains your HTML help files.
• Search database (optional) — These files enable searching in your HTML help files.
To view your custom documentation, open the Help browser and navigate to the home
page. At the bottom right of the home page, under Supplemental Software, click the
name of your toolbox. Your help opens in the current window.
31-27
31 Custom Help and Documentation
• Create a live script (*.mlx) and export it to HTML. For more information, see “Share
Live Scripts and Functions” on page 19-77.
• Create a script (*.m), and publish it to HTML. For more information, see “Publish and
Share MATLAB Code” on page 23-2.
Store all your HTML help files in one folder, such as an html subfolder in your toolbox
folder. This folder must be:
• A roadmap page (that is, an initial landing page for the documentation)
• Examples and topics that explain how to use the toolbox
• Function or block reference pages
31-28
Display Custom Documentation
To create info.xml to describe your toolbox, you can adapt this template:
<productinfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="optional">
<?xml-stylesheet type="text/xsl"href="optional"?>
<matlabrelease>R2016b</matlabrelease>
<name>MyToolbox</name>
<type>toolbox</type>
<icon></icon>
<help_location>html</help_location>
</productinfo>
You can also create info.xml by using the template info_template.xml included with
the MATLAB documentation. To create and edit a copy of the template file in your current
folder, run this code in the command window:
copyfile(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','templates','info_template.xml'),pwd)
fileattrib('info_template.xml','+w')
edit('info_template.xml')
The following table describes the required elements of the info.xml file.
31-29
31 Custom Help and Documentation
You also can include comments in your info.xml file, such as copyright and contact
information. Create comments by enclosing the text on a line between <!-- and -->.
Note MATLAB parses the info.xml file and displays your documentation when you
add the folder that contains info.xml to the path. If you created an info.xml file in
a folder already on the path, remove the folder from the path. Then add the folder
31-30
Display Custom Documentation
again, so that MATLAB parses the file. Make sure that the folder you are adding is not
your current folder.
You can create a helptoc.xml file by using the template included with the MATLAB
documentation. To create and edit a copy of the template file helptoc_template.xml in
your current folder, run this code in the Command Window:
copyfile(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','templates','helptoc_template.xml'),pwd)
fileattrib('helptoc_template.xml','+w')
edit('helptoc_template.xml')
Place the helptoc.xml file in the folder that contains your HTML documentation files.
This folder must be referenced as the <help_location> in your info.xml file.
Each <tocitem> entry in the helptoc.xml file references one of your HTML help files.
The first <tocitem> entry in the helptoc.xml file serves as the initial landing page for
your documentation.
Within the top-level <toc> element, the nested <tocitem> elements define the structure
of your table of contents. Each <tocitem> element has a target attribute that provides
the file name. File and path names are case-sensitive.
31-31
31 Custom Help and Documentation
</toc>
This helptoc.xml file, paired with a properly formulated info.xml file, produced this
display in the Help browser.
31-32
Display Custom Documentation
For example, suppose that your HTML files are in C:\MATLAB\MyToolbox\html. This
command creates a searchable database for those files:
builddocsearchdb('C:\MATLAB\MyToolbox\html')
You can search for terms in your toolbox from the Search Documentation field in the
Help browser.
31-33
31 Custom Help and Documentation
Beginning with MATLAB R2014b, you can maintain search indexes side by side. For
instance, if you already have a search index for MATLAB R2014a or earlier, run
builddocsearchdb against your help files using MATLAB R2014b. Then, when you run
any MATLAB release, the help browser automatically uses the appropriate index for
searching your documentation database.
When MATLAB finds an info.xml file on the search path or in the current folder, it
automatically validates the file against the supported schema. If there is an invalid
construct in the info.xml file, MATLAB displays an error in the Command Window. The
error is typically of the form:
An info.xml validation error can occur when you start MATLAB or add folders to the
search path.
31-34
See Also
If you do not list required XML elements in the prescribed order, you receive an XML
validation error:
Often, errors result from incorrect ordering of XML tags. Correct the error by updating
the info.xml file contents to follow the guidelines in the MATLAB help documentation.
For a description of the elements you need in an info.xml file and their required
ordering, see “Create info.xml File” on page 31-29.
Suppose that you have a file named info.xml that has nothing to do with custom
documentation. Because this info.xml file is an unrelated file, if it causes an error, you
can safely ignore it. To prevent the error message from reoccurring, rename the
unrelated info.xml file. Alternatively, ensure that the file is not on the search path or in
the current folder.
Use the error message to isolate the problem or use any XML schema validator. For more
information about the structure of the info.xml file, consult its schema at
matlabroot/sys/namespace/info/v1/info.xsd.
If you have an info.xml file from a different version of MATLAB, that file could contain
constructs that are not valid with your version. To identify an info.xml file from another
version, look at the full path names reported in the error message. The path usually
includes a version number, for example, ...\MATLAB\R14\.... In this situation, the
error is not actually causing any problems, so you can safely ignore the error message. To
ensure that the error does not reoccur, remove the offending info.xml file. Alternatively,
remove the outdated info.xml file from the search path and out of the current folder.
See Also
Related Examples
• “Display Custom Examples” on page 31-37
• “Create and Share Toolboxes” on page 25-13
31-35
31 Custom Help and Documentation
31-36
Display Custom Examples
1 Create your example files. Store the files in a folder that is on the MATLAB search
path, but outside the matlabroot folder.
Tip MATLAB includes a feature that converts scripts or functions to formatted HTML
files, which you can display as examples. To create these HTML files in MATLAB, use
either of these two methods:
• Create a live script (*.mlx) and export it to HTML. For more information, see
“Share Live Scripts and Functions” on page 19-77.
• Create a script (*.m), and publish it to HTML. For more information, see “Publish
and Share MATLAB Code” on page 23-2.
2 Create a demos.xml file that describes the name, type, and display information for
your examples.
For example, suppose that you have a toolbox named My Sample, which contains a
script named my_example that you published to HTML. This demos.xml file allows
you to display my_example:
<?xml version="1.0" encoding="utf-8"?>
<demos>
<name>My Sample</name>
<type>toolbox</type>
<icon>HelpIcon.DEMOS</icon>
<description>This text appears on the main page for your examples.</description>
<website><a href="https://www.mathworks.com">Link to your Web site</a></website>
<demosection>
<label>First Section</label>
<demoitem>
31-37
31 Custom Help and Documentation
</demos>
Within the demos.xml file, the root tag is <demos>. This tag includes elements that
determine the contents of the main page for your examples.
In previous releases, this icon was the icon for your example.
In those releases, you can use a standard icon,
HelpIcon.DEMOS. Or, you can provide a custom icon by
specifying a path to the icon relative to the location of the
demos.xml file.
31-38
Display Custom Examples
Optionally, define categories for your examples by including a <demosection> for each
category. If you include any categories, then all examples must be in categories.
Each <demosection> element contains a <label> that provides the category name, and
the associated <demoitem> elements.
31-39
31 Custom Help and Documentation
31-40
32
Projects
Create Projects
Projects can help you organize your work and collaborate. Projects promote productivity
and teamwork by helping you with common tasks.
Create Project
To create a blank project, on the Home tab, click New > Project > Blank Project. To
create a project from an existing folder, on the Home tab, click New > Project > From
Folder.
The New Project dialog box opens. Enter a project name, select a project folder, and click
Create.
32-2
Create Projects
Open Project
To open an existing project, on the Home tab, click Open and browse to an existing
project .prj file. Alternatively, in the Current Folder browser, double-click the
project .prj file.
Note To avoid conflicts, you can have only one project open at a time. If you open
another project, any currently open project closes.
To open a recent project, on the Home tab, click the Open arrow and select your project
under the Recent Projects list.
Set up Project
After you create a project, the Welcome to your project dialog box opens and prompts you
to set up the project.
32-3
32 Projects
2 In the Set Up Project (Step 1 of 2) dialog box, you can choose folders to add to the
project path. Adding project folders to the project path ensures that all users of the
project can access the files within them. MATLAB adds these folders to the search
path when you open the project, and removes them when you close the project.
To add all the folders in project folder to the project path, click Add with Subfolders
and then select the root project folder containing all your subfolders.
For more information about adding folders to the project path, including how to add
them after completing the project setup, see “Specify Project Path” on page 32-9.
32-4
Create Projects
3 After specifying the project path, click the Next button to continue.
4 In the Set Up Project (Step 2 of 2) dialog box, you can specify startup and shutdown
files. Startup files help you set up the environment for your project. Shutdown files
help you clean up the environment when you are done. Use shutdown files to undo
the setup that occurs in startup files.
Use the Add and Remove buttons to manage the startup and shutdown file lists. The
files run from the top down. If the order in which the files are run is important, use
the arrow buttons to move files up or down in the list.
For more information about specifying startup and shutdown files, including how to
specify them after completing the project setup, see “Specify Startup and Shutdown
Files” on page 32-10.
32-5
32 Projects
5 Click Finish to complete the project setup and open your new project.
To create a new file or folder in the project, in the Files view, right-click in white space
and select New Folder or New File. The new file or folder is created and added to the
project.
32-6
Create Projects
To add existing files to a project, on the Project tab, in the File section, click Add
Files. Then, select from the list of files in the project folder that are not yet added to the
project. To add existing files from the Files view, click All. Then, right-click one or more
files and folders, and select Add to Project or Add Folder to Project (Including Child
Files). To add existing files from a file browser or the Current Folder browser, cut and
paste or drag and drop the files into the project Files view. If you drag a file from outside
the project root folder, this moves the file and adds it to your project. Drag files within the
project root to move them.
To add and remove project files programmatically, use the addFile function.
You might not want to include all files in your project. For example, you might want to
exclude some files in the project root folder, such as SVN or CVS source control folders.
To determine which files need to be included in your project, see “Analyze Project
Dependencies” on page 32-38.
Some projects are shared as archived projects. An archived project is useful for sharing
with users who do not have access to a connected source control tool. To view and edit
the contents of an archived project, create a new project from the archived project.
To create a new project from an archived project, in the Current Folder browser, double-
click the archived project file which has an .mlproj extension. The Extract Project dialog
box opens. Specify the location for the new project and click Select Folder. For example,
C:\myNewProject.
The new project opens. The current folder (for example, C:\myNewProject) contains the
imported project folders. If the archived project contains referenced projects, MATLAB
32-7
32 Projects
imports files into two subfolders, mains and refs. The mains project folder (for
example, C:\myNewProject\mains) contains the project folders. The refs folder (for
example C:\myNewProject\refs) contains the referenced project folders.
If you have Simulink, you can use a Simulink template to create and reuse a standard
project structure.
1 On the Home or Project tab, click New > Project > From Simulink Template.
The Simulink Start Page opens.
2 The start page shows all project templates (*.sltx) on the MATLAB path. Select a
template in the list to read the template description.
If your templates do not appear, locate them by clicking Open. In the Open dialog
box, make *.sltx files visible by setting the file type to All MATLAB files, and
browse to your template.
3 Select a template and click Create Project. The Create Project dialog box opens.
Templates created in R2017b or later warn you if required products are missing.
Click the links to open Add-On Explorer and install required products.
4 Specify the root project folder, edit the project name, and click Create Project.
To use project templates created in R2014a or earlier (.zip files), upgrade them
to .sltx files using Simulink.exportToTemplate.
See Also
More About
• “Manage Project Files” on page 32-12
• “Analyze Project Dependencies” on page 32-38
• “Share Projects” on page 32-28
• “Use Source Control with Projects” on page 32-48
• “Create and Edit Projects Programmatically” on page 32-63
32-8
Automate Startup and Shutdown Tasks
More specifically, when you open a project, MATLAB changes the current folder to the
project startup folder, and runs (.m and .p files) or loads (.mat files) any specified
startup file. For more information about configuring the startup folder, see “Set Startup
Folder” on page 32-9.
To add a folder to the project path, on the Project tab, in the Environment section, click
Project Path. Click Add Folder and select the folder that you want to add. To add a
folder and all of its subfolders, click Add with Subfolders.
To remove a folder from the project path, select the folder from the displayed list and
click Remove.
You also can add or remove a folder from the project Files view. Right-click the folder,
select Project Path, and select from the available options.
Folders on the project path appear with the project path icon in the Status column of
the Files view.
To edit the project startup folder, on the Project tab, in the Environment section, click
Details. Then, in the Start up section, enter a path for the project startup folder.
32-9
32 Projects
To stop a file from running at startup or shutdown, right-click the file and select Remove
from Startup or Remove from Shutdown.
Alternatively, go to the Project tab and click Startup Shutdown. Then, in the Manage
Project Startup and Shutdown dialog box, use the Add and Remove buttons to manage
the startup and shutdown file lists. The files run from the top down. If the order in which
the files are run is important, use the arrow buttons to move files up or down in the list.
Note Startup and shutdown files are included when you commit modified files to source
control. When you configure startup and shutdown files, they run for all other project
users.
Startup files can have any name except startup.m. A file named startup.m on the
MATLAB path runs when you start MATLAB. If your startup.m file calls the project, an
error occurs because the project is not yet loaded. For more information about using
startup.m files, see “Startup Options in MATLAB Startup File”.
To create new startup and shutdown files programmatically, see addStartupFile and
addShutdownFile.
In Simulink, you can specify additional project startup options. For more information, see
“Automate Startup Tasks” (Simulink).
See Also
addStartupFile
More About
• “Manage Project Files” on page 32-12
• “Create and Edit Projects Programmatically” on page 32-63
32-10
See Also
32-11
32 Projects
Action Procedure
View project files. In the Files view, click Project to display only the files
and folders that are included in the project.
View all files in a project folder. To display all files and folders in the project folder, in
the Files view, click All.
You also can paste or drag files and folders from your
operating system file browser or the Current Folder
browser to the project Files view. When you drag a file
to the Files view, MATLAB adds the file to the project.
32-12
Manage Project Files
Action Procedure
Rename project files or folders. In the Files view, right-click the file, and then click
Rename.
Open project files. In the Files view, right-click the file, and then click
Open.
When renaming files, the project offers to automatically update references to the file.
Automatically updating references to a file when renaming prevents errors that result
from changing names or paths manually and overlooking or mistyping the name.
For example:
• When renaming a class, the project offers to automatically update all classes that
inherit from it.
32-13
32 Projects
• When renaming a .m or .mlx file, the project offers to automatically update any files
and callbacks that call it.
• When renaming a C file, the project prompts you to update the S-function that uses it.
For more information about automatic updates when renaming, deleting, or removing
Simulink files such as library links, model references, and model callbacks, see
“Automatic Updates When Renaming, Deleting, or Removing Files” (Simulink).
See Also
More About
• “Find Project Files” on page 32-15
• “Add Labels to Project Files” on page 32-20
• “Create Custom Tasks” on page 32-23
32-14
Find Project Files
For example, to group files by type, in the Layout field, select List. Then, click the
Actions button and select Group By > Type. To sort the grouped files by size, click
the actions button and select Sort By > Size.
To search for a file or folder, in the search field at the top of the view, begin typing the file
or folder name. The asterisk character (*) is a wildcard. For example, to show only file
names that begin with coll and have a .m extension, type coll*.m.
To filter the files and folders in the current view, click the Filter button next to the
search field. In the Filter Builder dialog box, select the names, file types, project statuses,
and labels to filter by.
Click Apply to filter the files and folders in the current view. The search field displays the
applied filter.
32-15
32 Projects
To clear a search or filter, click the Clear button to the right of the search field.
2 Enter the text to search. MATLAB searches the project files for the exact text in the
search field and displays the results.
32-16
See Also
4 Double-click the search result to open the file and locate the match.
5 Click Filters at the bottom left of the results to refine the results by file type, status,
or label.
See Also
More About
• “Manage Project Files” on page 32-12
• “Create Shortcuts to Frequent Tasks” on page 32-18
• “Add Labels to Project Files” on page 32-20
32-17
32 Projects
Run Shortcuts
To run a shortcut, in the Project Shortcuts tab, click the shortcut. Clicking a shortcut in
the Project Shortcuts tab performs the default action for the file type. For example,
MATLAB runs .m shortcut files and loads .mat shortcut files. If the shortcut file is not on
the path, MATLAB changes the current folder to the parent folder of the shortcut file,
runs the shortcut, and then changes the current folder back to the original folder.
Alternatively, in the Files view, you can right-click the shortcut file and select Run. If the
script is not on the path, then MATLAB asks if you want to change folder or add the folder
to the path.
Create Shortcuts
To create a shortcut from an existing project file:
1 In the Files view, right-click the file and select Create Shortcut. Alternatively, on the
Project Shortcuts tab, click New Shortcut and browse to select a file.
The shortcut appears with the selected name and icon on the Project Shortcuts tab. In
the Files view, the Status column displays an icon indicating that the file is a
shortcut.
Note Shortcuts are included when you commit your modified files to source control, so
you can share shortcuts with other project users.
32-18
See Also
Organize Shortcuts
You can organize shortcuts by categorizing them into groups. For example, you might
create one group of shortcuts for loading data, another for opening files, another for
generating code, and another for running tests.
1 On the Project Shortcuts tab, right-click a shortcut and select Edit Shortcut.
Alternatively, in the Files view, right-click a file and select Edit Shortcut.
See Also
More About
• “Manage Project Files” on page 32-12
• “Find Project Files” on page 32-15
• “Add Labels to Project Files” on page 32-20
32-19
32 Projects
Add Labels
To add a label to a project file, in the Files view, select the file. Then, drag the desired
label from the Labels panel at the bottom left of the project into the Label Editor panel
for the selected file. The Label Editor panel is located at the bottom right of the Files
view. To restore the panel if it is minimized, click the icon.
To add a label to multiple project files, in the Files or Dependency Analysis view, select
the files, right-click, and select Add Label. Choose a label from the list and click OK.
Note After you add a label to a file, the label persists across file revisions.
To add labels programmatically (for example, in custom task functions) see addLabel.
To change a label that belongs to a single-valued category, select the new value from the
label list.
32-20
Add Labels to Project Files
You can add additional annotations to labels from categories that you create. In the Label
Editor panel, click a label and insert or modify text. Then, click Apply.
Create Labels
Labels exist in two types of categories:
• Single-valued — You can attach only one label from the category to a file.
• Multi-valued — You can attach multiple labels from the category to a file.
All projects contain a built-in label category called Classification with several built-in
labels. These built-in labels are read-only.
1 In the Labels panel at the bottom left of the project, right-click and select Create
New Category. The Create Category dialog box opens.
2 Enter a name for the new category.
3 To create a single-valued label category, select the Single Valued check box.
Otherwise, MATLAB creates a multi-valued label category.
32-21
32 Projects
4 To specify a label data type other than the default String data type, from the Type
list, select from the available options.
5 Click Create.
1 In the Labels panel at the bottom left of the project, right-click the label category
and select Create New Label. The Create Label dialog box opens.
2 Enter a name for the new label and click OK.
See Also
More About
• “Manage Project Files” on page 32-12
• “Find Project Files” on page 32-15
32-22
Create Custom Tasks
For example, this custom task function extracts Code Analyzer information for each
file using the checkcode function.
[~,~,ext] = fileparts(file);
switch ext
case {'.m', '.mlx', '.mlapp'}
result = checkcode(file, '-string');
otherwise
result = [];
end
5 Save the file.
You can use the MATLAB editor to set breakpoints and debug a custom task function, just
as with any other MATLAB function.
32-23
32 Projects
1 On the Project tab, click Custom Tasks and then select Run Custom Task.
2 In the Include column of the table, select which project files you want to run the
custom task on.
To include or exclude multiple files in the table at once, press the Shift or Ctrl key,
select the files, and then right-click and select Include or Exclude. If the custom
task function can identify the files to operate on, include all files.
3 In the Custom task field, select from the available custom task functions. You also
can enter the name of a task directly in the field, or click Browse.
4 Click Run Task to run the task. The Custom Task Report window displays the results.
5 Examine the Results column in the table to ensure that the custom task ran correctly
on all files. To view detailed result information for a file, select the file in the table.
The results pane at the bottom of the Custom Task Report displays the details.
To save the Custom Task Report, click the Publish Report button at the bottom of the
Custom Task Report. You can either save the report as an HTML file or a Microsoft Word
file. If you have MATLAB Report Generator™, you also can save the report as a PDF file.
To see the report file and add it to your project, switch to the All files view.
See Also
More About
• “Create Shortcuts to Frequent Tasks” on page 32-18
• “Manage Project Files” on page 32-12
32-24
Componentize Large Projects
Projects can reference multiple other projects in a hierarchical manner. The project
reference hierarchy appears as a tree in the References view.
• Access the project paths, entry-point shortcuts, and source control information for all
referenced projects.
• View, edit, and run files that belong to a referenced project.
• Detect changes in referenced projects using checkpoints.
When the referenced project loads, MATLAB adds the referenced project path to the
MATLAB search path and then runs or loads specified startup files. Similarly, when the
32-25
32 Projects
referenced project closes, MATLAB removes the project path from the search path and
runs specified shutdown files. MATLAB loads referenced projects before their parent
projects. This allows the parent project to access the referenced project in startup and
shutdown files.
To remove a referenced project from your project hierarchy, in the References tree,
right-click the referenced project and select Remove Reference.
To view a referenced project, in the parent project, select the References view. In the
References tree, select a referenced project.
To display the referenced project files, at the top right of the References view, click
Show Files.
To modify or run a file, right-click the file and select from the list of available options.
To extract a folder from a project and convert the folder into a referenced project:
1 In the Files view, right-click the folder and select Extract to Referenced Project.
The Extract Folder to New Project dialog box opens.
2 Specify a project name and location
3 In the Reference type field, select either Relative or Absolute. Select Relative if
you specify the new project location with reference to the current project root. Select
Absolute if you specify the full path for the new location, which is, for example, on a
network drive
4 To disable any of the default content migration actions, click More Options and clear
the corresponding check boxes.
5 Click Extract.
32-26
See Also
The selected folder and its contents are removed from the project. On the Project
Shortcuts tab, the Referenced Projects section shows a new shortcut for the
referenced project.
To remove the checkpoint, in the Checkpoint section of the References tab, click
Clear.
See Also
More About
• “Create Projects” on page 32-2
• “Share Projects” on page 32-28
32-27
32 Projects
Share Projects
You can collaborate with others by packaging and sharing projects. You can also control
which files are included in a shared project by using labels and an export profile. (For
more information, see “Create an Export Profile” on page 32-32.)
To archive a project:
You can now share the archive file the way you would any
other file.
32-28
Share Projects
32-29
32 Projects
You can now share the toolbox file the way you would any
other file.
32-30
Share Projects
32-31
32 Projects
Before sharing a project with others, it can be useful to examine the required toolboxes
for your project by performing a dependency analysis. For more information, see “Find
Required Toolboxes” on page 32-43.
Note Export profiles do not apply changes to referenced projects. When you share your
project, MATLAB exports the entire referenced projects.
32-32
See Also
See Also
More About
• “Add Labels to Project Files” on page 32-20
• “Create Projects” on page 32-2
• “Analyze Project Dependencies” on page 32-38
32-33
32 Projects
Upgrade Projects
The Upgrade Project tool helps you check for compatibility issues or upgrade your project
to the current MATLAB release. The tool applies fixes automatically when possible and
produces a report.
Tip To perform an upgrade that you can later revert if necessary, add source control to
your project before upgrading. For more information, see “Use Source Control with
Projects” on page 32-48.
32-34
Upgrade Projects
If you want to run the Project Upgrade tool without applying fixes automatically, clear
Apply upgrades automatically.
If you want to specify which files to upgrade and which checks to run, click Change
Options. In the Upgrade Options dialog box, clear the check box for any file or check
that you want to exclude from the upgrade. For example, you might want to exclude
checks that require performing an update diagram, as this can be time consuming.
The Upgrade Project tool displays the results of the compatibility check or upgrade in the
Upgrade Project Report.
32-35
32 Projects
The summary shows how many files passed all of the upgrade checks and how many files
require attention. To view the upgrade results for a file, select the file in the left file list.
By default, the file list shows any files that need attention. To change which files are
shown, select from the options in the Show drop-down.
32-36
See Also
For each file in the Project Upgrade Report, examine the checks marked as needing
attention. These files are marked with an orange circle icon in the Result column. Select
a check in the Check Name column to display the check results and any automatically
applied fixes in the lower panel.
To see the differences before and after the upgrade, in the Upgrade Project Report, click
View Changes. If your project is under source control, you also can see the differences
by comparing the before and after versions of the file.
MATLAB saves an HTML report of the upgrade results in the project root folder. To open
the saved report, click the Report link at the top of the Upgrade Project Report.
See Also
More About
• “Use Source Control with Projects” on page 32-48
• “Analyze Project Dependencies” on page 32-38
32-37
32 Projects
Running a dependency analysis can show you how a change affects other files before you
make the change. For example, it is common to use dependency analysis to examine your
project structure, to find, investigate, and resolve problems in project files, or to find the
toolboxes required by the project. When investigating the potential impact of a change in
requirements, use dependency analysis to find the design files linked to the requirements
document. You also can use it to investigate change set impact by finding upstream and
downstream dependencies of modified files before committing the changes. Finding these
dependencies can help you identify design and test files that need modification and help
you find the tests you need to run.
After dependency analysis, you can open or label the files. You can also export the results
as workspace variables, images, or reloadable files. And you can send these files for
custom task processing. Exporting the results enables further processing or archiving of
impact analysis results. You can add the exported list of files to reports or other
documents that describe the impact of a change.
To run a dependency analysis on all the files in your project, select the Dependency
To analyze only specific files, on the Dependency Analysis tab, click Analyze > Select
Files to Analyze. Select the files for analysis and then click Analyze.
32-38
Analyze Project Dependencies
• Your project structure and its file dependencies, showing how files such as models,
libraries, functions, data files, source files, and derived files relate to each other.
• Required products and toolboxes.
• Relationships between source and derived files (such as .m and .p files, , .slx
and .slxp, .ssc and .sscp, or .c and .mex files), and between C/C++ source and
header files. You can see what code is generated by each model, and find what code
needs to be regenerated if you modify a model.
• Warnings about problem files, such as missing files, files not in the project, files with
unsaved changes, and out-of-date derived files.
To examine project dependencies and problem files, at the top right of the Dependency
Analysis view, click Table View.
After you run the first dependency analysis of your project, subsequent analyses are
incremental updates. However, if you update external toolboxes and want to discover
dependency changes in them, you must perform a complete analysis. To perform a
32-39
32 Projects
complete analysis, go to the Dependency Analysis tab and click Analyze > Reanalyze
All.
For more information about running a dependency analysis on Simulink models and
libraries, see “Perform Impact Analysis” (Simulink).
Use the Impact View to investigate problem files graphically. The center of the view
displays the impact graph for your project. The right panel of the Impact View shows the
toolbox dependencies and a list of problem files.
• To show only the problem files in the graph, hover over the Problems heading in the
right pane of the Impact View and click Find All. To return to the full project view,
clear the filter in the search box (for instance, Dependencies of "filename").
• To see the details for a specific problem file, including the path, type, and problem
message, select the file in the graph. To clear the file selection and see all problem
files for a project, click a blank area of the graph.
• To view the dependencies of a file, hover over the problem file and click Find All. The
graph updates to display only the problem file and its dependencies. To return to the
full project view, clear the filter in the search box (for instance, problem=='*').
Use the Table View to investigate your project dependencies and problem files as a table.
To use the Table View, at the top right of the Dependency Analysis view, select Table
View.
• To view the files with problems at the top of the list, sort the table by the Problem
Description column.
• To view the problems for a file, check the message in the Problem Description
column for that file.
• To view the dependencies of a file, select the file in the table. The lower pane shows
the files that depend on the selected file. You can view either the Directly Impacted
Files or switch to the Directly Required Files.
32-40
Analyze Project Dependencies
Resolve Problems
For each problem file, take actions to resolve the problem. This table lists common
problems and describes how to fix them.
32-41
32 Projects
32-42
Analyze Project Dependencies
In the right pane of the Dependency Analysis view, the Product section displays the
required products for the whole project. To view products required by selected files,
select some files by clicking the graph or a legend.
To find which file is introducing a product dependency, hover over the product name and
click Find Usages.
32-43
32 Projects
The graph updates to show only the files using the selected product. To return to the full
project view, clear the filter in the search box (for instance, Usages of
"productname").
If a required product is missing, the product list labels it Missing. To resolve a missing
product, install the product and rerun the dependency analysis.
Alternatively, select the file, go to the Dependency Analysis tab, and, in the Impact
Analysis section, click Find. Select from the available options.
To select groups of files, in the File Type legend at the top-left corner of the Impact
View, select from the available options. For example, to select all function files, click File
Type and select Function. To change how files in the view are grouped, go to the
32-44
Analyze Project Dependencies
Dependency Analysis tab, and in the View section, click Group By. Select from the
available options.
To select modified files, problem files, or external files, go to the Dependency Analysis
tab, and in the Impact Analysis section, click Select. Select from the available options.
To examine or edit a file dependency, expand the file by clicking the arrows next to the file
name in the graph. Then, double-click the referencing component (such as a line of
MATLAB code that references a problem file) to open it. To expand all files in the graph,
in the View section, click Expand All.
To reset the graph to show all analyzed dependencies in the project, on the Dependency
Analysis tab, in the Impact Analysis section, select Find > All Files.
Tip For large projects, viewing the results in a table can make navigation easier. To use
the Table View, at the top right of the Dependency Analysis view, select Table View.
• You can view and navigate to and from the linked requirements documents.
• You can create or edit Requirements Management links only if you have Simulink
Requirements™.
To highlight requirements documents in the graph, at the top right of the Impact View,
click the Dependency Type legend and select Requirements Link. Arrows connect
requirements documents to the files with the requirement links.
To find the requirement links for a specific file, expand the file by clicking the arrows next
to the file name in the graph. To expand all files in the graph, in the View section, click
Expand All. An arrow connects the component containing the requirement link to the
requirements document.
32-45
32 Projects
To save your results as a .graphml file, go to the Dependency Analysis tab, and in the
File section, click Save As. Choose a file name and location.
To open a saved dependency analysis result, on the Dependency Analysis tab, in the
File section, click Open.
To compare the results of a current dependency analysis with previously saved results, go
to the Dependency Analysis tab, and in the File section, click Compare to Saved.
Select a .graphml file and inspect the differences in the comparison report.
To export all the files displayed in the Impact View of a dependency analysis, first, click
the graph background to clear the selection on all files. Then, go to the Dependency
Analysis tab, and in the Impact Analysis section, click Export. Select from the available
options:
To export a subset of files in the graph, select the files, and then click Export. The menu
displays how many files are selected: Selected files: number of files . Alternatively, you
can right-click the selected files, and select from the options in the Export menu.
To export the Impact View graph as an image file for sharing or archiving, go to the
Dependency Analysis tab, and in the File section, select Save As > Save As Image.
Use the Save dialog box to specify the name, file type, and location. The default file type
is SVG, which supports image scaling.
You also can copy the image to the clipboard using the keyboard. You then can paste the
clipboard contents into other documents.
32-46
See Also
See Also
More About
• “Share Projects” on page 32-28
• “Use Source Control with Projects” on page 32-48
32-47
32 Projects
Projects integrate with two source control systems, Git and Subversion (SVN).
Then, when your project is under source control, you can perform operations from within
MATLAB such as checking files in and out, running checks, and committing and reverting
changes.
Create a new local copy of a project from an existing repository by retrieving files from
source control. You can clone a Git repository, or check out files from an SVN repository,
or use another source control integration.
1 On the Home tab, click New > Project > From Git or New > Project > From SVN.
The New Project From Source Control dialog box opens.
2 If you know your repository location, paste it into the Repository path field.
Otherwise, to browse for and validate the repository path to retrieve files from, click
Change.
32-48
Use Source Control with Projects
a In the dialog box, specify the repository URL by entering or pasting a URL into
the field, by selecting from the list of recent repositories, or by clicking the
button.
b Click Validate to check the repository path. If the path is invalid, check the URL
against your source control repository browser.
c If you see an authentication dialog box for your repository, enter login
information to continue.
d If necessary, select a deeper folder in the repository tree. With SVN, you might
want to check out from trunk or a branch folder under tags.
e When you have finished specifying the URL path you want to retrieve, click OK.
The dialog box closes and you return to the New Project From Source Control
dialog box.
3 In the Sandbox field, select the working folder where you want to put the retrieved
files for your new project. With SVN, use a local folder for best results, because using
a network folder is slow.
4 Click Retrieve.
If your repository already contains a project, the project is ready when the tool
finishes retrieving files to your selected sandbox folder.
If your sandbox does not yet contain a project, then a dialog box asks whether you
want to create a project in the folder. To create a project, specify a project name and
click OK. The Welcome screen appears to help you set up your new project. For more
information about setting up a project, see “Set up Project” on page 32-3.
If you encounter errors like OutOfMemoryError: Java heap space when cloning
large Git repositories, edit your MATLAB preferences to increase the heap size.
If you have an existing project, you can add it to Git or SVN source control.
32-49
32 Projects
1 On the Project tab, in the Source Control section, click Use Source Control. The
Source Control Information dialog box opens.
2 Click the Add project to source control button. The Add to Source Control dialog
box opens.
3 In the Source control tool list, select the right tool for your repository. If you choose
Git, then skip step 4 and go straight to step 5.
4 If you are using an SVN remote repository, click the Change button to select an
existing repository or create a new one.
•
To specify an existing repository, click the button to browse for your
repository, paste a URL into the field, or use the list to select a recent repository.
•
To create a new repository, click the button. For more information about
creating a new repository, see “Create New Repository” on page 33-7.
Click Validate to check the path to the selected repository, and then click OK.
5 Click Convert to finish adding the project to source control.
The project displays details of the current source control tool and the repository
location.
7 If you created a new repository, select the Files > Modified view and click Commit
to commit the first version of your files to the new repository. In the dialog box, enter
a comment if you want, and click Submit.
If you want to merge branches with Git, you need to follow additional setup steps. For
more information, see “Set Up Git Source Control” on page 33-30.
If you want to use a version of SVN other than the built-in version, see “Set Up SVN
Source Control” on page 33-18.
If you create a new project from a folder that is already under source control, MATLAB
can automatically add the new project to source control. After creating the project, click
the Detect button. For more information about creating a project from a folder, see
“Create Projects” on page 32-2.
32-50
Use Source Control with Projects
Creating a GitHub repository adds Git source control to your new or existing project. The
GitHub repository you create becomes the project remote repository. To create a GitHub
repository, you must have a GitHub account.
1 On the Home tab, click New > Project > From Git.
2 Select New > GitHub Repository. In the GitHub dialog box, enter your User name
and Password. Fill the Repository name and Description fields and click Create.
MATLAB creates a new public GitHub repository and populates the Repository path
field with information in the https://github.com/myusername/mynewrepository
format.
3 In the Sandbox field, specify the location for your sandbox. The selected folder must
be empty. Click Retrieve to create the sandbox.
After creating the GitHub repository and sandbox, add your files to the sandbox. See
“Mark Files for Addition to Source Control” on page 33-11. Commit the first version of
your files to your local repository, then push all modifications to your remote GitHub
repository.
Tip If you want to create a remote GitHub repository for an existing project, share your
project to GitHub instead.
With your project loaded, on the Project tab, select Share > GitHub. For detailed
instructions, see Publish on GitHub in “Share Projects” on page 32-28.
This table shows how to check for modified project files, update revisions, get and
manage file locks, and tag project files.
32-51
32 Projects
Action Procedure
Refresh status of project To check for locally modified files, on the Project tab, in
files. the Source Control section, click Refresh. Refreshing
queries the local sandbox state and checks for changes
made with another tool outside of MATLAB.
Using Git, to get the latest changes for all project files
from a source control repository and merge them into your
current branch, go to the Project tab, and in the Source
Control section, click Pull. To get changes and merge
manually, on the Project tab, in the Source Control
section, click Fetch. This updates all of the origin branches
in the local repository. When you click Fetch, your sandbox
files are not changed. To see the changes from others,
merge in the origin changes to your local branches. For
more information, see “Pull, Push and Fetch Files with Git”
on page 33-45.
32-52
Use Source Control with Projects
Action Procedure
Update revision for selected To update a selected set of files, in the Files view, right-
project files. click the files, and select the Source Control > Update
command for the source control system you are using. For
example, if you are using SVN, select Source Control >
Update from SVN to get fresh local copies of the selected
files from the repository.
Get SVN file locks. To get SVN file locks, in the Files view, select the files that
you want to check out. Right-click the selected files and
select Source Control > Get File Lock. A lock symbol
appears in the SVN source control column. Other users do
not see the lock symbol in their sandboxes, but they cannot
get a file lock or check in a change when you have the lock.
To view or break locks, on the Project tab, click Locks.
Get File Lock is only for SVN. Git does not have locks.
Manage SVN repository To manage global SVN locks for a repository, on the
locks. Project tab, in the Source Control section, click Locks.
For more information, see “Get SVN File Locks” on page
33-29.
Tag versions of project files. To identify specific revisions of all project files, on the
Project tab, in the Source Control section, click Tag.
Specify the tag text and click OK. The tag is added to
every project file. Errors appear if you do not have a tags
folder in your repository. For more information, see “Set
Up SVN Source Control” on page 33-18.
You can review changes in project files using the Files > Modified view. This table shows
how to view the list of modified project files, review the history of a file, and compare two
files' revisions.
32-53
32 Projects
Action Procedure
View modified project files. In the Files view, select the Modified (number of files)
tab. The Files > Modified view is visible only if you are
using source control with your project.
32-54
Use Source Control with Projects
Action Procedure
Compare revisions. In the Files view, right-click a file and select Compare >
Compare to Ancestor to run a comparison against the
local repository (Git) or against the last checked-out
version in the sandbox (SVN). The Comparison Tool
displays a report.
The files in the resources/project folder are project definition files generated when
you first create or make changes to your project. The project definition files allow you to
add metadata to files without checking them out, for example, by creating shortcuts,
adding labels, and adding a project description. Project definition files also specify the
files added to your project. These files are not part of the project.
Any changes you make to your project (for example, to shortcuts, labels, categories, or
files in the project) generate changes in the resources/project folder. These files
store the definition of your project in XML files whose format is subject to change.
You do not need to view project definition files directly, except when the source control
tool requires a merge. The files are shown so that you know about all the files being
committed to the source control system.
1 On the Home tab, in the Environment section, click Preferences. Select MATLAB
> Project and in the New Project section, select a Project definition files: option.
2 Create a .mlproj archive of the project. For more information, see “Share Projects”
on page 32-28 or export.
3 Create a new project from the archived project. For more information, see “Create
Projects” on page 32-2.
32-55
32 Projects
To stop managing your folder with a project and delete the resources/project folder,
see matlab.project.deleteProject.
To run checks for a project, go to the Project tab and click Run Checks > Check
Project. The project checks for problems with project integrity such as missing files,
unsaved files, or files not under source control. A dialog box reports the results. You can
click for details and follow prompts to fix problems.
If you want to check for required files, click Dependency Analysis to analyze the
dependencies of the modified files. Use the dependency tools to analyze the structure of
your project.
For details on problems the checks can fix, see “Work with Derived Files in Projects” on
page 32-60, and “Analyze Project Dependencies” on page 32-38.
After reviewing changes and running project checks, you are ready to commit your
modified project files to source control. This table shows how to commit modified project
files.
Action Procedure
Commit all modified files to In the Files view, select the Modified (number of files)
source control. tab. On the Project tab, in the Source Control section,
click Commit. Enter comments in the dialog box, and click
Submit. If you are using Git source control, this commits
to your local repository. If you are using SVN source
control, this commits changes to your repository.
32-56
Use Source Control with Projects
Action Procedure
Commit selected files to In the Files view, select the files, right-click, and select
source control. Source Control > Commit.
32-57
32 Projects
Action Procedure
Create a branch with Git. To create a branch, on the Project tab, in the Source
Control section, click Branches. The Branches dialog box
appears, where you can view, switch, create, and merge
branches.
32-58
Use Source Control with Projects
Action Procedure
Resolve conflicts. If you and another user change the same file in different
sandboxes or on different branches, a conflict message
appears when you try to commit your modified files.
Extract conflict markers if necessary, compare the
differences causing the conflict, and resolve the conflict.
For Git, the Branch status in the Git pane changes from
MERGING to SAFE.
Revert Changes
This table shows how to revert changes in project files. For more information about
reverting changes, see “Revert Changes in Source Control” on page 33-17.
32-59
32 Projects
Action Procedure
Revert local changes. To release locks and revert to the version in the last
sandbox update (that is, the last version you synchronized
or retrieved from the repository), in the Files view, right-
click the files to revert and select Source Control >
Discard Local Changes and Release Locks.
Once you have chosen a revision and are satisfied that the
information is correct, click Revert.
32-60
Use Source Control with Projects
folder to a project, the project checks advise you to remove this from the project and offer
to make the fix.
It is also a best practice is to exclude derived files from source control, because they can
cause problems. This includes .mex files, the contents of the resources folder, or other
code generation folders. For example:
• With a source control system that can do file locking, you can encounter conflicts. If
resources is under source control and you generate code, most of the files under
resources change and become locked. Other users cannot generate code because of
file permission errors. Locking binaries, such as .mex files, can have an impact on a
team.
• Deleting resources is often required. However, deleting resources causes
problems such as not a working copy errors if the folder is under some source
control tools (for example, SVN).
• If you want to check in the generated code as an artifact of the process, it is common
to copy some of the files out of the resources cache folder and into a separate
location that is part of the project. That way, you can delete the temporary cache
folder when you need to. Use the packNGo function to list the generated code files,
and use the project API to add them to the project with appropriate metadata.
• The resources folder can contain many small files. This can affect performance with
some source control tools when each of those files is checked to see if it is up-to-date.
In the Unsaved Changes dialog box, you can see all project files with unsaved changes. If
you have referenced projects, files are grouped by project. You can save or discard all
changes.
32-61
32 Projects
To control this behavior, on the Home tab, in the Environment section, click
Preferences. Go to MATLAB > Project and in the Project Shutdown section, select or
clear the check box labeled Check for open project models and close them, unless
they are dirty.
See Also
More About
• “About MathWorks Source Control Integration” on page 33-3
• “Set Up SVN Source Control” on page 33-18
• “Set Up Git Source Control” on page 33-30
32-62
Create and Edit Projects Programmatically
Create a working copy of the Times Table App example project files and open the project.
MATLAB® copies the files to an examples folder so that you can edit them. The project
puts the files under Git™ source control. Use currentProject to create a project object
from the currently loaded project.
matlab.project.example.timesTable
mainProject = currentProject;
files =
1×14 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
Use indexing to access files in this list. For example, get file number 10. Each file has
properties describing its path and attached labels.
mainProject.Files(10)
ans =
ProjectFile with properties:
Path: "C:\workSpace\examples\TimesTableApp\tests\tNewTimesTable.m"
Labels: [1×1 matlab.project.Label]
Revision: ""
32-63
32 Projects
SourceControlStatus: Unmodified
mainProject.Files(10).Labels
ans =
Label with properties:
File: "C:\workSpace\examples\TimesTableApp\tests\tNewTimesTable.m"
DataType: 'none'
Data: []
Name: "Test"
CategoryName: "Classification"
myfile = findFile(mainProject,"source/timestable.mlapp")
myfile =
ProjectFile with properties:
Path: "C:\workSpace\examples\TimesTableApp\source\timestable.mlapp"
Labels: [1×1 matlab.project.Label]
Revision: ""
SourceControlStatus: Unmodified
Create the Times Table Game project. This project will store the game logic behind the
Times Table App. The Times Table Game project will be used by the Times Table App
project through a project reference.
timesTableGameFolder = fullfile(mainProject.RootFolder,"refs","TimesTableGame");
timesTableGame = matlab.project.createProject(timesTableGameFolder);
timesTableGame.Name = "Times Table Game";
Move the Times Table App game logic from the main project folder to the new project
folder, and add it to the Times Table Game project. Then, remove the file from the Times
Table App project.
32-64
Create and Edit Projects Programmatically
movefile("..\..\source\timesTableGame.m");
addFile(timesTableGame,"timesTableGame.m");
reload(mainProject);
removeFile(mainProject,"source\timesTableGame.m");
Add the Times Table Game project root folder to the Times Table Game project path. This
makes the timesTableGame.m file available when the Times Table App project or any
project that references the Times Table App project is loaded.
reload(timesTableGame);
addPath(timesTableGame,timesTableGame.RootFolder);
Add the new Times Table Game project to the Times Table App project as a project
reference. This allows the Time Table App project to view, edit, and run files in the Times
Table Game project.
reload(mainProject);
addReference(mainProject,timesTableGame);
Get all the modified files in the Times Table App project. Compare this list with the Files
> Modified view in the project. You can see the files for the new Times Table Game
project, as well as the removed and modified files in the Times Table App project.
modifiedfiles = listModifiedFiles(mainProject)
modifiedfiles =
1×19 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
Get the second modified file in the list. Observe that the SourceControlStatus
property is Added. The listModifiedFiles function returns any files that are added,
modified, conflicted, deleted, and so on.
modifiedfiles(2)
32-65
32 Projects
ans =
ProjectFile with properties:
Path: "C:\workSpace\examples\TimesTableApp\refs\TimesTableGame\resou
Revision: ""
SourceControlStatus: Added
Refresh the source control status before querying individual files. You do not need to do
this before calling listModifiedFiles.
refreshSourceControl(mainProject)
Get all the project files that are Unmodified. Use the ismember function to get an array
of logicals stating which files in the Times Table App project are unmodified. Use the
array to get the list of unmodified files.
unmodifiedStatus = ismember([mainProject.Files.SourceControlStatus],matlab.sourcecontro
mainProject.Files(unmodifiedStatus)
ans =
1×9 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
Run a dependency analysis to update the known dependencies between project files.
updateDependencies(mainProject)
Get the list of dependencies in the Times Table App project. The Dependencies property
contains the graph of dependencies between project files, stored as a MATLAB digraph
object.
g = mainProject.Dependencies
g =
digraph with properties:
32-66
Create and Edit Projects Programmatically
Get the top-level files of all types in the graph. The indegree function finds all the files
that are not depended on by any other file.
top = g.Nodes.Name(indegree(g)==0)
Get the top-level files that have dependencies. The indegree function finds all the files
that are not depended on by any other file, and the outdegree function finds all the files
that have dependencies.
Find impacted (or "upstream") files by creating a transposed graph. Use the flipedge
function to reverse the direction of the edges in the graph.
transposed = flipedge(g)
32-67
32 Projects
transposed =
digraph with properties:
impacted = bfsearch(transposed,which('source/timestable.mlapp'))
Get information on the project files, such as the number of dependencies and orphans.
averageNumDependencies = mean(outdegree(g));
numberOfOrphans = sum(indegree(g)+outdegree(g)==0);
Change the sort order of the dependency graph to show project changes from the bottom
up.
ordered = g.Nodes.Name(flip(toposort(g)));
You can view the same dependency analysis graph in the Impact View of the project. To
plot the graph, save the impact view to an image file.
Query Shortcuts
You can use shortcuts to save frequent tasks and frequently accessed files, or to automate
startup and shutdown tasks.
shortcuts = mainProject.Shortcuts
shortcuts =
1×4 Shortcut array with properties:
Name
Group
File
32-68
Create and Edit Projects Programmatically
shortcuts(2)
ans =
Shortcut with properties:
shortcuts(3).File
ans =
"C:\workSpace\examples\TimesTableApp\utilities\openRequirementsDocument.m"
{shortcuts.File}'
Label files
Create a new category of labels of type char. In the Times Table App project, the new
Engineers category appears in the Labels pane.
createCategory(mainProject,'Engineers','char')
ans =
Category with properties:
Name: "Engineers"
SingleValued: 0
DataType: "char"
LabelDefinitions: [1×0 matlab.project.LabelDefinition]
32-69
32 Projects
category = findCategory(mainProject,'Engineers');
createLabel(category,'Bob');
ld = findLabel(category,'Bob')
ld =
LabelDefinition with properties:
Name: "Bob"
CategoryName: "Engineers"
Attach a label to a project file. If you select the file in the Times Table App project, you
can see this label in the Label Editor pane.
myfile = findFile(mainProject,"source/timestable.mlapp");
addLabel(myfile,'Engineers','Bob');
label = findLabel(myfile,'Engineers','Bob');
label.Data = 'Email: Bob.Smith@company.com'
label =
Label with properties:
File: "C:\workSpace\examples\TimesTableApp\source\timestable.mlapp"
DataType: 'char'
Data: 'Email: Bob.Smith@company.com'
Name: "Bob"
CategoryName: "Engineers"
mydata = label.Data
mydata =
'Email: Bob.Smith@company.com'
Create a new label category with data type double, the type MATLAB commonly uses for
numeric data.
32-70
See Also
createCategory(mainProject,'Assessors','double');
category = findCategory(mainProject,'Assessors');
createLabel(category,'Sam');
Attach the new label to a specified file and assign data value 2 to the label.
myfile = mainProject.Files(10);
addLabel(myfile, 'Assessors', 'Sam', 2)
ans =
Label with properties:
File: "C:\workSpace\examples\TimesTableApp\utilities"
DataType: 'double'
Data: 2
Name: "Sam"
CategoryName: "Assessors"
Close Project
Close the project to run shutdown scripts and check for unsaved files.
close(mainProject)
See Also
currentProject
More About
• “Create Projects” on page 32-2
• “Manage Project Files” on page 32-12
• “Analyze Project Dependencies” on page 32-38
32-71
32 Projects
Using the Times Table App example, we will explore how to:
1 Set up and browse some example project files under source control.
2 Examine project shortcuts to access frequently used files and tasks.
3 Analyze dependencies in the project and locate required files that are not yet in the
project.
4 Modify some project files, find and review modified files, compare them to an earlier
version, and commit modified files to source control.
5 Explore views of project files only, modified files, and all files under the project root
folder.
Create a working copy of the Times Table App example project files and open the project.
MATLAB® copies the files to an examples folder so that you can edit them. The project
puts the files under Git™ source control.
matlab.project.example.timesTable
You can view, search, and sort project files by using the Files view.
To view the files in the project, in the Files view, click Project (number of files).
When the view is selected, only the files in your project are shown.
To see all the files in your project folder, click All. This view shows all the files that are
under the project root, not just the files that are in the project. As a result, this view is
useful for adding files to the project.
To view files as a list instead of a tree, in the Layout field at the top right of the Files
view, select List.
32-72
Explore an Example Project
• To search for particular files or file types by name, in any file view, type in the search
box or click the Filter button. For example, in the search field, enter the text
timestable. The project returns all files and folders that contain the word
timestable. Click the to clear the search.
• To search the content of files, go to the Project tab and click the Search button. Enter
a value in the search field and click Enter. For example, enter the word tests. The
project displays all files and folders that contain the word tests. Click the to clear
the search.
• To change how files are grouped or sorted, and to customize the columns, click the
actions button and select from the available options.
You can use shortcuts to make files easier to find in a large project. View and run
shortcuts on the Project Shortcuts tab. You can organize the shortcuts into groups.
The Times Table App project contains several shortcuts, including a shortcut to open the
project requirements, and another to run all the tests in the project. The shortcuts make
these tasks easier for users of the project.
To perform an action, on the Project Shortcuts tab, click the associated shortcut. For
example, to open project requirements, click Documentation > Requirements. To run
tests, click Test > Run All Tests.
To create a new shortcut, select the Files view, right-click a file, and select Create
Shortcut.
Create a new folder and add it to the project path. Adding a project folder to the project
path ensures that all users of the project can access the files within it.
32-73
32 Projects
1 Select the Files view. View folders using the tree layout, and then expand the
utilities folder.
2 Right-click source/timesTableGame.m and select Open.
3 Make a change in the Editor, such as adding a comment, and save the file.
4 In the Files view, select the Modified (number of files) tab. After editing the
file, you see Modified (2). The file you changed appears in the list.
5 To review changes, right-click source/timesTableGame.m in the Modified files
view and select Compare > Compare to Ancestor. The MATLAB Comparison Tool
opens a report comparing the modified version of the file in your sandbox to its
ancestor stored in version control. The comparison report type can differ depending
on the file you select. If you select a Simulink® model to compare, this command
runs a Simulink model comparison.
* Note - When you open the Times Table App example project, the project shows a
modified file in the resources folder. This is a side effect of opening the example project.
When editing files in your own projects, only changes that affect file metadata, such as
adding a label to a file, create modified files in the resources folder.
Analyze Dependencies
To check that all required files are in the project, run a file dependency analysis on the
modified files.
Now, remove one of the required files. Select the Files view, right click on the source/
timesTableGame.m file, and select Remove from Project. Click Remove in the
Remove from Project dialog box.
32-74
Explore an Example Project
3 Hover over the messages under Problems and click Find All to view the files that
use a problem file. The graph updates to display only the problem file,
timesTableGame.m, and the problem message, Not in project, in the right
panel.
4 To view the dependencies of the problem file, on the Dependency Analysis tab, in
the Impact Analysis section, select Find > All Dependencies of Selection.
Now that you have seen the problem, fix it by returning the missing file to the project.
Right-click the file and select Add to Project. The next time you run a dependency
analysis, the file does not appear as a problem file.
1 On the Dependency Analysis tab, in the Impact Analysis section, select Find > All
Files.
2 In the Impact Analysis section, choose Select > Modified Files.
3 To view the dependencies of the modified files, in the Impact Analysis section, select
Find > All Dependencies of Selection.
To make sure that your changes are ready to commit, check your project. On the Project
tab, click Run Checks to run the project integrity checks. The checks look for missing
files, files to add to source control or retrieve from source control, and other issues. The
Checks dialog box offers automatic fixes to problems found, when possible. When you
click a Details button in the Checks dialog box, you can view recommended actions and
decide whether to make the changes.
After you modify files and you are satisfied with the results of the checks, you can commit
your changes to the source control repository.
1 In the Files view, select the Modified (number of files) tab. The files you
changed appear in the list.
2 To commit your changes to source control, on the Project tab, in the Source
Control section, click Commit.
3 Enter a comment for your submission, and click Submit. Watch the messages in the
status bar as the source control commits your changes. In Git, you can have both
32-75
32 Projects
local and remote repositories. These instructions commit to the local repository. To
commit to the remote repository, in the Source Control section, click Pull.
To view and edit project details, on the Project tab, in the Environment section, click
Details. View and edit project details such as the name, description, project root, startup
folder, and location of folders containing generated files.
To view details about the source control integration and repository location, on the
Project tab, in the Source Control section, click Git Details. The Times Table App
example project uses Git source control.
Click the at the top right corner of the project window to close the project.
proj = currentProject;
close(proj);
See Also
More About
• “Create Projects” on page 32-2
• “Manage Project Files” on page 32-12
• “Analyze Project Dependencies” on page 32-38
32-76
33
The source control interface provides access to your source control system from the
MATLAB desktop.
33-2
About MathWorks Source Control Integration
• Subversion (SVN)
• Git
• Retrieve files from an existing repository. See “Check Out from SVN Repository” on
page 33-25 or “Clone from Git Repository” on page 33-36.
• Add source control to a folder. See “Create New Repository” on page 33-7.
• Add new files in a folder already under source control. See “Mark Files for Addition to
Source Control” on page 33-11.
33-3
33 Source Control Interface
• Locking and user permissions on a per-file basis (e.g., you can enforce locking of
model files)
• Central server, reducing local storage needs
• Simple and easy to learn
This diagram represents the distributed source control workflow (for example, using Git).
• Offline working
• Local repository, which provides full history
• Branching
• Multiple remote repositories, enabling large-scale hierarchical access control
33-4
About MathWorks Source Control Integration
• You need to work offline, commit regularly, and need access to the full repository
history.
• You need to branch locally.
33-5
33 Source Control Interface
33-6
Create New Repository
Note Before using source control, you must register binary files with your source control
tools to avoid corruption. For more information, see “Register Binary Files with Git” on
page 33-32.
1 Right-click in the white space (any blank area) of the MATLAB Current Folder
browser and select Source Control > Manage Files. MATLAB opens the Manage
Files Using Source Control dialog box.
2 In the Source control integration list, select Git.
3 Click the Change button. MATLAB opens the Select a Repository dialog box.
4
Click the Create a Git repository on disk ( ) button.
5 Select an empty folder or create a new folder in which you want to create the
repository and then click Select Folder. MATLAB creates the repository, closes the
dialog box and returns to the Select a Repository dialog box.
6 Click Validate to check the path to the new repository, and then click OK. MATLAB
closes the dialog box and returns to the Manage Files Using Source Control dialog
box.
7 In the Sandbox field, specify the location for your sandbox. The selected folder must
be empty.
8 Click Retrieve to create the sandbox.
After creating the Git repository and sandbox, add your files to the sandbox. Then,
commit the first version of your files to the new repository. For more information, see
“Mark Files for Addition to Source Control” on page 33-11.
33-7
33 Source Control Interface
You also can change the repository URL after the repository is created. In the Current
Folder browser, in a folder under source control, right-click, and select Source Control >
Remote and specify a new URL.
To merge branches in Git, you need some additional setup steps. For more information,
see “Install Command-Line Git Client” on page 33-31.
To use a Git server for the repository on your local system, you can set up your own
Apache™ Git server or use a Git server hosting solution. If you cannot set up a server and
must use a remote repository via the file system using the file:/// protocol, make sure
that it is a bare repository with no checked out working copy.
Note
• Before using source control, you must register binary files with your source control
tools to avoid corruption. See “Register Binary Files with SVN” on page 33-18.
• Check that the folder you want to use for your SVN sandbox is on a local hard disk.
Using a network folder with SVN is slow and unreliable.
1 Right-click in the white space (any blank area) of the MATLAB Current Folder
browser and select Source Control > Manage Files. MATLAB opens the Manage
Files Using Source Control dialog box.
2 In the Source control integration list, select SVN.
3 Click the Change button. MATLAB opens the Specify SVN Repository URL dialog
box.
4
Click the Create an SVN repository in a folder ( ) button.
5 Select an empty folder or create a new folder in which you want to create the
repository and then click Select Folder.
33-8
See Also
MATLAB creates the repository in the selected folder, closes the dialog box and
returns to the Specify SVN Repository URL dialog box. The URL of the new
repository is in the Repository box, and the project automatically selects the trunk
folder. Specify file:// URLs and create new repositories for single users only. For
multiple users, see “Share a Subversion Repository” on page 33-23.
6 Click Validate to check the path to the new repository, and then click OK. MATLAB
closes the dialog box and returns to the Manage Files Using Source Control dialog
box.
If your SVN repository has a file URL, a warning appears that file URLs are for single
users. Click OK to continue.
7 In the Sandbox field, specify the location for your sandbox. For example, select the
folder that contains the files that you want to add to source control
8 Click Retrieve to create the sandbox.
After creating the SVN repository and sandbox, you can commit the first version of your
files to the new repository. For more information, see “Mark Files for Addition to Source
Control” on page 33-11.
See Also
Related Examples
• “Set Up Git Source Control” on page 33-30
• “Set Up SVN Source Control” on page 33-18
• “Clone from Git Repository” on page 33-36
• “Check Out from SVN Repository” on page 33-25
• “Commit Modified Files to Source Control” on page 33-16
33-9
33 Source Control Interface
• Show Revisions to open the File Revisions dialog box and browse the history of a file.
You can view information about who previously committed the file, when they
committed it, the log messages, and the list of files in each change set. You can select
multiple files and view revision history for each file.
With SVN, you can select a revision and browse the lower list of files in the change set.
Right-click files to view changes or save revisions.
• Compare to Revision to open a dialog box where you can select the revisions you
want to compare and view a comparison report. You can either:
With SVN, you can select a revision and browse the lower list of files in the change set.
Right-click files to view changes or save revisions.
• Compare to Ancestor to run a comparison with the last checked-out version in the
sandbox (SVN) or against the local repository (Git). The Comparison Tool displays a
report.
If you need to update the status of the modified files, see “Update SVN File Status and
Revision” on page 33-28 or “Update Git File Status and Revision” on page 33-38.
See Also
Related Examples
• “Resolve Source Control Conflicts” on page 33-12
• “Commit Modified Files to Source Control” on page 33-16
• “Revert Changes in Source Control” on page 33-17
33-10
Mark Files for Addition to Source Control
When the file is marked for addition to source control, the symbol changes to Added .
33-11
33 Source Control Interface
For details on using the Comparison Tool to merge changes, see “Merge Text Files” on
page 33-13.
After you are satisfied with the file that is marked conflicted, you can mark the conflict
resolved and commit the file.
Resolve Conflicts
1 Look for conflicted files in the Current Folder browser.
2 Check the source control status column (SVN or Git) for files with a red warning
symbol , which indicates a conflict.
3 Right-click the conflicted file and select Source Control > View Conflicts to
compare versions.
4 Examine the conflict. A comparison report opens that shows the differences between
the conflicted files.
With SVN, the comparison shows the differences between the file and the version of
the file in conflict.
With Git, the comparison shows the differences between the file on your branch and
the branch you want to merge into.
5 Use the Comparison Tool report to determine how to resolve the conflict.
33-12
Resolve Source Control Conflicts
You can use the Comparison Tool to merge changes between revisions, as described
in “Merge Text Files” on page 33-13.
6 When you have resolved the changes and want to commit the version in your
sandbox, in the Current Folder browser, right-click the file and select Source
Control > Mark Conflict Resolved.
With Git, the Branch status in the Source Control Details dialog box changes from
MERGING to SAFE.
7 Commit the modified files.
<<<<<<< .mine
then extract the conflict markers before merging, as described in “Extract Conflict
Markers” on page 33-14.
Tip When comparing a file to another version in source control, by default the right file is
the version in your sandbox and the left file is either a temporary copy of the previous
version or another version causing a conflict (e.g., filename_theirs). You can swap the
position of the files, so be sure to observe the file paths of the left and right file at the top
of the comparison report. Merge differences from the temporary copy to the version in
your sandbox to resolve conflicts.
1 In the Comparison Tool report, select a difference in the report and click Merge. The
selected difference is copied from the left file to the right file.
Merged differences display gray row highlighting and a green merge arrow.
The merged file name at the top of the report displays with an asterisk
(filename.m*) to show you that the file contains unsaved changes.
33-13
33 Source Control Interface
2 Click Save Merged File to save the file in your sandbox. To resolve conflicts, save
the merged file over the conflicted file.
3 If you want to inspect the files in the editor, click the line number links in the report.
Note If you make any further changes in the editor, the comparison report does not
update to reflect changes and report links can become incorrect.
4 When you have resolved the changes mark them as conflict resolved. Right-click the
file in the Current Folder browser and select Source Control > Mark Conflict
Resolved.
Source control tools can insert conflict markers in files that you have not registered as
binary (e.g., text files). You can use MATLAB to extract the conflict markers and compare
the files causing the conflict. This process helps you to decide how to resolve the conflict.
Caution Register files with source control tools to prevent them from inserting conflict
markers and corrupting files. See “Register Binary Files with SVN” on page 33-18 or
“Register Binary Files with Git” on page 33-32. If your files already contains conflict
markers, the MATLAB tools can help you to resolve the conflict.
If you try to open a file containing conflict markers, the Conflict Markers Found dialog
box opens. Follow the prompts to fix the file by extracting the conflict markers. After you
extract the conflict markers, resolve the conflicts as described in “Examining and
Resolving Conflicts” on page 33-12.
33-14
Resolve Source Control Conflicts
To view the conflict markers, in the Conflict Markers Found dialog box, click Load File.
Do not try to load files, because MATLAB does not recognize conflict markers. Instead,
click Fix File to extract the conflict markers.
When you open a conflicted file or select View Conflicts, MATLAB checks files for conflict
markers and offers to extract the conflict markers. MATLAB checks only conflicted files
for conflict markers.
However, some files that are not marked as conflicted can still contain conflict markers.
This can happen if you or another user marked a conflict resolved without removing the
conflict markers and then committed the file. If you see conflict markers in a file that is
not marked conflicted, you can extract the conflict markers.
1 In the Current Folder browser, right-click the file, and select Source Control >
Extract Conflict Markers to File.
2 In the Extract Conflict Markers to File dialog box, leave the default option to copy
“mine” file version over the conflicted file. Leave the Compare extracted files check
box selected. Click Extract.
3 Use the Comparison Tool report as usual to continue to resolve the conflict.
33-15
33 Source Control Interface
1 Right-click in the Current Folder browser and select Source Control > View and
Commit Changes. In the View and Commit Changes dialog box, select the files to
commit to the repository.
2 Enter comments in the dialog box, and click Commit.
3 A message appears if you cannot commit because the repository has moved ahead.
Before you can commit the file, you must update the revision up to the current HEAD
revision.
• If you are using SVN source control, right-click in the Current Folder browser.
Select Source Control > Update All from SVN.
• If you are using Git source control, right-click in the Current Folder browser.
Select Source Control > Pull.
See Also
Related Examples
• “Mark Files for Addition to Source Control” on page 33-11
• “Review Changes in Source Control” on page 33-10
• “Resolve Source Control Conflicts” on page 33-12
• “Update SVN File Status and Revision” on page 33-28
• “Update Git File Status and Revision” on page 33-38
• “Pull, Push and Fetch Files with Git” on page 33-45
33-16
Revert Changes in Source Control
With Git, right-click a file and select Source Control > Revert Local Changes. Git does
not have locks. To remove all local changes, right-click a blank space in the Current
Folder browser and select Source Control > Branches. In the Branches dialog box,
click Revert to Head.
If you revert a file to an earlier revision and then make changes, you cannot commit the
file until you resolve the conflict with the repository history.
See Also
Related Examples
• “Resolve Source Control Conflicts” on page 33-12
33-17
33 Source Control Interface
Caution Before using source control, you must register binary files with the source
control tools to avoid corruption. See “Register Binary Files with SVN” on page 33-18.
If you need to use a version of SVN other than the built-in version, you can create a
repository using the Command-Line SVN Integration (compatibility mode)
Source control integration option, but you must also install a command-line SVN client.
Command-line SVN integration communicates with any Subversion (SVN) client that
supports the command-line interface. With Command-Line SVN Integration
(compatibility mode), if you try to rename a file or folder to a name that contains an
@ character, an error occurs because command-line SVN treats all characters after the @
symbol as a peg revision value.
33-18
Set Up SVN Source Control
Also check that other file extensions are registered as binary to avoid corruption at check-
in. Check and register files such as MEX-files, .xlsx, .jpg, .pdf, .docx, etc.
You must register binary files if you use any version of SVN, including the built-in SVN
integration provided by MATLAB. If you do not register your extensions as binary, SVN
might add annotations to conflicted MATLAB files and attempt automerge. To avoid this
problem when using SVN, register file extensions.
1 Locate your SVN config file. Look for the file in these locations:
• C:\Users\myusername\AppData\Roaming\Subversion\config or
C:\Documents and Settings\myusername\Application Data
\Subversion\config on Windows
• ~/.subversion on Linux or macOS
2 If you do not find a config file, create a new one. See “Create SVN Config File” on
page 33-19.
3 If you find an existing config file, you have previously installed SVN. Edit the
config file. See “Update Existing SVN Config File” on page 33-20.
1 If you do not find an SVN config file, create a text file containing these lines:
[miscellany]
enable-auto-props = yes
[auto-props]
*.mlx = svn:mime-type=application/octet-stream
*.mat = svn:mime-type=application/octet-stream
*.fig = svn:mime-type=application/octet-stream
*.mdl = svn:mime-type=application/octet-stream
*.slx = svn:mime-type= application/octet-stream
*.mlapp = svn:mime-type= application/octet-stream
*.p = svn:mime-type=application/octet-stream
*.mdlp = svn:mime-type=application/octet-stream
*.slxp = svn:mime-type=application/octet-stream
*.sldd = svn:mime-type=application/octet-stream
*.slxc = svn:mime-type=application/octet-stream
*.mlproj = svn:mime-type=application/octet-stream
*.mldatx = svn:mime-type=application/octet-stream
*.slreqx = svn:mime-type=application/octet-stream
*.sfx = svn:mime-type=application/octet-stream
*.sltx = svn:mime-type=application/octet-stream
33-19
33 Source Control Interface
2 Check for other file types you use that you also need to register as binary to avoid
corruption at check-in. Check for files such as MEX-files
(.mexa64, .mexmaci64, .mexw64), .xlsx, .jpg, .pdf, .docx, etc. Add a line to
the config file for each file type you need. Examples:
*.mexa64 = svn:mime-type=application/octet-stream
*.mexw64 = svn:mime-type=application/octet-stream
*.mexmaci64 = svn:mime-type=application/octet-stream
*.xlsx = svn:mime-type=application/octet-stream
*.docx = svn:mime-type=application/octet-stream
*.pdf = svn:mime-type=application/octet-stream
*.jpg = svn:mime-type=application/octet-stream
*.png = svn:mime-type=application/octet-stream
3 Name the file config and save it in the appropriate location:
• C:\Users\myusername\AppData\Roaming\Subversion\config or
C:\Documents and Settings\myusername\Application Data
\Subversion\config on Windows
• ~/.subversion on Linux or macOS.
After you create the SVN config file, SVN treats new files with these extensions as
binary. If you already have binary files in repositories, see “Register Files Already in
Repositories” on page 33-22.
If you find an existing config file, you have previously installed SVN. Edit the config
file to register files as binary.
enable-auto-props = yes
Ensure that this line is not commented (that is, that it does not start with #). Config
files can contain example lines that are commented out. If there is a # character at
the beginning of the line, delete it.
3 Locate the [auto-props] section. Ensure that [auto-props] is not commented. If
there is a # character at the beginning, delete it.
4 Add the following lines at the end of the [auto-props] section:
33-20
Set Up SVN Source Control
*.mlx = svn:mime-type=application/octet-stream
*.mat = svn:mime-type=application/octet-stream
*.fig = svn:mime-type=application/octet-stream
*.mdl = svn:mime-type=application/octet-stream
*.slx = svn:mime-type= application/octet-stream
*.mlapp = svn:mime-type= application/octet-stream
*.p = svn:mime-type=application/octet-stream
*.mdlp = svn:mime-type=application/octet-stream
*.slxp = svn:mime-type=application/octet-stream
*.sldd = svn:mime-type=application/octet-stream
*.slxc = svn:mime-type=application/octet-stream
*.mlproj = svn:mime-type=application/octet-stream
*.mldatx = svn:mime-type=application/octet-stream
*.slreqx = svn:mime-type=application/octet-stream
*.sfx = svn:mime-type=application/octet-stream
*.sltx = svn:mime-type=application/octet-stream
These lines prevent SVN from adding annotations to MATLAB and Simulink files on
conflict and from automerging.
5 Check for other file types you use that you also need to register as binary to avoid
corruption at check-in. Check for files such as MEX-files
(.mexa64, .mexmaci64, .mexw64), .xlsx, .jpg, .pdf, .docx, etc. Add a line to
the config file for each file type you use. Examples:
*.mexa64 = svn:mime-type=application/octet-stream
*.mexw64 = svn:mime-type=application/octet-stream
*.mexmaci64 = svn:mime-type=application/octet-stream
*.xlsx = svn:mime-type=application/octet-stream
*.docx = svn:mime-type=application/octet-stream
*.pdf = svn:mime-type=application/octet-stream
*.jpg = svn:mime-type=application/octet-stream
*.png = svn:mime-type=application/octet-stream
6 Save the config file.
After you create or update the SVN config file, SVN treats new files as binary. If you
already have files in repositories, register them as described in “Register Files Already in
Repositories” on page 33-22.
33-21
33 Source Control Interface
Caution Changing your SVN config file does not affect files already committed to an
SVN repository. If a file is not registered as binary, use svn propset to manually
register the files as binary.
To manually register a file in a repository as binary, use the following command with
command-line SVN:
If you use MATLAB to create an SVN repository, it creates the standard repository
structure. To enable tagging, the repository must have the standard trunk/ and tags/
folders. After you create a repository with this structure, you can click Tag in the Source
Control context menu to add tags to all of your files. For more information, see “Tag
Versions of Files” on page 33-22.
1 Right-click in the Current Folder browser, and select Source Control > Tag.
2 Specify the tag text and click Submit. The tag is added to every file in the folder.
Errors appear if you do not have a tags folder in your repository.
Note You can retrieve a tagged version of your files from source control, but you cannot
tag them again with a new tag. You must check out from trunk to create new tags.
33-22
Set Up SVN Source Control
To enforce locking files, modify entries in the SVN config file. To locate your SVN
config file, see “Register Binary Files with SVN” on page 33-18.
1 To make files with a .m extension read only, add a property to your SVN config file
in the [auto-props] section. If there is no entry for files with a .m extension, add
one with the needs-lock property.
*.m = svn:needs-lock=yes
If an entry exists, you can combine properties in any order, but multiple entries must
be on a single line separated by semicolons.
2 To make files with a .mlx extension read only, add a property to your SVN config
file in the [auto-props] section. Since you must register files with a .mlx
extension as binary, there is an entry for the file type. Add the needs-lock property
to the entry in any order, but on the same line and separated by a semicolon.
*.mlx = svn:mime-type=application/octet-stream;svn:needs-lock=yes
3 Re-create the sandbox for the configuration to take effect.
With this setting, you need to select Get File Lock before you can edit files with a .m
extension. See “Get SVN File Locks” on page 33-29.
Caution Do not allow multiple users to access a repository directly via file:// URLs or
you risk corrupting the repository. Use file:// URLs only for single-user repositories.
33-23
33 Source Control Interface
Be aware of this caution if you use MATLAB to create a repository. MATLAB uses the
file:// protocol. Creating new repositories is provided for local, single-user access only,
for testing and debugging. Accessing a repository via file:// URLs is slower than using
a server.
When you want to share a repository, you need to set up a server. You can use svnserve
or the Apache SVN module. See the Web page references:
http://svnbook.red-bean.com/en/1.7/svn-book.html#svn.serverconfig.svnserve
http://svnbook.red-bean.com/en/1.7/svn-book.html#svn.serverconfig.httpd
See Also
Related Examples
• “Check Out from SVN Repository” on page 33-25
33-24
Check Out from SVN Repository
1 Right-click in the white space (any blank area) in the Current Folder browser and
select Source Control > Manage Files.
2 In the Manage Files Using Source Control dialog box, select the source control
interface from the Source control integration list. To use SVN, leave the default
SVN.
3 If you know your repository location, paste it into the Repository path field.
Otherwise, to browse for and validate the repository path to retrieve files from, click
Change.
a In the Specify SVN Repository URL dialog box, specify the repository URL by
entering a URL in the box, using the list of recent repositories, or by using the
Repository button .
Caution Use file:// URLs only for single-user repositories. For more
information, see “Share a Subversion Repository” on page 33-23.
b Click Validate to check the repository path.
If you see an authentication dialog box for your repository, enter the login
information to continue.
c If the path is invalid, check the URL against your source control repository
browser.
If necessary, select a deeper folder in the repository tree. You might want to
check out from trunk or from a branch folder under tags, if your repository
contains tagged versions of files. You can check out from a branch, but the built-
in SVN integration does not support branch merging. Use an external tool such
as TortoiseSVN to perform branch merging.
d When you have finished specifying the URL path you want to retrieve, click OK.
The dialog box closes and you return to the Manage Files Using Source Control
dialog box.
4 In the Manage Files Using Source Control dialog box, select the sandbox folder to
store the retrieved files and click Retrieve.
33-25
33 Source Control Interface
If you see an authentication dialog box for your repository, enter the login
information to continue.
Caution Use local sandbox folders. Using a network folder with SVN slows source
control operations.
The Manage Files Using Source Control dialog box displays messages as it retrieves
the files from source control.
Note To update an existing sandbox from source control, see “Update SVN File Status
and Revision” on page 33-28.
1 Right-click in the white space in the Current Folder browser, and select Source
Control > Manage Files.
2 In the Manage Files Using Source Control dialog box, select the source control
interface from the Source control integration list. To use SVN, leave the default
SVN.
3 Click Change to select the repository path that you want to retrieve files from.
4 In the Specify SVN Repository URL dialog box:
a Select a recent repository from the Repository list, or click the Repository
33-26
See Also
6 Click Retrieve.
See Also
Related Examples
• “Set Up SVN Source Control” on page 33-18
• “Update SVN File Status and Revision” on page 33-28
33-27
33 Source Control Interface
In this section...
“Refresh Status of Files” on page 33-28
“Update Revisions of Files” on page 33-28
To refresh the status of all files in a folder, right-click the white space of the Current
Folder browser and select Source Control > Refresh SVN status.
Note For SVN, refreshing the source control status does not contact the repository. To
get the latest revisions, see “Update Revisions of Files” on page 33-28.
To update all files in a folder, right-click the Current Folder browser and select Source
Control > Update All from SVN.
See Also
Related Examples
• “Check Out from SVN Repository” on page 33-25
• “Review Changes in Source Control” on page 33-10
33-28
Get SVN File Locks
In the Current Folder browser, select the files you want to check out. Right-click the
selected files and select Source Control > Get File Lock. A lock symbol appears in
the source control status column. Other users cannot see the lock symbol in their
sandboxes, but they cannot get a file lock or check in a change when you have the lock. To
view or break locks, right-click in the Current Folder browser and select Source Control
> Locks.
If you see an SVN message reporting a working copy locked error, remove stale
locks. In the Current Folder browser, right-click and select Source Control > SVN
Cleanup. SVN uses working copy locks internally and they are not the file locks you
control using Source Control > Get File Lock.
See Also
Related Examples
• “Enforce Locking Files Before Editing” on page 33-23
33-29
33 Source Control Interface
• Branch management
• Local full revision history
• Local access that is quicker than remote access
• Offline working
• Tracking of file names and contents separately
• Enforcing of change logs for tracing accountability
• Integration of batches of changes when ready
These capabilities do not suit every situation. If your project is not appropriate for offline
working or your repository is too large for a full local revision history, for example, Git is
not the ideal source control. In addition, if you need to enforce locking of files before
editing, Git does not have this ability. In that situation, SVN is the better choice.
33-30
Set Up Git Source Control
Some clients are not available systemwide, including the mingw32 environment provided
by GitHub (Git Shell on the Start menu). Installing command-line Git makes it available
systemwide, and then MATLAB can locate standard ssh keys.
33-31
33 Source Control Interface
Check if Git is available by using the command !git in MATLAB. If Git is not available,
install it. After you have installed a command-line Git client and registered your files as
binary, you can use the merging features of Git in MATLAB.
On Windows:
1 Download the Git installer and run it. You can find command-line Git at:
https://msysgit.github.io/
2 In the section on adjusting your PATH, choose the install option to Use Git from the
Windows Command Prompt. This option adds Git to your PATH variable, so that
MATLAB can communicate with Git.
3 In the section on configuring the line-ending conversions, choose the option
Checkout as-is, commit as-is to avoid converting any line endings in files.
4 To avoid corrupting binary files, before using Git to merge branches, register the
binary files.
On Linux, Git is available for most distributions. Install Git for your distribution. For
example, on Debian®, install Git by entering:
On Mac, on Mavericks (10.9) or above, try to run git from the Terminal. If you do not
have Git installed already, it will prompt you to install Xcode Command Line Tools. For
more options, see https://git-scm.com/doc.
Also check that other file extensions are registered as binary to avoid corruption at check-
in. Check and register files such as MEX-files, .xlsx, .jpg, .pdf, .docx, etc.
33-32
Set Up Git Source Control
After you install a command-line Git client, you can prevent Git from corrupting your files
by inserting conflict markers. To do so, edit your .gitattributes file to register binary
files. For details, see:
https://git-scm.com/docs/gitattributes
1 If you do not already have a .gitattributes file in your sandbox folder, create one
at the MATLAB command prompt:
edit .gitattributes
2 Add these lines to the .gitattributes file:
*.mlx -crlf -diff -merge
*.mat -crlf -diff -merge
*.fig -crlf -diff -merge
*.mdl -crlf -diff -merge
*.slx -crlf -diff -merge
*.mlapp -crlf -diff -merge
*.p -crlf -diff -merge
*.mdlp -crlf -diff -merge
*.slxp -crlf -diff -merge
*.sldd -crlf -diff -merge
*.slxc -crlf -diff -merge
*.mlproj -crlf -diff -merge
*.mldatx -crlf -diff -merge
*.slreqx -crlf -diff -merge
*.sfx -crlf -diff -merge
*.sltx -crlf -diff -merge
These lines specify not to try automatic line feed, diff, and merge attempts for these
types of files.
3 Check for other file types you use that you also need to register as binary to avoid
corruption at check-in. Check for files such as MEX-files
(.mexa64, .mexmaci64, .mexw64), .xlsx, .jpg, .pdf, .docx, etc. Add a line to
the attributes file for each file type you need.
Examples:
*.mexa64 -crlf -diff -merge
*.mexw64 -crlf -diff -merge
*.mexmaci64 -crlf -diff -merge
*.xlsx -crlf -diff -merge
*.docx -crlf -diff -merge
*.pdf -crlf -diff -merge
33-33
33 Source Control Interface
Tip You can reduce your Git repository size by saving Simulink models without
compression. Turning off compression results in larger SLX files on disk but reduces
repository size.
To use this setting with new SLX files, create your models using a model template with
SLX Compression set to none. For existing SLX files, set compression and then save the
model. For more information, see “Set SLX Compression Level” (Simulink).
1 Right-click in the MATLAB Current Folder browser, and select Source Control >
Submodules.
2 In the Submodules dialog box, click the + button.
3 In the Add Submodule dialog box, in the Remote box, specify a repository location.
Optionally, click Validate.
4 In the Path box, specify a location for the submodule and click OK. The Submodules
dialog box displays the status and details of the submodule.
5 Check the status message, and click Close.
Update Submodules
After using Pull to get the latest changes from a remote repository, check that
submodules are up to date by clicking Submodules and then click Update. If any
submodule definition have changed, then the update ensures that the submodule folder
contains the correct files. Update applies to all child submodules in the submodule
hierarchy.
When you want to manage the added submodule, open the Submodules dialog box.
33-34
See Also
1 To get the latest version of a submodule, in the Submodules dialog box, click Fetch.
2 After fetching, you must merge. Check the Status message in the Submodules dialog
box for information about your current branch relative to the remote tracking branch
in the repository. When you see the message Behind, you need to merge in changes
from the repository to your local branch.
3 Click Branches and merge in the origin changes to your local branch using the
Branches dialog box. See “Fetch and Merge” on page 33-46.
If you make changes in your submodule and want to send changes back to the repository:
If you want other users to obtain your changes in the submodule when they clone the
parent folder, make sure the index and head match.
1 In the Submodules dialog box, check the index and head values. The index points to
the head commit at the time you first cloned the submodule, or when you last
committed the parent folder. If the index and head do not match, you must update the
index.
2 To update the index, commit your changes in the parent folder, and then click Push
in the Submodules dialog box. This action makes the index and head the same.
See Also
Related Examples
• “Clone from Git Repository” on page 33-36
33-35
33 Source Control Interface
1 Right-click in the white space (any blank area) in the Current Folder browser, and
select Source Control > Manage Files.
2 In the Manage Files Using Source Control dialog box, select Git from the Source
control integration list.
3 If you know your repository location, paste it into the Repository path field.
Otherwise, to browse for and validate the repository path to retrieve files from, click
Change.
a In the Select a Repository dialog box, browse to your repository using the
Remote button .
b Click Validate to check the repository path.
If you see an authentication dialog box for your repository, enter the login
information to continue.
c If the path is valid, click OK. The dialog box closes and you return to the Manage
Files Using Source Control dialog box.
4 In the Manage Files Using Source Control dialog box, select the sandbox folder to
store the retrieved files and click Retrieve.
If you see an authentication dialog box for your repository, enter the login
information to continue.
Troubleshooting
If you encounter errors like OutOfMemoryError: Java heap space, for example
when cloning big Git repositories, then edit your MATLAB preferences to increase the
memory size.
33-36
See Also
See Also
Related Examples
• “Set Up Git Source Control” on page 33-30
• “Update Git File Status and Revision” on page 33-38
• “Branch and Merge with Git” on page 33-40
33-37
33 Source Control Interface
To refresh the status of all files in the repository, right-click the white space of the
Current Folder browser and select Source Control > Refresh Git status.
Caution Ensure you have registered binary files with Git before using Pull. If you do not,
conflict markers can corrupt your files. For more information, see “Register Binary Files
with Git” on page 33-32.
Pull fetches the latest changes and merges them into your current branch. If you are not
sure what is going to come in from the repository, use fetch to examine the changes first
and then merge the changes manually. For more information, see “Pull, Push and Fetch
Files with Git” on page 33-45.
Pull might fail if you have conflicts. With a complicated change you might want to create a
branch from the origin, make some compatibility changes, then merge that branch into
the main tracking branch.
33-38
See Also
See Also
Related Examples
• “Clone from Git Repository” on page 33-36
• “Review Changes in Source Control” on page 33-10
33-39
33 Source Control Interface
Create Branch
1 From within your Git repository folder, right-click the white space of the Current
Folder browser and select Source Control > Branches. In the Branches dialog box,
you can view, switch, create, and merge branches.
Tip You can inspect information about each commit node. Select a node in the
Branch Browser diagram to view the author, date, commit message, and changed
files.
33-40
Branch and Merge with Git
2 Select a source for the new branch. Click a node in the Branch Browser diagram, or
enter a unique identifier in the Source text box. You can enter a tag, branch name, or
a unique prefix of the SHA1 hash (for example, 73c637 to identify a specific commit).
Leave the default to create a branch from the head of the current branch.
3 Enter a name in the Branch name text box and click Create.
4 To work on the files on your new branch, switch your project to the branch.
In the Branches drop-down list, select the branch you want to switch to and click
Switch.
33-41
33 Source Control Interface
5 Close the Branches dialog box and work on the files on your branch.
For next steps, see “Pull, Push and Fetch Files with Git” on page 33-45.
Switch Branch
1 From within your Git repository folder, right-click the white space of the Current
Folder browser and select Source Control > Branches.
2 In the Branches dialog box, in the Branches drop-down list, select the branch you
want to and click Switch.
3 Close the Branches dialog box and work on the files on your branch.
Compare Branches
From within your Git repository folder, right-click the white space of the Current Folder
browser and select Source Control > Branches.
• To examine differences in a file between the current revision and its parent, right-click
a file in the tree under Differences from parent and select Show Difference.
• To examine differences in a file between any two revisions including revisions on two
different development branches, hold the Ctrl key and select the two different
revisions. Right-click a file in the tree under Difference from selection and
select Show Difference.
MATLAB opens a comparison report. You can save a copy of the selected file on either
revision. Right-click a file and select Save As to save a copy of the file on the selected
revision. Select Save Parent As to save a copy of the file on the prior revision. This is
useful if you want to test how the code ran in previous revisions or on other branches.
Merge Branches
Before you can merge branches, you must install command-line Git on your system path
and register binary files to prevent Git from inserting conflict markers. See “Install
Command-Line Git Client” on page 33-31.
Tip After you fetch changes, you must merge. For more information, see “Fetch and
Merge” on page 33-46.
33-42
Branch and Merge with Git
1 From within your Git repository folder, right-click the white space of the Current
Folder browser and select Source Control and Branches.
2 In the Branches dialog box, from the Branches drop-down list, select a branch you
want to merge into the current branch, and click Merge.
3 Close the Branches dialog box and work on the files on your branch.
If the branch merge causes a conflict that Git cannot resolve automatically, an error
dialog box reports that automatic merge failed. Resolve the conflicts before proceeding.
Caution Do not move or delete files outside of MATLAB because this can cause errors on
merge.
1 To keep your version of the file, right-click the file and select Mark Conflict
Resolved.
2 Click Commit Modified Files to commit your change that marks the conflict
resolved.
If you merge a branch and there is a conflict in a file, Git marks the file as conflicted and
does not modify the contents. Right-click the file and select Source Control > View
Conflicts. A comparison report opens that shows the differences between the file on your
branch and the branch you want to merge into. Decide how to resolve the conflict. See
“Resolve Source Control Conflicts” on page 33-12.
Revert to Head
1 From within your Git repository folder, right-click the white space of the Current
Folder browser and select Source Control > Branches.
2 In the Branches dialog box, click Revert to Head to remove all local changes.
33-43
33 Source Control Interface
Delete Branches
1 In the Branches dialog box under Branch Browser, expand the Branches drop-
down list, and select the branch you want to delete.
2 On the far right, click the down arrow and select Delete Branch.
See Also
Related Examples
• “Set Up Git Source Control” on page 33-30
• “Pull, Push and Fetch Files with Git” on page 33-45
• “Resolve Source Control Conflicts” on page 33-12
33-44
Pull, Push and Fetch Files with Git
Note Before you can merge, you must install command-line Git and register binary files
to prevent Git from inserting conflict markers. See “Install Command-Line Git Client” on
page 33-31.
33-45
33 Source Control Interface
Pull might fail if you have conflicts. With a complicated change you might want to create a
branch from the origin, make some compatibility changes, then merge that branch into
the main tracking branch.
To commit changes to the local repository, right-click the Current Folder browser and
select Source Control > View and Commit Changes.
To see if your local changes have moved ahead of the remote tracking branch, right-click
the file or white space of the Current Folder browser and select Source Control > View
Details. The Git information field indicates whether your committed local changes are
ahead of, behind, or coincident with the remote tracking branch.
To send local commits to the remote repository, right-click in the Current Folder browser
and select Source Control > Push. A message appears if you cannot push your changes
directly because the repository has moved on. Right-click in the Current Folder browser
and select Source Control > Fetch to fetch all changes from the remote repository.
Merge branches and resolve conflicts, and then you can push your changes.
Using Git, you cannot add empty folders to source control, so you cannot select Push and
then clone an empty folder. You can create an empty folder in MATLAB, but if you push
changes and then sync a new sandbox, then the empty folder does not appear in the new
sandbox. To push empty folders to the repository for other users to sync, create a
gitignore file in the folder and then push your changes.
Note After fetching, you must merge. Before you can merge branches, you must install
command-line Git and register binary files to prevent Git from inserting conflict markers.
See “Install Command-Line Git Client” on page 33-31.
To fetch changes from the remote repository, right-click in the Current Folder browser
and select Source Control > Fetch. Fetch updates all of the origin branches in the local
repository. Your sandbox files do not change. To see others’ changes, you need to merge in
the origin changes to your local branches.
For information about your current branch relative to the remote tracking branch in the
repository, right-click the file or white space of the Current Folder browser and select
33-46
Pull, Push and Fetch Files with Git
Source Control > View Details. The Git information field indicates whether your
committed local changes are ahead of, behind, or coincident with the remote tracking
branch. When you see the message Behind, you need to merge in changes from the
repository to your local branch.
For example, if you are on the master branch, get all changes from the master branch in
the remote repository.
1 Right-click in the Current Folder browser and select Source Control > Fetch
2 Right-click in the Current Folder browser and select Source Control > Branches.
3 In the Branches dialog box, select origin/master in the Branches list.
4 Click Merge. The origin branch changes merge into the master branch in your
sandbox.
If you right-click the Current Folder browser and select Source Control > View Details,
the Git information field indicates Coincident with /origin/master. You can now
view the changes that you fetched and merged from the remote repository in your local
sandbox.
To create and manage stashes, in the Current Folder browser, right-click the white space
in a folder managed by Git and select Source Control > Stashes.
• To create a stash containing your currently modified files, click New Stash.
• To view modified files in a stash, select the stash under Available Stashes. Right-click
modified files to view changes or save a copy.
• To apply the stash to your current branch and then delete the stash, click Pop.
• To apply the stash and keep it, click Apply.
• To delete the stash, click Drop.
33-47
33 Source Control Interface
See Also
Related Examples
• “Branch and Merge with Git” on page 33-40
• “Resolve Source Control Conflicts” on page 33-12
33-48
Move, Rename, or Delete Files Under Source Control
To move a file under source control, right-click the file in the Current Folder browser,
select Source Control > Move, and enter a new file location.
To rename a file under source control, right-click the file in the Current Folder browser,
select Source Control > Rename, and enter a new file name.
To delete a file from the repository, mark the file for deletion.
• To mark a file for deletion from the repository and retain a local copy, right-click the
file in the Current Folder browser. Select Source Control and then Delete from SVN
or Delete from Git. When the file is marked for deletion from source control, the
symbol changes to Deleted . The file is removed from the repository at the next
commit.
• To mark a file for deletion from the repository and from your disk, right-click the file in
the Current Folder browser. Select Source Control and then Delete from SVN and
disk or Delete from Git and disk. The file disappears from the Current Folder
browser and is immediately deleted from your disk. The file is removed from the
repository at the next commit.
See Also
Related Examples
• “Mark Files for Addition to Source Control” on page 33-11
• “Commit Modified Files to Source Control” on page 33-16
33-49
33 Source Control Interface
MATLAB Comparison Tool provides useful merge tools for MathWorks files and is
compatible with all popular software configuration management and version control
systems.
Set up your source control tool to use MATLAB as the application for diff and merge for
the file extensions you want, for example, .mlx, .mat, .slx, or .mdl, by following these
steps:
1 To get the required file paths and set the preference to reuse open MATLAB sessions,
run this command in MATLAB:
comparisons.ExternalSCMLink.setup()
This command sets the MATLAB preference, under Comparison, called Allow
external source control tools to use open MATLAB sessions for diffs and
merges.
The command also displays the file paths you will copy and paste into your source
control tool setup:
• On Windows:
matlabroot\bin\win64\mlDiff.exe
matlabroot\bin\win64\mlMerge.exe
• On Linux:
matlabroot/bin/glnxa64/mlDiff
matlabroot/bin/glnxa64/mlMerge
On Mac:
matlabroot/bin/maci64/mlDiff
33-50
Customize External Source Control to Use MATLAB for Diff and Merge
matlabroot/bin/maci64/mlMerge
Where matlabroot is replaced with the full path to your installation, for
example, C:\Program Files\MATLAB\R2016b.
2 Set up diff.
a In the MATLAB Command Window, copy the file path to mlDiff, for example,
C:\Program Files\MATLAB\R2016b\bin\win64\mlDiff.exe.
b In your source control tool, locate the diff setting, and add an entry to specify
what to do with a particular file extension (for example, .slx). Paste in the file
path to mlDiff that you copied from the MATLAB Command Window.
c After the path to the script, or in the arguments box, add arguments to specify
the input files. Look up the argument names specific to your source control tool.
Specify the inputs for diffs, in this order: leftFile, rightFile.
3 Set up merge.
33-51
33 Source Control Interface
Your diff and merge operations use open MATLAB sessions if available, and only open
MATLAB when necessary. The comparison only uses the specified MATLAB
installation.
See Also
Related Examples
• “Compare and Merge Text”
• “Compare and Merge MAT-Files”
• “Compare Variables”
• “Merge Simulink Models from the Comparison Report” (Simulink)
33-52
MSSCCI Source Control Interface
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
If you use source control systems to manage your files, you can interface with the systems
to perform source control actions from within the MATLAB, Simulink, and Stateflow
products. Use menu items in the MATLAB, Simulink, or Stateflow products, or run
functions in the MATLAB Command Window to interface with your source control
systems.
The source control interface on Windows works with any source control system that
conforms to the Microsoft Common Source Control standard, Version 1.1. If your source
control system does not conform to the standard, use a Microsoft Source Code Control
API wrapper product for your source control system so that you can interface with it from
the MATLAB, Simulink, and Stateflow products.
This documentation uses the Microsoft Visual SourceSafe® software as an example. Your
source control system might use different terminology and not support the same options
or might use them in a different way. Regardless, you should be able to perform similar
actions with your source control system based on this documentation.
Perform most source control interface actions from the Current Folder browser. You can
also perform many of these actions for a single file from the MATLAB Editor, a Simulink
model window, or a Stateflow chart window—for more information, see “Access MSSCCI
Source Control from Editors” on page 33-71.
33-53
33 Source Control Interface
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
In this section...
“Create Projects in Source Control System” on page 33-54
“Specify Source Control System with MATLAB Software” on page 33-56
“Register Source Control Project with MATLAB Software” on page 33-57
“Add Files to Source Control” on page 33-59
All files in a folder must belong to the same source control project. Be sure the working
folder for the project in the source control system specifies the correct path to the folder
on disk.
This example uses the project my_thesis_files in Microsoft Visual SourceSafe. This
illustration of the Current Folder browser shows the path to the folder on disk,
D:\my_thesis_files.
33-54
Set Up MSSCCI Source Control
The following illustration shows the example project in the source control system.
33-55
33 Source Control Interface
To set the working folder in Microsoft Visual SourceSafe for this example, select
my_thesis_files, right-click, select Set Working Folder from the context menu, and
specify D:\my_thesis_files in the resulting dialog box.
The currently selected system is shown in the Preferences dialog box. The list includes all
installed source control systems that support the Microsoft Common Source Control
standard.
Select the source control system you want to interface with and click OK.
33-56
Set Up MSSCCI Source Control
MATLAB remembers preferences between sessions, so you only need to perform this
action again when you want to access a different source control system.
If you run a 64-bit version of MATLAB and want MATLAB to interface with your source
control system, your source control system must be 64-bit compliant. If you have a 32-bit
source control system, or if you have a 64-bit source control system running in 32-bit
compatibility mode, MATLAB cannot use it. In that event, MATLAB displays a warning
about the problem in the Source Control preference pane.
1 In the MATLAB Current Folder browser, select a file that is in the folder you want to
associate with a project in your source control system. For example, select
D:\my_thesis_files\wind.m. This will associate all files in the
my_thesis_files folder.
2 Right-click, and from the context menu, select Source Control > Register
Name_of_Source_Control_System Project with MATLAB. The
33-57
33 Source Control Interface
33-58
Set Up MSSCCI Source Control
The selected file, its folder, and all files in the folder, are associated with the source
control system project you selected. For the example, MATLAB associates all files in
D:\my_thesis_files with the source control project my_thesis_files.
1 In the Current Folder browser, select files you want to add to the source control
system.
2 Right-click, and from the context menu, select Source Control > Add to Source
Control.
33-59
33 Source Control Interface
3 The resulting Add to source control dialog box lists files you selected to add. You
can add text in the Comments field. If you expect to use the files soon, select the
Keep checked out check box (which is selected by default). Click OK.
If you try to add an unsaved file, the file is automatically saved upon adding.
33-60
Check Files In and Out from MSSCCI Source Control
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
In this section...
“Check Files Into Source Control” on page 33-61
“Check Files Out of Source Control” on page 33-62
“Undoing the Checkout” on page 33-63
Before checking files into and out of your source control system from the MATLAB
desktop, be sure to set up your system for use with MATLAB as described in “Set Up
MSSCCI Source Control” on page 33-54.
1 In the Current Folder browser, select the files to check in. A file can be open or closed
when you check it in, but it must be saved, that is, it cannot contain unsaved changes.
2 Right-click, and from the context menu, select Source Control > Check In.
3 In the resulting Check in file(s) dialog box, you can add text in the Comments field.
If you want to continue working on the files, select the check box Keep checked out.
Click OK.
33-61
33 Source Control Interface
If a file contains unsaved changes when you try to check it in, you will be prompted to
save the changes to complete the checkin. If you did not keep the file checked out and
you keep the file open, note that it is a read-only version.
After checking out a file, make changes to it in MATLAB or another product, and save the
file. For example, edit a file in the Editor.
If you try to change a file without first having checked it out, the file is read-only, as seen
in the title bar, and you will not be able to save any changes. This protects you from
accidentally overwriting the source control version of the file.
If you end the MATLAB session, the file remains checked out. You can check in the file
from within MATLAB during a later session, or folder from your source control system.
33-62
Check Files In and Out from MSSCCI Source Control
1 In the MATLAB Current Folder browser, select the files for which you want to undo
the checkout.
2 Right-click, and from the context menu, select Source Control > Undo Checkout.
The MATLAB Undo checkout dialog box opens, listing the files you selected.
3 Click OK.
33-63
33 Source Control Interface
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
In this section...
“Getting the Latest Version of Files for Viewing or Compiling” on page 33-64
“Removing Files from the Source Control System” on page 33-65
“Showing File History” on page 33-66
“Comparing the Working Copy of a File to the Latest Version in Source Control” on page
33-67
“Viewing Source Control Properties of a File” on page 33-69
“Starting the Source Control System” on page 33-69
1 In the MATLAB Current Folder browser, select the folders or files that you want to
get. If you select files, you cannot select folders too.
2 Right-click, and from the context menu, select Source Control > Get Latest
Version.
33-64
Additional MSSCCI Source Control Actions
The MATLAB Get latest version dialog box opens, listing the files or folders you
selected.
3 Click OK.
You can now open the file to view it, run the file, or check out the file for editing.
1 In the MATLAB Current Folder browser, select the files you want to remove.
2 Right-click, and from the context menu, select Source Control > Remove from
Source Control.
The MATLAB Remove from source control dialog box opens, listing the files you
selected.
3 Click OK.
33-65
33 Source Control Interface
1 In the MATLAB Current Folder browser, select the file for which you want to view the
history.
2 Right-click, and from the context menu, select Source Control > History.
A dialog box, which is specific to your source control system, opens. For Microsoft
Visual SourceSafe, the History Options dialog box opens, as shown in the following
example illustration.
3 Complete the dialog box to specify the range of history you want for the selected file
and click OK. For example, enter my_name for User.
The history presented depends on your source control system. For Microsoft Visual
SourceSafe, the History dialog box opens for that file, showing the file's history in
the source control system.
33-66
Additional MSSCCI Source Control Actions
1 In the MATLAB Current Folder browser, select the file for which you want to view
differences. This is a file that has been checked out and edited.
2 Right-click, and from the context menu, select Source Control > Differences.
A dialog box, which is specific to your source control system, opens. For Microsoft
Visual SourceSafe, the Difference Options dialog box opens.
3 Review the default entries in the dialog box, make any needed changes, and click OK.
The following example is for Microsoft Visual SourceSafe.
33-67
33 Source Control Interface
The method of presenting differences depends on your source control system. For
Microsoft Visual SourceSafe, the Differences for dialog box opens. This highlights
the differences between the working copy of the file and the latest checked-in version
of the file.
33-68
Additional MSSCCI Source Control Actions
1 In the MATLAB Current Folder browser, select the file for which you want to view
properties.
2 Right-click, and from the context menu, select Source Control > Properties.
A dialog box, which is specific to your source control system, opens. The following
example shows the Microsoft Visual SourceSafe properties dialog box.
33-69
33 Source Control Interface
The interface to your source control system opens, showing the source control project
associated with the current folder in MATLAB. The following example shows the
Microsoft Visual SourceSafe Explorer interface.
33-70
Access MSSCCI Source Control from Editors
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
You can create or open a file in the Editor, the Simulink or Stateflow products and
perform most source control actions from their File > Source Control menus, rather
than from the Current Folder browser. Following are some differences in the source
control interface process when you use the Editor, Simulink, or Stateflow:
33-71
33 Source Control Interface
Note MSSCCI support has been removed. Replace this functionality with one of the
following options.
• Use a source control system that is part of the MathWorks “Source Control
Integration” with the Current Folder browser.
• Use the Source Control Software Development Kit to create a plug-in for your source
control.
• Use the MATLAB system function to access the command-line API for your source
control tool. This option does not provide integration with the MATLAB Current Folder
browser menus or source control status column.
In this section...
“Source Control Error: Provider Not Present or Not Installed Properly” on page 33-72
“Restriction Against @ Character” on page 33-73
“Add to Source Control Is the Only Action Available” on page 33-73
“More Solutions for Source Control Problems” on page 33-74
Often, this error occurs because a registry key that MATLAB requires from the source
control application is not present. Make sure this registry key is present:
HKEY_LOCAL_MACHINE\SOFTWARE\SourceCodeControlProvider\
InstalledSCCProviders
33-72
Troubleshoot MSSCCI Source Control Problems
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SourceSafe\SccServerPath
This registry key has a path to a DLL-file in the file system. Make sure the DLL-file exists
in that location. If you are not familiar with registry keys, ask your system administrator
for help.
If this does not solve the problem and you use Microsoft Visual SourceSafe, try running a
client setup for your source control application. When SourceSafe is installed on a server
for a group to use, each machine client can run a setup but is not required to do so.
However, some applications that interface with SourceSafe, including MATLAB, require
you to run the client setup. Run the client setup, which should resolve the problem.
You might be able to work around this restriction by quoting nonstandard characters in
file names, such as with an escape sequence, which some source control systems allow.
Consult your source control system documentation or technical support resources for a
workaround.
33-73
33 Source Control Interface
33-74
34
Unit Testing
34-2
Write Test Using Live Script
• The name of the live-script file must start or end with the word 'test', which is case-
insensitive.
• Place each unit test into a separate section of the live-script file. The heading of each
section becomes the name of the test element. If a section has no heading, MATLAB
assigns a name to the test.
• Consider how you are running your live-script-based test. If you run the test using the
Run buttons in the Live Editor and MATLAB encounters a test failure, then it stops
execution of the script and does not run any remaining tests. If you run the live script
using the unit testing framework, such as with the runtests function, then if
MATLAB encounters a test failure, it still runs remaining tests.
• When a live script runs as a test, variables defined in one test are not accessible within
other tests. Similarly, variables defined in other workspaces are not accessible to the
tests.
Outside of this example, in your current MATLAB folder, create a function in a file,
rightTri.m. This function takes lengths of two sides of a triangle as input and returns
the three angles of the corresponding right triangle. The input sides are the two shorter
edges of the triangle, not the hypotenuse.
type rightTri.m
A = atand(sides(1)/sides(2));
B = atand(sides(2)/sides(1));
hypotenuse = sides(1)/sind(A);
C = asind(hypotenuse*sind(A)/sides(1));
angles = [A B C];
end
34-3
34 Unit Testing
You can include equations and images in your live script to help document the test. Create
the following test for the small angle approximation. Typically, when you compare
floating-point values, you specify a tolerance for the comparison.
The rightTri function should return values consistent with the small angle
approximation, such that sin(θ) ≈ θ.
∑ ak = 180∘
k
You can have multiple assert statements in the same test. However, if the first assertion
fails, the MATLAB does not evaluate the remaining statements.
The sum of all angles of the resulting right triangle should always be 180 degrees.
angles = rightTri([7 9]);
assert(sum(angles) == 180)
34-4
See Also
Test that the sides of the triangle reduce to 1 and 3. In which case, the angles are
∘ ∘ ∘
30 , 60 , and 90 .
tol = 1e-10;
angles = rightTri([2 2*sqrt(3)]);
assert(abs(angles(1)-30)<= tol)
assert(abs(angles(2)-60)<= tol)
assert(abs(angles(3)-90)<= tol)
For isosceles triangles, both of the non-right angles must be 45 degrees; otherwise
assert throws an error.
Test that two sides of the triangle are equal. In which case, the corresponding angles are
equal.
To run your tests, best practice is to use the testing framework via the runtests
function instead of the Run button in the Live Editor. The testing framework provides
additional diagnostic information. In the event of a test failure, the framework runs
subsequent tests but the Run button in the Live Editor does not. For example, to run this
test at the MATLAB command prompt, type result =
runtests('TestRightTriLiveScriptExample'). For more information, see
runtests.
See Also
assert | runtests
Related Examples
• “Write Script-Based Unit Tests” on page 34-6
• “Write Function-Based Unit Tests” on page 34-25
34-5
34 Unit Testing
Create this function in a file, rightTri.m, in your current MATLAB® folder. This
function takes lengths of two sides of a triangle as input and returns the three angles of
the corresponding right triangle. The input sides are the two shorter edges of the
triangle, not the hypotenuse.
A = atand(sides(1)/sides(2));
B = atand(sides(2)/sides(1));
hypotenuse = sides(1)/sind(A);
C = asind(hypotenuse*sind(A)/sides(1));
angles = [A B C];
end
In your working folder, create a new script, rightTriTest.m. Each unit test checks a
different output of the rightTri function. A test script must adhere to the following
conventions:
• The name of the script file must start or end with the word 'test', which is case-
insensitive.
• Place each unit test into a separate section of the script file. Each section begins with
two percent signs (%%), and the text that follows on the same line becomes the name of
the test element. If no text follows the %%, MATLAB assigns a name to the test. If
MATLAB encounters a test failure, it still runs remaining tests.
• In a test script, the shared variable section consists of any code that appears before
the first explicit code section (the first line beginning with %%). Tests share the
variables that you define in this section. Within a test, you can modify the values of
34-6
Write Script-Based Unit Tests
these variables. However, in subsequent tests, the value is reset to the value defined in
the shared variables section.
• In the shared variables section (first code section), define any preconditions necessary
for your tests. If the inputs or outputs do not meet this precondition, MATLAB does not
run any of the tests. MATLAB marks the tests as failed and incomplete.
• When a script is run as a test, variables defined in one test are not accessible within
other tests unless they are defined in the shared variables section (first code section).
Similarly, variables defined in other workspaces are not accessible to the tests.
• If the script file does not include any code sections, MATLAB generates a single test
element from the full contents of the script file. The name of the test element is the
same as the script file name. In this case, if MATLAB encounters a failed test, it halts
execution of the entire script.
In rightTriTest.m, write four tests to test the output of rightTri. Use the assert
function to test the different conditions. In the shared variables section, define four
triangle geometries and define a precondition that the rightTri function returns a right
triangle.
% test triangles
tri = [7 9];
triIso = [4 4];
tri306090 = [2 2*sqrt(3)];
triSkewed = [1 1500];
% preconditions
angles = rightTri(tri);
assert(angles(3) == 90,'Fundamental problem: rightTri not producing right triangle')
angles = rightTri(triIso);
assert(sum(angles) == 180)
angles = rightTri(tri306090);
assert(sum(angles) == 180)
angles = rightTri(triSkewed);
assert(sum(angles) == 180)
34-7
34 Unit Testing
Test 1 tests the summation of the triangle angles. If the summation is not equal to 180
degrees, assert throws an error.
Test 2 tests that if two sides are equal, the corresponding angles are equal. If the non-
right angles are not both equal to 45 degrees, the assert function throws an error.
Test 3 tests that if the triangle sides are 1 and sqrt(3), the angles are 30, 60, and 90
degrees. If this condition is not true, assert throws an error.
Test 4 tests the small-angle approximation. The small-angle approximation states that for
small angles the sine of the angle in radians is approximately equal to the angle. If it is
not true, assert throws an error.
Run Tests
Execute the runtests function to run the four tests in rightTriTest.m. The runtests
function executes each test in each code section individually. If Test 1 fails, MATLAB still
runs the remaining tests. If you execute rightTriTest as a script instead of by using
runtests, MATLAB halts execution of the entire script if it encounters a failed assertion.
Additionally, when you run tests using the runtests function, MATLAB provides
informative test diagnostics.
result = runtests('rightTriTest');
Running rightTriTest
..
34-8
Write Script-Based Unit Tests
================================================================================
Error occurred in rightTriTest/Test3_30_60_90Triangle and it did not run to completion.
---------
Error ID:
---------
'MATLAB:assertion:failed'
--------------
Error Details:
--------------
Error using rightTriTest (line 31)
Assertion failed.
================================================================================
.
================================================================================
Error occurred in rightTriTest/Test4_SmallAngleApproximation and it did not run to comp
---------
Error ID:
---------
''
--------------
Error Details:
--------------
Error using rightTriTest (line 39)
Problem with small angle approximation
================================================================================
.
Done rightTriTest
__________
Failure Summary:
The test for the 30-60-90 triangle and the test for the small-angle approximation fail in
the comparison of floating-point numbers. Typically, when you compare floating-point
values, you specify a tolerance for the comparison. In Test 3 and Test 4, MATLAB throws
an error at the failed assertion and does not complete the test. Therefore, the test is
marked as both Failed and Incomplete.
34-9
34 Unit Testing
For script-based unit tests, manually verify that the difference between two values is less
than a specified tolerance. If instead you write a function-based unit test, you can access
built-in constraints to specify a tolerance when comparing floating-point values.
% test triangles
tri = [7 9];
triIso = [4 4];
tri306090 = [2 2*sqrt(3)];
triSkewed = [1 1500];
% preconditions
angles = rightTri(tri);
assert(angles(3) == 90,'Fundamental problem: rightTri not producing right triangle')
angles = rightTri(triIso);
assert(sum(angles) == 180)
angles = rightTri(tri306090);
assert(sum(angles) == 180)
angles = rightTri(triSkewed);
assert(sum(angles) == 180)
34-10
Write Script-Based Unit Tests
angles = rightTri(triIso);
assert(angles(1) == 45)
assert(angles(1) == angles(2))
result = runtests('rightTriTolTest');
Running rightTriTolTest
....
Done rightTriTolTest
__________
rt = table(result)
rt =
4x6 table
34-11
34 Unit Testing
See Also
assert | runtests
Related Examples
• “Write Script-Based Test Using Local Functions” on page 34-13
• “Write Function-Based Unit Tests” on page 34-25
34-12
Write Script-Based Test Using Local Functions
Create this function in a file, approxSinCos.m, in your current MATLAB folder. This
function takes an angle in radians and approximates the sine and cosine of the angle
using Taylor series.
%% Test 0rad
% Test expected values of 0
[sinApprox,cosApprox] = approxSinCos(0);
assertWithAbsTol(sinApprox,0)
assertWithRelTol(cosApprox,1)
%% Test 2pi
% Test expected values of 2pi
[sinApprox,cosApprox] = approxSinCos(2*pi);
assertWithAbsTol(sinApprox,0)
34-13
34 Unit Testing
assertWithRelTol(cosApprox,1)
function assertWithAbsTol(actVal,expVal,varargin)
% Helper function to assert equality within an absolute tolerance.
% Takes two values and an optional message and compares
% them within an absolute tolerance of 1e-6.
tol = 1e-6;
tf = abs(actVal-expVal) <= tol;
assert(tf, varargin{:});
end
function assertWithRelTol(actVal,expVal,varargin)
% Helper function to assert equality within a relative tolerance.
% Takes two values and an optional message and compares
% them within a relative tolerance of 0.1%.
relTol = 0.001;
tf = abs(expVal - actVal) <= relTol.*abs(expVal);
assert(tf, varargin{:});
end
Each unit test uses assert to check different output of the approxSinCos function.
Typically, when you compare floating-point values, you specify a tolerance for the
comparison. The local functions assertWithAbsTol and assertWithRelTol are helper
functions to compute whether the actual and expected values are equal within the
specified absolute or relative tolerance.
• Test 0rad tests whether the computed and expected values for an angle of 0 radians
are within an absolute tolerance of 1e-6 or a relative tolerance 0.1%. Typically, you
use absolute tolerance to compare values close to 0.
34-14
Write Script-Based Test Using Local Functions
• Test 2pi tests whether the computed and expected values for an angle of radians
are equal within an absolute tolerance of 1e-6 or a relative tolerance 0.1%.
• Test pi over 4 equality tests whether the sine and cosine of are equal
within a relative tolerance of 0.1%.
• Test matches MATLAB fcn tests whether the computed sine and cosine of
are equal to the values from the sin and cos functions within a relative tolerance of
0.1%.
Run Tests
Execute the runtests function to run the four tests in approxSinCosTest.m. The
runtests function executes each test individually. If one test fails, MATLAB still runs the
remaining tests. If you execute approxSinCosTest as a script instead of using
runtests, MATLAB halts execution of the entire script if it encounters a failed assertion.
Additionally, when you run tests using the runtests function, MATLAB provides
informative test diagnostics.
results = runtests('approxSinCosTest');
Running approxSinCosTest
....
Done approxSinCosTest
__________
rt = table(results)
rt =
4x6 table
34-15
34 Unit Testing
See Also
assert | runtests
Related Examples
• “Write Script-Based Unit Tests” on page 34-6
More About
• “Add Functions to Scripts” on page 18-18
34-16
Extending Script-Based Tests
In this section...
“Test Suite Creation” on page 34-17
“Test Selection” on page 34-18
“Programmatic Access of Test Diagnostics” on page 34-19
“Test Runner Customization” on page 34-19
Typically, with script-based tests, you create a test file, and pass the file name to the
runtests function without explicitly creating a suite of Test objects. If you create an
explicit test suite, there are additional features available in script-based testing. These
features include selecting tests and using plugins to customize the test runner. For
additional functionality, consider using “Function-Based Unit Tests” or “Class-Based Unit
Tests”.
Also, you can create a test suite from all the test files in a specified folder using the
matlab.unittest.TestSuite.fromFolder method. If you know the name of a
particular test in your script-based test file, you can create a test suite from that test
using matlab.unittest.TestSuite.fromName.
34-17
34 Unit Testing
Test Selection
With an explicit test suite, use selectors to refine your suite. Several of the selectors are
applicable only for class-based tests, but you can select tests for your suite based on the
test name:
• Use the 'Name' name-value pair argument in a suite generation method, such as
matlab.unittest.TestSuite.fromFile.
• Use a selectors instance and optional constraints instance.
import matlab.unittest.selectors.HasName
import matlab.unittest.constraints.ContainsSubstring
import matlab.unittest.TestSuite.fromFile
f = 'rightTriTolTest.m';
selector = HasName(ContainsSubstring('Triangle'));
% fromFile, selector
suite = TestSuite.fromFile(f,selector)
% selectIf, selector
fullSuite = TestSuite.fromFile(f);
suite = selectIf(fullSuite,selector)
If you use one of the suite creation methods with a selector or name-value pair, the
testing framework creates the filtered suite. If you use the selectIf method, the testing
framework creates a full test suite and then filters it. For large test suites, this approach
can have performance implications.
34-18
See Also
After you run tests, you can access recorded diagnostics via the DiagnosticRecord field
in the Details property on TestResult. For example, if your test results are stored in
the variable results, find the recorded diagnostics for the second test in the suite by
invoking records = result(2).Details.DiagnosticRecord.
For example,use test suite, suite, to create a silent test runner and run the tests with
the run method of TestRunner.
runner = matlab.unittest.TestRunner.withNoPlugins;
results = runner.run(suite);
Use plugins to customize the test runner further. For example, you can redirect output,
determine code coverage, or change how the test runner responds to warnings. For more
information, see “Add Plugin to Test Runner” on page 34-110 and the plugins classes.
See Also
TestRunner | TestSuite | matlab.unittest.constraints | plugins | selectors
34-19
34 Unit Testing
Related Examples
• “Add Plugin to Test Runner” on page 34-110
34-20
Run Tests in Editor
The Run Tests button runs all the tests in the file. The Run Current Test button runs
the test at your current cursor location.
In the Editor, create a test in a file named sampleTest.m. When you save the test, the
Run section in the Editor tab changes to Run Tests.
function tests = sampleTest
tests = functiontests(localfunctions);
end
function testA(testCase)
verifyEqual(testCase,5,5)
end
function testB(testCase)
verifyGreaterThan(testCase,42,13)
end
function testC(testCase)
verifySubstring(testCase,'hello, world','llo')
end
Click the Run Tests icon. MATLAB displays the command it uses to run the tests in the
Command Window, and the test output follows. MATLAB runs all three tests from
sampleTest.m.
runtests('sampleTest')
Running sampleTest
...
34-21
34 Unit Testing
Done sampleTest
__________
ans =
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
3 Passed, 0 Failed, 0 Incomplete.
0.0071673 seconds testing time.
In the Editor, place your cursor in the testB function and click the Run Current Test
icon. MATLAB runs testB only.
runtests('sampleTest','ProcedureName','testB')
Running sampleTest
.
Done sampleTest
__________
ans =
Name: 'sampleTest/testB'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 9.9411e-04
Details: [1×1 struct]
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.00099411 seconds testing time.
34-22
Run Tests in Editor
In addition to running tests, you can customize the test run using the test options under
the Run Tests icon. MATLAB uses test options whether you run all the tests in a file or
just the test at your cursor location.
34-23
34 Unit Testing
When you select a test option, the selection persists for the duration of your current
MATLAB session.
See Also
runtests
34-24
Write Function-Based Unit Tests
The main function collects all of the local test functions into a test array. Since it is the
main function, the function name corresponds to the name of your .m file and follows the
naming convention of starting or ending in the word ‘test’, which is case-insensitive. In
this sample case, the MATLAB file is exampleTest.m. The main function needs to make a
call to functiontests to generate a test array, tests. Use localfunctions as the
input to functiontests to automatically generate a cell array of function handles to all
the local functions in your file. This is a typical main function.
Individual test functions are included as local functions in the same MATLAB file as the
main (test-generating) function. These test function names must begin or end with the
case-insensitive word, ‘test’. Each of the local test functions must accept a single input,
which is a function test case object, testCase. The Unit Test Framework automatically
generates this object. For more information on creating test functions, see “Write Simple
Test Case Using Functions” on page 34-30 and “Table of Verifications, Assertions, and
Other Qualifications” on page 34-60. This is a typical example of skeletal local-test
functions.
34-25
34 Unit Testing
function testFunctionOne(testCase)
% Test specific code
end
function FunctionTwotest(testCase)
% Test specific code
end
Setup and teardown code, also referred to as test fixture functions, set up the pretest
state of the system and return it to the original state after running the test. There are two
types of these functions: file fixture functions that run once per test file, and fresh fixture
functions that run before and after each local test function. These functions are not
required to generate tests. In general, it is preferable to use fresh fixtures over file
fixtures to increase unit test encapsulation.
A function test case object, testCase, must be the only input to file fixture and fresh
fixture functions. The Unit Test Framework automatically generates this object. The
TestCase object is a means to pass information between setup functions, test functions,
and teardown functions. Its TestData property is, by default, a struct, which allows
easy addition of fields and data. Typical uses for this test data include paths and graphics
handles. For an example using the TestData property, see “Write Test Using Setup and
Teardown Functions” on page 34-35.
File Fixture Functions
Use file fixture functions to share setup and teardown functions across all the tests in a
file. The names for the file fixture functions must be setupOnce and teardownOnce,
respectively. These functions execute a single time for each file. You can use file fixtures
to set a path before testing, and then reset it to the original path after testing. This is a
typical example of skeletal file fixture setup and teardown code.
34-26
Write Function-Based Unit Tests
Use fresh fixture functions to set up and tear down states for each local test function. The
names for these fresh fixture functions must be setup and teardown, respectively. You
can use fresh fixtures to obtain a new figure before testing and to close the figure after
testing. This is typical example of skeletal test function level setup and teardown code.
function setup(testCase) % do not change function name
% open a figure, for example
end
%% Test Functions
function testFunctionOne(testCase)
% Test specific code
end
function FunctionTwotest(testCase)
% Test specific code
end
34-27
34 Unit Testing
To run tests from the command prompt, use the runtests command with your MATLAB
test file as input. For example:
results = runtests('exampleTest.m')
results = run(exampleTest)
For more information on running tests see the runtests reference page and “Run Tests
for Various Workflows” on page 34-105.
See Also
functiontests | localfunctions | runtests
34-28
See Also
Related Examples
• “Write Simple Test Case Using Functions” on page 34-30
• “Write Test Using Setup and Teardown Functions” on page 34-35
34-29
34 Unit Testing
This MATLAB function solves quadratic equations. Create this function in a folder on your
MATLAB path.
end
34-30
Write Simple Test Case Using Functions
Use the qualification function, verifyEqual, to compare the output of the function,
actSolution, to the desired output, expSolution. If the qualification fails, the
framework continues executing the test. Typically, when using verifyEqual on floating
point values, you specify a tolerance for the comparison. For more information, see
matlab.unittest.constraints.
Create a test to verify that quadraticSolver returns the right value for imaginary
solutions. For example, the equation x2 + 2x + 10 = 0 has imaginary solutions x = -1
+ 3i and x = -1 - 3i. Typically, when using verifyEqual on floating point values,
you specify a tolerance for the comparison. For more information, see
matlab.unittest.constraints.
The order of the tests within the solverTest.m file does not matter because they are
fully independent test cases.
The following is the complete solverTest.m test file. Save this file in a folder on your
MATLAB path.
34-31
34 Unit Testing
function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2 1];
verifyEqual(testCase,actSolution,expSolution)
end
function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
expSolution = [-1+3i -1-3i];
verifyEqual(testCase,actSolution,expSolution)
end
results = runtests('solverTest.m')
Running solverTest
..
Done solverTest
__________
results =
Name
Passed
Failed
Incomplete
Duration
Totals:
2 Passed, 0 Failed, 0 Incomplete.
0.19172 seconds testing time.
34-32
Write Simple Test Case Using Functions
Cause one of the tests to fail by forcing roots in quadraticSolver.m to be real. Before
ending the function, add the line: roots = real(roots);. (Do not change
solverTest.m.) Save the file and run the tests.
results = runtests('solverTest.m')
Running solverTest
.
================================================================================
Verification failed in solverTest/testImaginarySolution.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> Complexity does not match.
Actual Complexity:
Real
Expected Complexity:
Complex
Actual Value:
-1 -1
Expected Value:
-1.000000000000000 + 3.000000000000000i -1.000000000000000 - 3.000000000000000i
------------------
Stack Information:
------------------
In C:\work\solverTest.m (testImaginarySolution) at 14
================================================================================
.
Done solverTest
__________
Failure Summary:
results =
Name
Passed
Failed
Incomplete
Duration
34-33
34 Unit Testing
Totals:
1 Passed, 1 Failed, 0 Incomplete.
0.043751 seconds testing time.
See Also
matlab.unittest.constraints
More About
• “Write Function-Based Unit Tests” on page 34-25
• “Table of Verifications, Assertions, and Other Qualifications” on page 34-60
34-34
Write Test Using Setup and Teardown Functions
Create a file containing the main function that tests figure axes properties and include
two test functions. One function verifies that the x-axis limits are correct, and the other
one verifies that the face color of a surface is correct.
File fixture functions are setup and teardown code that runs a single time in your test file.
These fixtures are shared across the test file. In this example, the file fixture functions
create a temporary folder and set it as the current working folder. They also create and
save a new figure for testing. After tests are complete, the framework reinstates the
original working folder and deletes the temporary folder and saved figure.
In this example, a helper function creates a simple figure — a red cylinder. In a more
realistic scenario, this code is part of the product under test and is computationally
expensive, thus motivating the intent to create the figure only once and to load
independent copies of the result for each test function. For this example, however, you
want to create this helper function as a local function to axesPropertiesTest. Note
that the test array does not include the function because its name does not start or end
with ‘test’.
Write a helper function that creates a simple red cylinder and add it as a local function to
axesPropertiesTest.
34-35
34 Unit Testing
function f = createFigure
f = figure;
ax = axes('Parent', f);
cylinder(ax,10)
h = findobj(ax,'Type','surface');
h.FaceColor = [1 0 0];
end
You must name the setup and teardown functions of a file test fixture setupOnce and
teardownOnce, respectively. These functions take a single input argument, testCase,
into which the test framework automatically passes a function test case object. This test
case object contains a TestData structure that allows data to pass between setup, test,
and teardown functions. In this example, the TestData structure uses assigned fields to
store the original path, the temporary folder name, and the figure file name.
function setupOnce(testCase)
% create and change to temporary folder
testCase.TestData.origPath = pwd;
testCase.TestData.tmpFolder = ['tmpFolder' datestr(now,30)];
mkdir(testCase.TestData.tmpFolder)
cd(testCase.TestData.tmpFolder)
function teardownOnce(testCase)
delete(testCase.TestData.figName)
cd(testCase.TestData.origPath)
rmdir(testCase.TestData.tmpFolder)
34-36
Write Test Using Setup and Teardown Functions
end
Fresh fixtures are function level setup and teardown code that runs before and after each
test function in your file. In this example, the functions open the saved figure and find the
handles. After testing, the framework closes the figure.
You must name fresh fixture functions setup and teardown, respectively. Similar to the
file fixture functions, these functions take a single input argument, testCase. In this
example, these functions create a new field in the TestData structure that includes
handles to the figure and to the axes. This allows information to pass between setup, test,
and teardown functions.
function setup(testCase)
testCase.TestData.Figure = openfig(testCase.TestData.figName);
testCase.TestData.Axes = findobj(testCase.TestData.Figure,...
'Type','Axes');
end
function teardown(testCase)
close(testCase.TestData.Figure)
end
In addition to custom setup and teardown code, the Unit Testing Framework provides
some classes for creating fixtures. For more information see
matlab.unittest.fixtures.
Each test is a local function that follows the naming convention of having ‘test’ at the
beginning or end of the function name. The test array does not include local functions
that do not follow this convention. Similar to setup and teardown functions, individual test
functions must accept a single input argument, testCase. Use this test case object for
verifications, assertions, assumptions, and fatal assertions functions.
34-37
34 Unit Testing
The testDefaultXLim function test verifies that the x-axis limits are large enough to
display the cylinder. The lower limit needs to be less than -10, and the upper limit needs
to be greater than 10. These values come from the figure generated in the helper function
— a cylinder with a 10 unit radius centered on the origin. This test function opens the
figure created and saved in the setupOnce function, queries the axes limit, and verifies
the limits are correct. The qualification functions, verifyLessThanOrEqual and
verifyGreaterThanOrEqual, takes the test case, the actual value, the expected value,
and optional diagnostic information to display in the case of failure as inputs.
function testDefaultXLim(testCase)
xlim = testCase.TestData.Axes.XLim;
verifyLessThanOrEqual(testCase, xlim(1), -10,...
'Minimum x-limit was not small enough')
verifyGreaterThanOrEqual(testCase, xlim(2), 10,...
'Maximum x-limit was not big enough')
end
The surfaceColorTest function accesses the figure that you created and saved in the
setupOnce function. surfaceColorTest queries the face color of the cylinder and
verifies that it is red. The color red has an RGB value of [1 0 0]. The qualification
function, verifyEqual, takes as inputs the test case, the actual value, the expected
value, and optional diagnostic information to display in the case of failure. Typically when
using verifyEqual on floating point-values, you specify a tolerance for the comparison.
For more information, see matlab.unittest.constraints.
function surfaceColorTest(testCase)
h = findobj(testCase.TestData.Axes,'Type','surface');
co = h.FaceColor;
verifyEqual(testCase, co, [1 0 0],'FaceColor is incorrect')
end
34-38
Write Test Using Setup and Teardown Functions
Now the axesPropertiesTest.m file is complete with a main function, file fixture
functions, fresh fixture functions, and two local test functions. You are ready to run the
tests.
Run Tests
The next step is to run the tests using the runtests function. In this example, the call to
runtests results in the following steps:
1 The main function creates a test array.
2 The file fixture records the working folder, creates a temporary folder, sets the
temporary folder as the working folder, then generates and saves a figure.
3 The fresh fixture setup opens the saved figure and finds the handles.
4 The testDefaultXLim test is run.
5 The fresh fixture teardown closes the figure.
6 The fresh fixture setup opens the saved figure and finds the handles.
7 The surfaceColorTest test is run.
8 The fresh fixture teardown closes the figure.
9 The file fixture teardown deletes the saved figure, changes back to the original path
and deletes the temporary folder.
Running axesPropertiesTest
..
Done axesPropertiesTest
__________
results =
Name
Passed
Failed
Incomplete
Duration
Details
34-39
34 Unit Testing
Totals:
2 Passed, 0 Failed, 0 Incomplete.
2.8671 seconds testing time.
To access functionality available to tables, create one from the TestResult object.
rt = table(results)
rt =
2x6 table
writetable(rt,'myTestResults.xls')
sortrows(rt,'Duration')
ans =
2x6 table
34-40
See Also
See Also
matlab.unittest.constraints | matlab.unittest.fixtures
More About
• “Write Function-Based Unit Tests” on page 34-25
• “Table of Verifications, Assertions, and Other Qualifications” on page 34-60
34-41
34 Unit Testing
In this section...
“Fixtures for Setup and Teardown Code” on page 34-42
“Test Logging and Verbosity” on page 34-43
“Test Suite Creation” on page 34-44
“Test Selection” on page 34-44
“Test Running” on page 34-45
“Programmatic Access of Test Diagnostics” on page 34-45
“Test Runner Customization” on page 34-46
Typically, with function-based tests, you create a test file and pass the file name to the
runtests function without explicitly creating a suite of Test objects. However, if you
create an explicit test suite, additional features are available in function-based testing.
These features include:
These fixtures take the place of manually coding the actions in the setupOnce,
teardownOnce, setup, and teardown functions of your function-based test.
34-42
Extending Function-Based Tests
For example, if you manually write setup and teardown code to set up a temporary folder
for each test, and then you make that folder your current working folder, your setup and
teardown functions could look like this.
function setup(testCase)
% store current folder
testCase.TestData.origPath = pwd;
function teardown(testCase)
% change to original folder
cd(testCase.TestData.origPath)
However, you also can use a fixture to replace both of those functions with just a modified
setup function. The fixture stores the information necessary to restore the initial state
and performs the teardown actions.
function setup(testCase)
% create temporary folder
f = testCase.applyFixture(matlab.unittest.fixtures.TemporaryFolderFixture);
34-43
34 Unit Testing
Test Selection
With an explicit test suite, use selectors to refine your suite. Several of the selectors are
applicable only for class-based tests, but you can select tests for your suite based on the
test name:
• Use the 'Name' name-value pair argument in a suite generation method, such as
matlab.unittest.TestSuite.fromFile.
• Use a selectors instance and optional constraints instance.
import matlab.unittest.selectors.HasName
import matlab.unittest.constraints.ContainsSubstring
import matlab.unittest.TestSuite.fromFile
f = 'rightTriTolTest.m';
selector = HasName(ContainsSubstring('Triangle'));
% fromFile, selector
suite = TestSuite.fromFile(f,selector)
% selectIf, selector
fullSuite = TestSuite.fromFile(f);
suite = selectIf(fullSuite,selector)
34-44
Extending Function-Based Tests
If you use one of the suite creation methods with a selector or name-value pair, the
testing framework creates the filtered suite. If you use the selectIf method, the testing
framework creates a full test suite and then filters it. For large test suites, this approach
can have performance implications.
Test Running
There are several ways to run a function-based test.
For more information, see “Run Tests for Various Workflows” on page 34-105.
After you run tests, you can access recorded diagnostics via the DiagnosticRecord field
in the Details property on TestResult. For example, if your test results are stored in
the variable results, find the recorded diagnostics for the second test in the suite by
invoking records = result(2).Details.DiagnosticRecord.
34-45
34 Unit Testing
For example,use test suite, suite, to create a silent test runner and run the tests with
the run method of TestRunner.
runner = matlab.unittest.TestRunner.withNoPlugins;
results = runner.run(suite);
Use plugins to customize the test runner further. For example, you can redirect output,
determine code coverage, or change how the test runner responds to warnings. For more
information, see “Add Plugin to Test Runner” on page 34-110 and the plugins classes.
See Also
matlab.unittest.TestCase | matlab.unittest.TestSuite |
matlab.unittest.constraints | matlab.unittest.diagnostics |
matlab.unittest.qualifications | matlab.unittest.selectors
Related Examples
• “Run Tests for Various Workflows” on page 34-105
• “Add Plugin to Test Runner” on page 34-110
34-46
Author Class-Based Unit Tests in MATLAB
%% Test Function
function testASolution(testCase)
%% Exercise function under test
% act = the value from the function under test
34-47
34 Unit Testing
end
end
Qualifications are methods for testing values and responding to failures. This table lists
the types of qualifications.
Often, each unit test function obtains an actual value by exercising the code that you are
testing and defines the associated expected value. For example, if you are testing the
34-48
See Also
plus function, the actual value might be plus(2,3) and the expected value 5. Within
the test function, you pass the actual and expected values to a qualification method. For
example:
testCase.verifyEqual(plus(2,3),5)
For an example of a basic unit test, see “Write Simple Test Case Using Classes” on page
34-51.
• Setup and teardown methods blocks to implicitly set up the pretest state of the system
and return it to the original state after running the tests. For an example of a test class
with setup and teardown code, see “Write Setup and Teardown Code Using Classes”
on page 34-56.
• Advanced qualification features, including actual value proxies, test diagnostics, and a
constraint interface. For more information, see matlab.unittest.constraints
and matlab.unittest.diagnostics.
• Parameterized tests to combine and execute tests on the specified lists of parameters.
For more information, see “Create Basic Parameterized Test” on page 34-82 and
“Create Advanced Parameterized Test” on page 34-88.
• Ready-to-use fixtures for handling the setup and teardown of frequently used testing
actions and for sharing fixtures between classes. For more information, see
matlab.unittest.fixtures and “Write Tests Using Shared Fixtures” on page 34-
68.
• Ability to create custom test fixtures. For more information see “Create Basic Custom
Fixture” on page 34-72 and “Create Advanced Custom Fixture” on page 34-75.
See Also
Related Examples
• “Write Simple Test Case Using Classes” on page 34-51
• “Write Setup and Teardown Code Using Classes” on page 34-56
34-49
34 Unit Testing
34-50
Write Simple Test Case Using Classes
The following MATLAB function solves quadratic equations. Create this function in a
folder on your MATLAB path.
end
To use the matlab.unittest framework, write MATLAB functions (tests) in the form of
a test case, a class derived from matlab.unittest.TestCase.
methods (Test)
end
34-51
34 Unit Testing
end
The following steps show how to create specific tests. Put these tests inside the methods
block with the (Test) attribute.
function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution)
end
Create a test to verify that quadraticSolver returns the right value for imaginary
solutions. For example, the equation has imaginary solutions
and . Add this function, testImaginarySolution, inside the
methods (Test) block.
function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
34-52
Write Simple Test Case Using Classes
The order of the tests within the block does not matter.
The following is the complete SolverTest class definition. Save this file in a folder on
your MATLAB path.
methods (Test)
function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution);
end
function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
expSolution = [-1+3i, -1-3i];
testCase.verifyEqual(actSolution,expSolution);
end
end
end
testCase = SolverTest;
res = run(testCase)
Running SolverTest
..
Done SolverTest
__________
34-53
34 Unit Testing
res =
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 0 Failed, 0 Incomplete.
0.061058 seconds testing time.
testCase = SolverTest;
res = run(testCase,'testRealSolution')
Running SolverTest
.
Done SolverTest
__________
res =
Name: 'SolverTest/testRealSolution'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.0111
Details: [1x1 struct]
Totals:
1 Passed, 0 Failed, 0 Incomplete.
34-54
See Also
See Also
Related Examples
• “Author Class-Based Unit Tests in MATLAB” on page 34-47
• “Write Setup and Teardown Code Using Classes” on page 34-56
• “Analyze Test Case Results” on page 34-144
• “Create Simple Test Suites” on page 34-102
34-55
34 Unit Testing
Test Fixtures
Test fixtures are setup and teardown code that sets up the pretest state of the system and
returns it to the original state after running the test. Setup and teardown methods are
defined in the TestCase class by the following method attributes:
It is good practice for test authors to perform all teardown activities from within the
TestMethodSetup and TestClassSetup blocks using the addTeardown method
instead of implementing corresponding teardown methods in the TestMethodTeardown
and TestClassTeardown blocks. This guarantees the teardown is executed in the
reverse order of the setup and also ensures that the test content is exception safe.
properties
34-56
Write Setup and Teardown Code Using Classes
TestFigure
end
methods(TestMethodSetup)
function createFigure(testCase)
% comment
testCase.TestFigure = figure;
end
end
methods(TestMethodTeardown)
function closeFigure(testCase)
close(testCase.TestFigure)
end
end
methods(Test)
function defaultCurrentPoint(testCase)
cp = testCase.TestFigure.CurrentPoint;
testCase.verifyEqual(cp, [0 0], ...
'Default current point is incorrect')
end
function defaultCurrentObject(testCase)
import matlab.unittest.constraints.IsEmpty
co = testCase.TestFigure.CurrentObject;
testCase.verifyThat(co, IsEmpty, ...
'Default current object should be empty')
end
end
end
To setup the BankAccountTest, which tests the BankAccount class example described
in “Developing Classes — Typical Workflow”, add a TestClassSetup method,
addBankAccountClassToPath. This method adds the path to the BankAccount
34-57
34 Unit Testing
example file. Typically, you set up the path using a PathFixture. This example performs
the setup and teardown activities manually for illustrative purposes.
methods (TestClassSetup)
function addBankAccountClassToPath(testCase)
p = path;
testCase.addTeardown(@path,p);
addpath(fullfile(matlabroot,'help','techdoc','matlab_oop',...
'examples'));
end
end
methods (Test)
function testConstructor(testCase)
b = BankAccount(1234, 100);
testCase.verifyEqual(b.AccountNumber, 1234, ...
'Constructor failed to correctly set account number');
testCase.verifyEqual(b.AccountBalance, 100, ...
'Constructor failed to correctly set account balance');
end
function testConstructorNotEnoughInputs(testCase)
import matlab.unittest.constraints.Throws;
testCase.verifyThat(@()BankAccount, ...
Throws('MATLAB:minrhs'));
end
function testDesposit(testCase)
b = BankAccount(1234, 100);
b.deposit(25);
testCase.verifyEqual(b.AccountBalance, 125);
end
function testWithdraw(testCase)
b = BankAccount(1234, 100);
b.withdraw(25);
testCase.verifyEqual(b.AccountBalance, 75);
end
function testNotifyInsufficientFunds(testCase)
callbackExecuted = false;
34-58
See Also
function testCallback(~,~)
callbackExecuted = true;
end
b = BankAccount(1234, 100);
b.addlistener('InsufficientFunds', @testCallback);
b.withdraw(50);
testCase.assertFalse(callbackExecuted, ...
'The callback should not have executed yet');
b.withdraw(60);
testCase.verifyTrue(callbackExecuted, ...
'The listener callback should have fired');
end
end
end
See Also
addTeardown | matlab.unittest.TestCase
Related Examples
• “Author Class-Based Unit Tests in MATLAB” on page 34-47
• “Write Simple Test Case Using Classes” on page 34-51
34-59
34 Unit Testing
34-60
Table of Verifications, Assertions, and Other Qualifications
34-61
34 Unit Testing
See Also
Assertable | Assumable | FatalAssertable | Verifiable |
matlab.unittest.qualifications
34-62
Tag Unit Tests
Tag Tests
To define test tags, use a cell array of meaningful character vectors or a string array. For
example, TestTags = {'Unit'} or TestTags = ["Unit","FeatureA"].
This sample test class, ExampleTagTest, uses the TestTags method attribute to tag
individual tests.
34-63
34 Unit Testing
Several of the tests in class ExampleTagTest are tagged. For example, testD is tagged
with 'Unit' and 'FeatureA'. One test, testA, is not tagged.
This sample test class, ExampleTagClassTest, uses a TestTags class attribute to tag
all the tests within the class, and a TestTags method attribute to add tags to individual
tests.
classdef (TestTags = {'FeatureB'}) ...
ExampleTagClassTest < matlab.unittest.TestCase
methods (Test)
function testF (testCase)
% test code
end
end
methods (Test, TestTags = {'FeatureC','System'})
function testG (testCase)
% test code
end
end
methods (Test, TestTags = {'System','FeatureA'})
function testH (testCase)
% test code
end
end
end
Use the runtests function to select and run tests without explicitly creating a test suite.
Select and run all the tests from ExampleTagTest and ExampleTagClassTest that
include the 'FeatureA' tag.
34-64
Tag Unit Tests
results = runtests({'ExampleTagTest','ExampleTagClassTest'},'Tag','FeatureA');
Running ExampleTagTest
..
Done ExampleTagTest
__________
Running ExampleTagClassTest
.
Done ExampleTagClassTest
__________
ans =
3×6 table
The selected tests are testE and testD from ExampleTagTest, and testH from
ExampleTagClassTest.
Create a suite of tests from the ExampleTagTest class that are tagged with
'FeatureA'.
import matlab.unittest.TestSuite
sA = TestSuite.fromClass(?ExampleTagTest,'Tag','FeatureA');
Create a suite of tests from the ExampleTagClassTest class that are tagged with
'FeatureC'.
sB = TestSuite.fromFile('ExampleTagClassTest.m','Tag','FeatureC');
34-65
34 Unit Testing
ans =
'ExampleTagTest/testE'
'ExampleTagTest/testD'
'ExampleTagClassTest/testG'
Create a suite of all the tests from the ExampleTagTest and ExampleTagClassTest
classes.
import matlab.unittest.selectors.HasTag
sA = TestSuite.fromClass(?ExampleTagTest);
sB = TestSuite.fromFile('ExampleTagClassTest.m');
suite = [sA sB];
s1 = suite.selectIf(~HasTag)
s1 =
Name: 'ExampleTagTest/testA'
ProcedureName: 'testA'
TestClass: "ExampleTagTest"
BaseFolder: 'C:\work'
Parameterization: [0×0 matlab.unittest.parameters.EmptyParameter]
SharedTestFixtures: [0×0 matlab.unittest.fixtures.EmptyFixture]
Tags: {1×0 cell}
Tests Include:
0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
Select all the tests with the 'Unit' tag and display their names.
s2 = suite.selectIf(HasTag('Unit'));
{s2.Name}'
34-66
See Also
ans =
'ExampleTagTest/testD'
'ExampleTagTest/testB'
'ExampleTagTest/testC'
Select all the tests with the 'FeatureB' or 'System' tag using a constraint.
import matlab.unittest.constraints.IsEqualTo
constraint = IsEqualTo('FeatureB') | IsEqualTo('System');
s3 = suite.selectIf(HasTag(constraint));
{s3.Name}'
ans =
'ExampleTagTest/testE'
'ExampleTagClassTest/testH'
'ExampleTagClassTest/testG'
'ExampleTagClassTest/testF'
See Also
matlab.unittest.TestCase | matlab.unittest.TestSuite |
matlab.unittest.constraints | matlab.unittest.selectors.HasTag |
runtests
34-67
34 Unit Testing
The two test classes used in this example test the DocPolynom class and the
BankAccount class. You can access both classes in MATLAB, but you must add them to
the MATLAB path. A path fixture adds the directory to the current path, runs the tests,
and removes the directory from the path. Since both classes require the same addition to
the path, the tests use a shared fixture.
Create a test file for the DocPolynom class. Create the shared fixture by specifying the
SharedTestFixtures attribute for the TestCase and passing in a PathFixture.
properties
msgEqn = 'Equation under test: ';
end
methods (Test)
function testConstructor(testCase)
p = DocPolynom([1, 0, 1]);
testCase.verifyClass(p, ?DocPolynom)
end
function testAddition(testCase)
p1 = DocPolynom([1, 0, 1]);
p2 = DocPolynom([5, 2]);
actual = p1 + p2;
expected = DocPolynom([1, 5, 3]);
msg = [testCase.msgEqn,...
34-68
Write Tests Using Shared Fixtures
function testMultiplication(testCase)
p1 = DocPolynom([1, 0, 3]);
p2 = DocPolynom([5, 2]);
actual = p1 * p2;
expected = DocPolynom([5, 2, 15, 6]);
msg = [testCase.msgEqn,...
'(x^2 + 3) * (5*x + 2) = 5*x^3 + 2*x^2 + 15*x + 6'];
testCase.verifyEqual(actual, expected, msg)
end
end
end
Create a test file for the BankAccount class. Create the shared fixture by specifying the
SharedTestFixtures attribute for the TestCase and passing in a PathFixture.
methods (Test)
function testConstructor(testCase)
b = BankAccount(1234, 100);
testCase.verifyEqual(b.AccountNumber, 1234, ...
'Constructor failed to correctly set account number')
testCase.verifyEqual(b.AccountBalance, 100, ...
'Constructor failed to correctly set account balance')
end
function testConstructorNotEnoughInputs(testCase)
import matlab.unittest.constraints.Throws
testCase.verifyThat(@()BankAccount, ...
Throws('MATLAB:minrhs'))
end
34-69
34 Unit Testing
function testDesposit(testCase)
b = BankAccount(1234, 100);
b.deposit(25)
testCase.verifyEqual(b.AccountBalance, 125)
end
function testWithdraw(testCase)
b = BankAccount(1234, 100);
b.withdraw(25)
testCase.verifyEqual(b.AccountBalance, 75)
end
function testNotifyInsufficientFunds(testCase)
callbackExecuted = false;
function testCallback(~,~)
callbackExecuted = true;
end
b = BankAccount(1234, 100);
b.addlistener('InsufficientFunds', @testCallback);
b.withdraw(50)
testCase.assertFalse(callbackExecuted, ...
'The callback should not have executed yet')
b.withdraw(60)
testCase.verifyTrue(callbackExecuted, ...
'The listener callback should have fired')
end
end
end
34-70
See Also
result = run(suiteFolder);
Setting up PathFixture.
Description: Adds 'C:\Program Files\MATLAB\R2013b\help\techdoc\matlab_oop\examples' to
__________
Running BankAccountTest
.....
Done BankAccountTest
__________
Running DocPolynomTest
...
Done DocPolynomTest
__________
The test framework sets up the test fixture, runs all the tests in each file, and then tears
the fixture down. If the path fixture was set up and torn down using TestClassSetup
methods, the fixture is set up and torn down twice—once for each test file.
See Also
PathFixture | TestCase | matlab.unittest.fixtures
34-71
34 Unit Testing
In a file in your working folder, create a new class, FormatHexFixture that inherits
from the matlab.unittest.fixtures.Fixture class. Since we want the fixture to
restore the pretest state of the MATLAB display format, create an OriginalFormat
property to keep track of the original display format.
Subclasses of the Fixture class must implement the setup method. Use this method to
record the pretest display format, and set the format to 'hex'. Use the teardown
method to restore the original display format. Define the setup and teardown methods
in the methods block of the FormatHexFixture.m file.
methods
function setup(fixture)
fixture.OriginalFormat = get(0, 'Format');
set(0, 'Format', 'hex')
end
function teardown(fixture)
set(0, 'Format', fixture.OriginalFormat)
end
end
end
In a file in your working folder, create the following test class, SampleTest.m.
34-72
See Also
function test1(testCase)
testCase.applyFixture(FormatHexFixture);
actStr = getColumnForDisplay([1;2;3], 'Small Integers');
expStr = ['Small Integers '
'3ff0000000000000'
'4000000000000000'
'4008000000000000'];
testCase.verifyEqual(actStr, expStr)
end
end
end
This test applies the custom fixture and verifies that the displayed column of hexadecimal
representation is as expected.
run(SampleTest);
Running SampleTest
.
Done SampleTest
__________
See Also
matlab.unittest.fixtures.Fixture
34-73
34 Unit Testing
Related Examples
• “Create Advanced Custom Fixture” on page 34-75
• “Write Tests Using Shared Fixtures” on page 34-68
34-74
Create Advanced Custom Fixture
properties (SetAccess=private)
UserName
end
Subclasses of the Fixture class must implement the setup method. Use this method to
save the original UserName variable. This method also defines the
TeardownDescription and registers the teardown task of setting the UserName to the
original state after testing.
34-75
34 Unit Testing
Classes that derive from Fixture must implement the isCompatible method if the
constructor is configurable. Since you can configure the UserName property through the
constructor, UserNameEnvironmentVariableFixture must implement
isCompatible.
The isCompatible method is called with two instances of the same class. In this case, it
is called with two instances of UserNameEnvironmentVariableFixture. The testing
framework considers the two instances compatible if their UserName properties are
equal.
properties (SetAccess=private)
UserName
end
methods
function fixture = UserNameEnvironmentVariableFixture(name)
validateattributes(name, {'char'}, {'row'}, '','UserName')
fixture.UserName = name;
fixture.SetupDescription = sprintf( ...
'Set the UserName environment variable to "%s".',...
fixture.UserName);
end
34-76
Create Advanced Custom Fixture
function setup(fixture)
originalUserName = getenv('UserName');
fixture.assertNotEmpty(originalUserName, ...
'An existing UserName environment variable must be defined.')
fixture.addTeardown(@setenv, 'UserName', originalUserName)
fixture.TeardownDescription = sprintf(...
'Restored the UserName environment variable to "%s".',...
originalUserName);
setenv('UserName', fixture.UserName)
end
end
methods (Access=protected)
function bool = isCompatible(fixture, other)
bool = strcmp(fixture.UserName, other.UserName);
end
end
end
In a file in your working folder, create the following test class, ExampleTest.m.
classdef ExampleTest < matlab.unittest.TestCase
methods(TestMethodSetup)
function mySetup(testCase)
testCase.applyFixture(...
UserNameEnvironmentVariableFixture('David'));
end
end
methods (Test)
function t1(~)
fprintf(1, 'Current UserName: "%s"', getenv('UserName'))
end
end
end
Running ExampleTest
Current UserName: "David".
Done ExampleTest
__________
34-77
34 Unit Testing
In your working folder, create three test classes using a shared fixture. Using a shared
fixture allows the UserNameEnvironmentVariableFixture to be shared across
classes.
34-78
Create Advanced Custom Fixture
Setting up UserNameEnvironmentVariableFixture
Done setting up UserNameEnvironmentVariableFixture: Set the UserName environment variab
__________
Running testA
Current UserName: "David".
Done testA
__________
Setting up UserNameEnvironmentVariableFixture
Done setting up UserNameEnvironmentVariableFixture: Set the UserName environment variab
__________
Running testB
Current UserName: "Andy".
Done testB
__________
Running testC
Current UserName: "Andy".
Done testC
__________
Recall that the fixtures are compatible if their UserName properties match. The tests in
testA and testB use incompatible shared fixtures, since 'David' is not equal to
'Andy'. Therefore, the framework invokes the fixture teardown and setup methods
between calls to testA and testB. However, the shared test fixture in testC is
compatible with the fixture in testB, so the framework doesn’t repeat fixture teardown
and setup before testC.
An alternate approach to using the addTeardown method within the setup method is to
implement a separate teardown method . Instead of the setup method described above,
implement the following setup and teardown methods within
UserNameEnvironmentVariableFixture.m.
34-79
34 Unit Testing
properties (Access=private)
OriginalUser
end
properties (SetAccess=private)
UserName
end
methods
function fixture = UserNameEnvironmentVariableFixture(name)
validateattributes(name, {'char'}, {'row'}, '','UserName')
fixture.UserName = name;
fixture.SetupDescription = sprintf( ...
'Set the UserName environment variable to "%s".',...
fixture.UserName);
end
function setup(fixture)
fixture.OriginalUser = getenv('UserName');
fixture.assertNotEmpty(fixture.OriginalUser, ...
'An existing UserName environment variable must be defined.')
setenv('UserName', fixture.UserName)
end
function teardown(fixture)
fixture.TeardownDescription = sprintf(...
'Restored the UserName environment variable to "%s".',...
fixture.OriginalUser);
setenv('UserName', fixture.OriginalUser)
end
end
methods (Access=protected)
function bool = isCompatible(fixture, other)
bool = strcmp(fixture.UserName, other.UserName);
end
end
end
The setup method does not contain a call to addTeardown or a definition for
TeardownDescription. These tasks are relegated to the teardown method. The
alternative class definition contains an additional property, OriginalUser, which allows
the information to be passed between methods.
See Also
matlab.unittest.fixtures.Fixture
34-80
See Also
Related Examples
• “Create Basic Custom Fixture” on page 34-72
• “Write Tests Using Shared Fixtures” on page 34-68
34-81
34 Unit Testing
In your working folder, create a function in the file sierpinski.m. This function returns
a matrix representing an image of a Sierpinski carpet fractal. It takes as input the fractal
level and an optional data type.
function carpet = sierpinski(nLevels,classname)
if nargin == 1
classname = 'single';
end
mSize = 3^nLevels;
carpet = ones(mSize,classname);
function cutCarpet(x,y,s,cL)
if cL
ss = s/3; % define subsize
for lx = 0:2
for ly = 0:2
if lx == 1 && ly == 1
% remove center square
carpet(x+ss:x+2*ss-1,y+ss:y+2*ss-1) = 0;
else
% recurse
cutCarpet(x + lx*ss, y + ly*ss, ss, cL-1)
end
end
end
end
end
end
In a file in your working folder, create a new class, TestCarpet, to test the sierpinski
function.
classdef TestCarpet < matlab.unittest.TestCase
34-82
Create Basic Parameterized Test
Define the properties used for parameterized testing. In the TestCarpet class, define
these properties in a property block with the TestParameter attribute.
properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small', 2,'medium', 4, 'large', 6);
side = struct('small', 9, 'medium', 81,'large', 729);
end
The type property contains the different data types you want to test. The level property
contains the different fractal level you want to test. The side property contains the
number of rows and columns in the Sierpinski carpet matrix and corresponds to the
level property. To provide meaningful names for each parameterization value, level
and side are defined as structs.
methods (Test)
function testRemainPixels(testCase, level)
% expected number pixels equal to 1
expPixelCount = 8^level;
% actual number pixels equal to 1
actPixels = find(sierpinski(level));
testCase.verifyNumElements(actPixels,expPixelCount)
end
function testDefaultL1Output(testCase)
exp = single([1 1 1; 1 0 1; 1 1 1]);
testCase.verifyEqual(sierpinski(1), exp)
end
end
34-83
34 Unit Testing
This method uses the level property and, therefore, results in three test elements—one
for each value in level. The testClass method tests the class of the output from the
sierpinski function with each combination of the type and level properties. This
approach results in nine test elements. The testDefaultL1Output test method does
not use a TestParameter property and, therefore, is not parameterized. This test
method verifies that the level 1 matrix contains the expected values. Since the test
method is not parameterized, it results in a one test element.
In the test methods above, you did not define the ParameterCombination attribute of
the Test methods block. This attribute is, by default, 'exhaustive'. The test
framework invokes a given test method once for every combination of the test
parameters.
Define the following test methods in the TestCarpet class to ensure that the matrix
output by the sierpinski function has the correct number of elements. Set the
ParameterCombination attribute to 'sequential'.
properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small', 2,'medium', 4, 'large', 6);
side = struct('small', 9, 'medium', 81,'large', 729);
34-84
Create Basic Parameterized Test
end
methods (Test)
function testRemainPixels(testCase, level)
% expected number pixels equal to 1
expPixelCount = 8^level;
% actual number pixels equal to 1
actPixels = find(sierpinski(level));
testCase.verifyNumElements(actPixels,expPixelCount)
end
function testDefaultL1Output(testCase)
exp = single([1 1 1; 1 0 1; 1 1 1]);
testCase.verifyEqual(sierpinski(1), exp)
end
end
suite = matlab.unittest.TestSuite.fromFile('TestCarpet.m');
{suite.Name}'
ans =
'TestCarpet/testNumel(level=small,side=small)'
'TestCarpet/testNumel(level=medium,side=medium)'
'TestCarpet/testNumel(level=large,side=large)'
'TestCarpet/testRemainPixels(level=small)'
'TestCarpet/testRemainPixels(level=medium)'
34-85
34 Unit Testing
'TestCarpet/testRemainPixels(level=large)'
'TestCarpet/testClass(type=single,level=small)'
'TestCarpet/testClass(type=single,level=medium)'
'TestCarpet/testClass(type=single,level=large)'
'TestCarpet/testClass(type=double,level=small)'
'TestCarpet/testClass(type=double,level=medium)'
'TestCarpet/testClass(type=double,level=large)'
'TestCarpet/testClass(type=uint16,level=small)'
'TestCarpet/testClass(type=uint16,level=medium)'
'TestCarpet/testClass(type=uint16,level=large)'
'TestCarpet/testDefaultL1Output'
The suite had 16 test elements. The element’s Name indicates any parameterization.
suite.run;
Running TestCarpet
..........
......
Done TestCarpet
__________
Use the selectIf method of the TestSuite to select test elements that use a particular
parameterization. Select all test elements that use the parameter name small in the
level parameter property list.
s1 = suite.selectIf('ParameterName','small');
{s1.Name}'
ans =
'TestCarpet/testNumel(level=small,side=small)'
'TestCarpet/testRemainPixels(level=small)'
'TestCarpet/testClass(type=single,level=small)'
'TestCarpet/testClass(type=double,level=small)'
'TestCarpet/testClass(type=uint16,level=small)'
s1.run;
Running TestCarpet
.....
34-86
See Also
Done TestCarpet
__________
Alternatively, create the same test suite directly from the fromFile method of
TestSuite.
import matlab.unittest.selectors.HasParameter
s1 = matlab.unittest.TestSuite.fromFile('TestCarpet.m',...
HasParameter('Name','small'));
See Also
matlab.unittest.TestCase | matlab.unittest.selectors.HasParameter |
selectIf
Related Examples
• “Create Advanced Parameterized Test” on page 34-88
34-87
34 Unit Testing
Test Overview
At each test level, you can use the ParameterCombination method attribute to specify
the test parameterization.
34-88
Create Advanced Parameterized Test
For this example, class setup-level parameterization defines the type of random number
generator. The method setup-level parameterization defines the seed for the random
number generator, and the test-level parameterization defines the data type and size of
the random number output.
Define the properties used for parameterized testing. Each properties block
corresponds to parameterization at a particular level.
properties (ClassSetupParameter)
generator = {'twister','combRecursive','multFibonacci'};
end
properties (MethodSetupParameter)
seed = {0, 123, 4294967295};
end
properties (TestParameter)
dim1 = struct('small', 1,'medium', 2, 'large', 3);
dim2 = struct('small', 2,'medium', 3, 'large', 4);
dim3 = struct('small', 3,'medium', 4, 'large', 5);
34-89
34 Unit Testing
type = {'single','double'};
end
Define the setup methods at the test class and test method level. These methods register
the initial random number generator state. After the framework runs the tests, the
methods restore the original state. The ClassSetup method defines the type of random
number generator, and the TestMethodSetup seeds the generator.
methods (TestClassSetup)
function ClassSetup(testCase, generator)
orig = rng;
testCase.addTeardown(@rng, orig)
rng(0, generator)
end
end
methods (TestMethodSetup)
function MethodSetup(testCase, seed)
orig = rng;
testCase.addTeardown(@rng, orig)
rng(seed)
end
end
The method tests the size of the output for each corresponding parameter in dim1, dim2,
and dim3. For example, to test all the 'medium' values use:
testCase.verifySize(rand(2,3,4),[2 3 4]);. For a given TestClassSetup and
TestMethodSetup parameterization, the framework calls the testSize method three
times—once each for the 'small', 'medium', and 'large' values.
34-90
Create Advanced Parameterized Test
The test method verifies that the random number generator results are repeatable. For a
given TestClassSetup and TestMethodSetup parameterization, the framework calls
the testRepeatble method 10 times to ensure testing of each pair of dim1, dim2, and
dim3. However, if the parameter combination attribute is exhaustive, the framework calls
the method 3^3=27 times.
Define a methods block with the Test attribute or no defined parameter combination.
The parameter combination is exhaustive by default. The test framework invokes these
methods once for every combination of property values.
methods (Test)
function testClass(testCase,dim1,dim2,type)
testCase.verifyClass(rand(dim1,dim2,type), type)
end
end
The test method verifies that the class of the output from rand is the same as the
expected class. For a given TestClassSetup and TestMethodSetup parameterization,
the framework calls the testClass method 3*3*2=18 times to ensure testing of each
combination of dim1, dim2, and type.
34-91
34 Unit Testing
end
properties (MethodSetupParameter)
seed = {0, 123, 4294967295};
end
properties (TestParameter)
dim1 = struct('small', 1,'medium', 2, 'large', 3);
dim2 = struct('small', 2,'medium', 3, 'large', 4);
dim3 = struct('small', 3,'medium', 4, 'large', 5);
type = {'single','double'};
end
methods (TestClassSetup)
function ClassSetup(testCase, generator)
orig = rng;
testCase.addTeardown(@rng, orig)
rng(0, generator)
end
end
methods (TestMethodSetup)
function MethodSetup(testCase, seed)
orig = rng;
testCase.addTeardown(@rng, orig)
rng(seed)
end
end
34-92
Create Advanced Parameterized Test
methods (Test)
function testClass(testCase,dim1,dim2,type)
testCase.verifyClass(rand(dim1,dim2,type), type)
end
end
end
suite =
Name
ProcedureName
TestClass
BaseFolder
Parameterization
SharedTestFixtures
Tags
Tests Include:
17 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
The test suite contains 279 test elements. For a given TestClassSetup and
TestMethodSetup parameterization, the framework creates 3+10+18=31 test elements.
These 31 elements are called three times—once for each TestMethodSetup
parameterization resulting in 3*31=93 test elements for each TestClassSetup
parameterization. There are three TestClassSetup parameterizations resulting in a
total of 3*93=279 test elements.
ans =
'TestRand[generator=twister]/[seed=value1]testClass(dim1=small,dim2=small,type=sing
The name of each element is constructed from the combination of the following:
34-93
34 Unit Testing
The name for the seed property isn’t particularly meaningful (value1). The testing
framework provided this name because the seed property values are numbers. For a
more meaningful name, define the seed property as a struct with more descriptive field
names.
At the command prompt, create a selector to select test elements that test the
'twister' generator for 'single' precision. Omit test elements that use properties
with the 'large' name.
import matlab.unittest.selectors.HasParameter
s = HasParameter('Property','generator', 'Name','twister') & ...
HasParameter('Property','type', 'Name','single') & ...
~HasParameter('Name','large');
suite2 = matlab.unittest.TestSuite.fromClass(?TestRand,s)
suite2 =
Name
ProcedureName
TestClass
BaseFolder
Parameterization
SharedTestFixtures
Tags
Tests Include:
9 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
If you first generate the full suite, construct the same test suite as above using the
selectIf method.
34-94
Create Advanced Parameterized Test
suite = matlab.unittest.TestSuite.fromClass(?TestRand);
suite2 = selectIf(suite,s);
suite2.run;
Running TestRand
..........
..
Done TestRand
__________
At the command prompt, create a selector that omits test elements that use properties
with the 'large' or 'medium' name. Limit results to test elements from the
testRepeatable method.
import matlab.unittest.selectors.HasParameter
s = ~(HasParameter('Name','large') | HasParameter('Name','medium'));
suite3 = matlab.unittest.TestSuite.fromMethod(?TestRand,'testRepeatable',s);
{suite3.Name}'
ans =
'TestRand[generator=twister]/[seed=value1]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=twister]/[seed=value2]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=twister]/[seed=value3]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=combRecursive]/[seed=value1]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=combRecursive]/[seed=value2]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=combRecursive]/[seed=value3]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=multFibonacci]/[seed=value1]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=multFibonacci]/[seed=value2]testRepeatable(dim1=small,dim2=small,dim3=small)'
'TestRand[generator=multFibonacci]/[seed=value3]testRepeatable(dim1=small,dim2=small,dim3=small)'
suite3.run;
Running TestRand
.........
Done TestRand
__________
34-95
34 Unit Testing
At the command prompt, run all the test elements from TestRand.m that use the
parameter name 'double'.
runtests('TestRand','ParameterName','double');
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
.
Done TestRand
__________
See Also
matlab.unittest.TestCase | matlab.unittest.TestSuite |
matlab.unittest.selectors
Related Examples
• “Create Basic Parameterized Test” on page 34-82
34-96
Use External Parameters in Parameterized Test
Create the following function to test. The function accepts an array, vectorizes the array,
removes 0, Nan, and Inf, and then sorts the array.
function Y = cleanData(X)
Y = X(:); % Vectorize array
Y = rmmissing(Y); % Remove NaN
% Remove 0 and Inf
idx = (Y==0 | Y==Inf);
Y = Y(~idx);
% If array is empty, set to eps
if isempty(Y)
Y = eps;
end
Y = sort(Y); % Sort vector
end
Create the following parameterized test for the cleanData function. The test repeats
each of the four test methods for the two data sets that are defined in the properties
block.
34-97
34 Unit Testing
end
function finiteCheck(testCase,Data)
import matlab.unittest.constraints.IsFinite
act = cleanData(Data);
testCase.verifyThat(act,IsFinite)
end
function noZeroCheck(testCase,Data)
import matlab.unittest.constraints.EveryElementOf
import matlab.unittest.constraints.IsEqualTo
act = cleanData(Data);
testCase.verifyThat(EveryElementOf(act),~IsEqualTo(0))
end
end
end
Create and run a parameterized test suite. View the results. The framework runs the
eight parameterized tests using the data defined in the test file.
import matlab.unittest.TestSuite
suite1 = TestSuite.fromClass(?TestClean);
results = suite1.run;
table(results)
Running TestClean
........
Done TestClean
__________
ans =
8×6 table
34-98
Use External Parameters in Parameterized Test
Create a parameter with the external data set. The fromData method accepts the name
of the parameterized property from the properties block in TestClean and the new
data as a cell array (or struct).
import matlab.unittest.parameters.Parameter
newData = {A};
param = Parameter.fromData('Data',newData);
Create and a new test suite and view the suite element names. The fromClass method
accepts the new parameter.
suite2 = TestSuite.fromClass(?TestClean,'ExternalParameters',param);
{suite2.Name}'
ans =
{'TestClean/classCheck(Data=value1#ext)' }
{'TestClean/sortCheck(Data=value1#ext)' }
{'TestClean/finiteCheck(Data=value1#ext)'}
{'TestClean/noZeroCheck(Data=value1#ext)'}
Using the external parameter, the framework creates four suite elements. Since the
parameters are defined as a cell array, MATLAB generates the parameter name (value1).
Also, it appends the characters #ext to the end of the parameter name, indicating the
parameter is defined externally.
To assign meaningful parameter names (instead of valueN), define the parameter using a
struct. View the suite element names and run the tests.
newData = struct('commandLineData',A);
param = Parameter.fromData('Data',newData);
suite2 = TestSuite.fromClass(?TestClean,'ExternalParameters',param);
{suite2.Name}'
results = suite2.run;
34-99
34 Unit Testing
ans =
{'TestClean/classCheck(Data=commandLineData#ext)' }
{'TestClean/sortCheck(Data=commandLineData#ext)' }
{'TestClean/finiteCheck(Data=commandLineData#ext)'}
{'TestClean/noZeroCheck(Data=commandLineData#ext)'}
Running TestClean
....
Done TestClean
__________
B = rand(3);
B(2,4) = 0;
dlmwrite('myFile.dat',B)
clear B
newData = struct('commandLineData',A,'storedData',dlmread('myFile.dat'));
param2 = Parameter.fromData('Data',newData);
suite3 = TestSuite.fromClass(?TestClean,'ExternalParameters',param2);
To run the tests using parameters defined in the test file and externally, concatenate test
suites. View the suite element names and run the tests.
ans =
{'TestClean/classCheck(Data=clean)' }
{'TestClean/classCheck(Data=needsCleaning)' }
{'TestClean/sortCheck(Data=clean)' }
{'TestClean/sortCheck(Data=needsCleaning)' }
34-100
See Also
{'TestClean/finiteCheck(Data=clean)' }
{'TestClean/finiteCheck(Data=needsCleaning)' }
{'TestClean/noZeroCheck(Data=clean)' }
{'TestClean/noZeroCheck(Data=needsCleaning)' }
{'TestClean/classCheck(Data=commandLineData#ext)' }
{'TestClean/classCheck(Data=storedData#ext)' }
{'TestClean/sortCheck(Data=commandLineData#ext)' }
{'TestClean/sortCheck(Data=storedData#ext)' }
{'TestClean/finiteCheck(Data=commandLineData#ext)'}
{'TestClean/finiteCheck(Data=storedData#ext)' }
{'TestClean/noZeroCheck(Data=commandLineData#ext)'}
{'TestClean/noZeroCheck(Data=storedData#ext)' }
Running TestClean
........
Done TestClean
__________
Running TestClean
........
Done TestClean
__________
See Also
matlab.unittest.TestSuite |
matlab.unittest.parameters.Parameter.fromData
Related Examples
• “Create Basic Parameterized Test” on page 34-82
• “Create Advanced Parameterized Test” on page 34-88
34-101
34 Unit Testing
Create the following function that solves roots of the quadratic equation in a file,
quadraticSolver.m, in your working folder.
function roots = quadraticSolver(a, b, c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.
end
Create the following test class in a file, SolverTest.m, in your working folder.
classdef SolverTest < matlab.unittest.TestCase
% SolverTest tests solutions to the quadratic equation
% a*x^2 + b*x + c = 0
methods (Test)
function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution);
end
function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
expSolution = [-1+3i, -1-3i];
testCase.verifyEqual(actSolution,expSolution);
34-102
Create Simple Test Suites
end
end
end
import matlab.unittest.TestSuite
Make sure the SolverTest class definition file is on your MATLAB path.
The fromClass method creates a suite from all Test methods in the SolverTest class.
suiteClass = TestSuite.fromClass(?SolverTest);
result = run(suiteClass);
The fromFile method creates a suite using the name of the file to identify the class.
suiteFile = TestSuite.fromFile('SolverTest.m');
result = run(suiteFile);
The fromFolder method creates a suite from all test case files in the specified folder. For
example, the following files are in the current folder:
• BankAccountTest.m
• DocPolynomTest.m
• FigurePropertiesTest.m
• IsSupportedTest.m
• SolverTest.m
suiteFolder = TestSuite.fromFolder(pwd);
result = run(suiteFolder);
34-103
34 Unit Testing
suiteMethod = TestSuite.fromMethod(?SolverTest,'testRealSolution')'
result = run(suiteMethod);
See Also
TestSuite
Related Examples
• “Write Simple Test Case Using Classes” on page 34-51
34-104
Run Tests for Various Workflows
You can also assign the test file output to a variable and run the tests using the functional
form or dot notation.
34-105
34 Unit Testing
results2 = run(t2);
Alternatively, you can run tests contained in a single file by using runtests or from the
Editor.
results1 = run(DocPolynomTest,'testMultiplication');
Function-based test files return an array of Test objects instead of a single TestCase
object. You can run a particular test by indexing into the array. However, you must
examine the Name field in the test array to ensure you run the correct test. For example,
only run the test, surfaceColorTest, from the axesPropertiesTest file.
ans =
axesPropertiesTest/testDefaultXLim
ans =
axesPropertiesTest/surfaceColorTest
34-106
Run Tests for Various Workflows
suite = {'axesPropertiesTest','DocPolynomTest'};
runtests(suite);
Run all tests in the current folder using the pwd as input to the runtests function.
runtests(pwd);
Alternatively, you can explicitly create Test arrays and use the run method to run them.
import matlab.unittest.TestSuite
s1 = TestSuite.fromClass(?DocPolynomTest);
s2 = TestSuite.fromFile('axesPropertiesTest.m');
Since the suite is explicitly defined, it is easy for you to perform further analysis on the
suite, such as rerunning failed tests.
failedTests = fullSuite([result.Failed]);
result2 = run(failedTests);
import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
34-107
34 Unit Testing
import matlab.unittest.plugins.TestRunProgressPlugin
% Generate TestSuite.
s1 = TestSuite.fromClass(?DocPolynomTest);
s2 = TestSuite.fromFile('axesPropertiesTest.m');
suite = [s1 s2];
See Also
run | run | run | runtests
More About
• “Run Tests in Editor” on page 34-21
34-108
Programmatically Access Test Diagnostics
After you run tests, you can access recorded diagnostics via the DiagnosticRecord field
in the Details property on TestResult. For example, if your test results are stored in
the variable results, find the recorded diagnostics for the second test in the suite by
invoking records = result(2).Details.DiagnosticRecord.
See Also
matlab.unittest.TestResult |
matlab.unittest.plugins.DiagnosticsRecordingPlugin |
matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord
Related Examples
• “Add Plugin to Test Runner” on page 34-110
34-109
34 Unit Testing
In a file in your working folder, create a test file for the BankAccount class.
type BankAccountTest.m
methods (TestClassSetup)
function addBankAccountClassToPath(testCase)
p = path;
testCase.addTeardown(@path,p);
addpath(fullfile(matlabroot,'help','techdoc','matlab_oop',...
'examples'));
end
end
methods (Test)
function testConstructor(testCase)
b = BankAccount(1234, 100);
testCase.verifyEqual(b.AccountNumber, 1234, ...
'Constructor failed to correctly set account number');
testCase.verifyEqual(b.AccountBalance, 100, ...
'Constructor failed to correctly set account balance');
end
function testConstructorNotEnoughInputs(testCase)
import matlab.unittest.constraints.Throws;
testCase.verifyThat(@()BankAccount, ...
Throws('MATLAB:minrhs'));
end
function testDesposit(testCase)
b = BankAccount(1234, 100);
b.deposit(25);
testCase.verifyEqual(b.AccountBalance, 125);
34-110
Add Plugin to Test Runner
end
function testWithdraw(testCase)
b = BankAccount(1234, 100);
b.withdraw(25);
testCase.verifyEqual(b.AccountBalance, 75);
end
function testNotifyInsufficientFunds(testCase)
callbackExecuted = false;
function testCallback(~,~)
callbackExecuted = true;
end
b = BankAccount(1234, 100);
b.addlistener('InsufficientFunds', @testCallback);
b.withdraw(50);
testCase.assertFalse(callbackExecuted, ...
'The callback should not have executed yet');
b.withdraw(60);
testCase.verifyTrue(callbackExecuted, ...
'The listener callback should have fired');
end
end
end
At the command prompt, create a test suite, ts, from the BankAccountTest test case.
ts = matlab.unittest.TestSuite.fromClass(?BankAccountTest);
runner = matlab.unittest.TestRunner.withNoPlugins;
res = runner.run(ts);
No output displayed.
34-111
34 Unit Testing
import matlab.unittest.plugins.TestRunProgressPlugin
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))
res = runner.run(ts);
Running BankAccountTest
.....
Done BankAccountTest
__________
See Also
matlab.unittest.plugins
34-112
Write Plugins to Extend TestRunner
At the test suite, test class, and test levels, the reportFinalizedResult method
enables the TestRunner to report finalized test results. A test result is finalized when no
remaining test content can modify it. The TestRunner determines if it invokes the
reportFinalizedResult method at each level. At the test session level, the
reportFinalizedSuite method enables the TestRunner to report test results once
the test suite is finalized.
34-113
34 Unit Testing
The creation methods are the only set of TestRunnerPlugin methods with an output
argument. Typically, you extend the creation methods to listen for various events
originating from the test content at the corresponding level. Since both TestCase and
Fixture instances inherit from the handle class, you add listeners using the
addlistener method. The methods that set up, run, and tear down test content extend
the way the TestRunner evaluates the test content.
The run method at this level, runTestSuite, extends the running of a portion of the
entire TestSuite array that the testing framework passes to the TestRunner. The
reportFinalizedSuite method extends the reporting of a test suite that has been
finalized by runTestSuite.
At this level, the createSharedTestFixture method is the only plugin method with an
output argument. It returns the Fixture instances for each shared fixture required by a
test class. These fixture instances are available to the test through the
getSharedTestFixtures method of TestCase.
The run method at this level, runTestClass, extends the running of tests that belong to
the same test class or the same function-based test, and incorporates the functionality
described for the test class level plugin methods.
34-114
Write Plugins to Extend TestRunner
At this level, the createTestClassInstance method is the only plugin method with an
output argument. It returns the TestCase instances created at the class level. For each
class, the testing framework passes the instance into any methods with the
TestClassSetup or TestClassTeardown attribute.
The run method at this level, runTest, extends the running of a single TestSuite
element, and incorporates the functionality described for the test level plugin methods.
The testing framework evaluates methods at the test class level within the scope of the
runTestClass method. If the TestClassSetup code completes successfully, it invokes
the runTest method one time for each element in the TestSuite array. Each
TestClassSetup parameterization invokes the creation, setup, and teardown methods a
single time.
34-115
34 Unit Testing
At this level, the createTestMethodInstance method is the only plugin method with
an output argument. It returns the TestCase instances created for each Test element.
The testing framework passes each of these instances into the corresponding Test
methods, and into any methods with the TestMethodSetup or TestMethodTeardown
attribute.
The testing framework evaluates methods at the test level within the scope of the
runTest method. Provided the framework completes all TestMethodSetup work, it
invokes the plugin methods at this level a single time per Test element.
See Also
addlistener | matlab.unittest.TestCase | matlab.unittest.TestRunner |
matlab.unittest.TestSuite | matlab.unittest.fixtures.Fixture |
matlab.unittest.plugins.OutputStream |
matlab.unittest.plugins.Parallelizable |
matlab.unittest.plugins.TestRunnerPlugin
Related Examples
• “Create Custom Plugin” on page 34-117
• “Run Tests in Parallel with Custom Plugin” on page 34-124
• “Plugin to Generate Custom Test Output Format” on page 34-140
• “Write Plugin to Save Diagnostic Details” on page 34-135
34-116
Create Custom Plugin
To keep track of the number of passing and failing assertions, define two read-only
properties, NumPassingAssertions and NumFailingAssertions, within a
properties block.
plugin.NumPassingAssertions = 0;
plugin.NumFailingAssertions = 0;
runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
34-117
34 Unit Testing
The testing framework evaluates this method one time. It displays information about the
total number of tests, initializes the properties used by the plugin to generate text output,
and invokes the superclass method. After the framework completes evaluating the
superclass method, the runTestSuite method displays the assertion count summary by
calling the helper method printAssertionSummary (see Define Helper Methods on
page 34-0 ).
Invoke the corresponding superclass method when you override the creation methods.
The creation methods return the content that the testing framework creates for each of
their respective contexts. When implementing one of these methods using the
incrementPassingAssertionsCount and incrementFailingAssertionsCount
helper methods on page 34-0 , add the listeners required by the plugin to the returned
Fixture or TestCase instance.
fixture.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
fixture.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
34-118
Create Custom Plugin
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
end
Extend runTest to display the name of each test at run time. Include this method in a
methods block with protected access. Like all plugin methods, the runTest method
requires you to invoke the corresponding superclass method.
methods (Access = protected)
function runTest(plugin, pluginData)
fprintf('### Running test: %s\n', pluginData.Name)
runTest@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
end
end
In a methods block with private access, define three helper methods. These methods
increment the number of passing or failing assertions, and print the assertion count
summary.
methods (Access = private)
function incrementPassingAssertionsCount(plugin)
plugin.NumPassingAssertions = plugin.NumPassingAssertions + 1;
end
function incrementFailingAssertionsCount(plugin)
plugin.NumFailingAssertions = plugin.NumFailingAssertions + 1;
end
function printAssertionSummary(plugin)
34-119
34 Unit Testing
plugin.NumPassingAssertions = 0;
plugin.NumFailingAssertions = 0;
runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
fixture.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
fixture.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
34-120
Create Custom Plugin
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
runTest@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
end
end
function incrementFailingAssertionsCount(plugin)
plugin.NumFailingAssertions = plugin.NumFailingAssertions + 1;
end
function printAssertionSummary(plugin)
fprintf('%s\n', repmat('_', 1, 30))
fprintf('Total Assertions: %d\n', ...
plugin.NumPassingAssertions + plugin.NumFailingAssertions)
fprintf('\t%d Passed, %d Failed\n', ...
plugin.NumPassingAssertions, plugin.NumFailingAssertions)
end
end
end
In your current folder, create a file named ExampleTest.m containing the following test
class.
34-121
34 Unit Testing
At the command prompt, create a test suite from the ExampleTest class.
import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
suite = TestSuite.fromClass(?ExampleTest);
Create a TestRunner instance with no plugins. This code creates a silent runner and
gives you control over the installed plugins.
runner = TestRunner.withNoPlugins;
result = runner.run(suite);
runner.addPlugin(AssertionCountingPlugin)
result = runner.run(suite);
See Also
addlistener | matlab.unittest.TestCase | matlab.unittest.TestRunner |
matlab.unittest.fixtures.Fixture |
matlab.unittest.plugins.OutputStream |
matlab.unittest.plugins.TestRunnerPlugin
34-122
See Also
Related Examples
• “Write Plugins to Extend TestRunner” on page 34-113
• “Run Tests in Parallel with Custom Plugin” on page 34-124
• “Write Plugin to Save Diagnostic Details” on page 34-135
34-123
34 Unit Testing
To keep track of the number of passing and failing assertions, define four read-only
properties within a properties block. Each MATLAB worker on the current parallel pool
uses NumPassingAssertions and NumFailingAssertions to track the number of
passing and failing assertions when running a portion of the TestSuite array. The
MATLAB client uses FinalizedNumPassingAssertions and
FinalizedNumFailingAssertions to aggregate the results from different workers and
to report the total number of passing and failing assertions at the end of the test session.
To extend the running of the entire TestSuite array, override the runSession method
of TestRunnerPlugin in a methods block with protected access. The testing
framework evaluates this method one time on the client.
34-124
Run Tests in Parallel with Custom Plugin
plugin.FinalizedNumPassingAssertions = 0;
plugin.FinalizedNumFailingAssertions = 0;
runSession@matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
runSession displays information about the total number of Test elements, initializes
the properties used by the plugin to generate text output, and invokes the superclass
method to trigger the entire test run. After the framework completes evaluating the
superclass method, runSession displays the assertion count summary by calling the
helper method printAssertionSummary (see Define Helper Methods on page 34-0 ).
Invoke the corresponding superclass method when you override the creation methods.
The creation methods return the content that the testing framework creates for each of
their respective contexts. When implementing one of these methods using the
incrementPassingAssertionsCount and incrementFailingAssertionsCount
helper methods on page 34-0 , add the listeners required by the plugin to the returned
Fixture or TestCase instance.
fixture.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
fixture.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
34-125
34 Unit Testing
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
end
The testing framework divides the entire TestSuite array into different groups and
assigns them to workers for processing. Each worker can run one or more test suite
portions. To customize the behavior of workers, override the runTestSuite method of
TestRunnerPlugin in a methods block with protected access.
Extend the TestRunner to display the identifier of each test group that a worker runs
along with the number of Test elements within the group. Additionally, store the number
of passing and failing assertions in a buffer so that the client can retrieve these values to
produce the finalized results. Like all plugin methods, the runTestSuite method
requires you to invoke the corresponding superclass method at an appropriate point. In
this case, invoke the superclass method after initializing the properties and before storing
the worker data. The testing framework evaluates runTestSuite on the workers as
many times as the number of test suite portions.
methods (Access = protected)
function runTestSuite(plugin, pluginData)
suiteSize = numel(pluginData.TestSuite);
groupNumber = pluginData.Group;
fprintf('### Running a total of %d tests in group %d\n', ...
34-126
Run Tests in Parallel with Custom Plugin
suiteSize, groupNumber);
plugin.NumPassingAssertions = 0;
plugin.NumFailingAssertions = 0;
runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
reportFinalizedSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
end
end
34-127
34 Unit Testing
In a methods block with private access, define three helper methods. These methods
increment the number of passing or failing assertions within each running test suite
portion, and print the assertion count summary.
function incrementFailingAssertionsCount(plugin)
plugin.NumFailingAssertions = plugin.NumFailingAssertions + 1;
end
function printAssertionSummary(plugin)
fprintf('%s\n', repmat('_', 1, 30))
fprintf('Total Assertions: %d\n', plugin.FinalizedNumPassingAssertions + ...
plugin.FinalizedNumFailingAssertions)
fprintf('\t%d Passed, %d Failed\n', plugin.FinalizedNumPassingAssertions, ...
plugin.FinalizedNumFailingAssertions)
end
end
runSession@matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
34-128
Run Tests in Parallel with Custom Plugin
plugin.printAssertionSummary()
end
fixture.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
fixture.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
testCase.addlistener('AssertionPassed', ...
@(~,~)plugin.incrementPassingAssertionsCount);
testCase.addlistener('AssertionFailed', ...
@(~,~)plugin.incrementFailingAssertionsCount);
end
runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
34-129
34 Unit Testing
plugin.FinalizedNumFailingAssertions + assertionStruct.Failing;
reportFinalizedSuite@matlab.unittest.plugins.TestRunnerPlugin(...
plugin, pluginData);
end
end
function incrementFailingAssertionsCount(plugin)
plugin.NumFailingAssertions = plugin.NumFailingAssertions + 1;
end
function printAssertionSummary(plugin)
fprintf('%s\n', repmat('_', 1, 30))
fprintf('Total Assertions: %d\n', plugin.FinalizedNumPassingAssertions + ...
plugin.FinalizedNumFailingAssertions)
fprintf('\t%d Passed, %d Failed\n', plugin.FinalizedNumPassingAssertions, ...
plugin.FinalizedNumFailingAssertions)
end
end
end
In your current folder, create a file named ExampleTest.m containing the following
parameterized test class. This class results in 300 Test elements, 100 of which are
assertion tests that compare pseudorandom integers between 1 and 10.
properties (TestParameter)
num1 = repmat({@()randi(10)}, 1, 10);
num2 = repmat({@()randi(10)}, 1, 10);
end
methods(Test)
function testAssert(testCase, num1, num2)
testCase.assertNotEqual(num1(), num2())
end
function testVerify(testCase, num1, num2)
testCase.verifyNotEqual(num1(), num2())
end
function testAssume(testCase, num1, num2)
testCase.assumeNotEqual(num1(), num2())
end
34-130
Run Tests in Parallel with Custom Plugin
end
end
At the command prompt, create a test suite from the ExampleTest class.
import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
suite = TestSuite.fromClass(?ExampleTest);
Create a TestRunner instance with no plugins. This code creates a silent runner and
gives you control over the installed plugins.
runner = TestRunner.withNoPlugins;
Add AssertionCountingPlugin to the runner and run the tests in parallel. You can
also run the same tests in serial mode if you invoke the run method on the runner.
runner.addPlugin(AssertionCountingPlugin)
result = runner.runInParallel(suite);
----------------
Finished Group 1
----------------
### Running a total of 20 tests in group 1
----------------
Finished Group 2
----------------
### Running a total of 20 tests in group 2
----------------
Finished Group 3
34-131
34 Unit Testing
----------------
### Running a total of 19 tests in group 3
----------------
Finished Group 4
----------------
### Running a total of 19 tests in group 4
----------------
Finished Group 5
----------------
### Running a total of 18 tests in group 5
----------------
Finished Group 7
----------------
### Running a total of 18 tests in group 7
----------------
Finished Group 8
----------------
### Running a total of 17 tests in group 8
----------------
Finished Group 9
----------------
### Running a total of 17 tests in group 9
-----------------
Finished Group 10
-----------------
### Running a total of 17 tests in group 10
-----------------
Finished Group 11
-----------------
### Running a total of 16 tests in group 11
-----------------
Finished Group 12
-----------------
### Running a total of 16 tests in group 12
-----------------
34-132
See Also
Finished Group 15
-----------------
### Running a total of 15 tests in group 15
-----------------
Finished Group 14
-----------------
### Running a total of 15 tests in group 14
-----------------
Finished Group 17
-----------------
### Running a total of 14 tests in group 17
-----------------
Finished Group 16
-----------------
### Running a total of 14 tests in group 16
-----------------
Finished Group 13
-----------------
### Running a total of 15 tests in group 13
-----------------
Finished Group 18
-----------------
### Running a total of 12 tests in group 18
See Also
addlistener | matlab.unittest.TestCase | matlab.unittest.TestResult |
matlab.unittest.TestRunner | matlab.unittest.TestSuite |
matlab.unittest.fixtures.Fixture |
matlab.unittest.plugins.Parallelizable |
matlab.unittest.plugins.TestRunnerPlugin | runInParallel
34-133
34 Unit Testing
Related Examples
• “Write Plugins to Extend TestRunner” on page 34-113
• “Create Custom Plugin” on page 34-117
34-134
Write Plugin to Save Diagnostic Details
Create Plugin
In a file in your working folder, create a class, myPlugin, that inherits from the
matlab.unittest.plugins.TestRunnerPlugin class. In the plugin class:
• Define a FailedTestData property on the plugin that stores information from failed
tests.
• Override the default createTestMethodInstance method of TestRunnerPlugin
to listen for assertion, fatal assertion, and verification failures, and to record relevant
information.
• Override the default runTestSuite method of TestRunnerPlugin to initialize the
FailedTestData property value. If you do not initialize value of the property, each
time you run the tests using the same test runner, failed test information is appended
to the FailedTestData property.
• Define a helper function, recordData, to save information about the test failure as a
table.
properties
FailedTestData
end
34-135
34 Unit Testing
matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
testName = pluginData.Name;
testCase.addlistener('AssertionFailed', ...
@(~,event)plugin.recordData(event,testName, 'Assertion'));
testCase.addlistener('FatalAssertionFailed', ...
@(~,event)plugin.recordData(event,testName, 'Fatal Assertion'));
testCase.addlistener('VerificationFailed', ...
@(~,event)plugin.recordData(event,testName, 'Verification'));
end
end
In your working folder, create the file ExampleTest.m containing the following test
class.
classdef ExampleTest < matlab.unittest.TestCase
methods(Test)
function testOne(testCase)
testCase.assertGreaterThan(5,10)
end
function testTwo(testCase)
wrongAnswer = 'wrong';
testCase.verifyEmpty(wrongAnswer,'Not Empty')
testCase.verifyClass(wrongAnswer,'double','Not double')
end
34-136
Write Plugin to Save Diagnostic Details
function testThree(testCase)
testCase.assertEqual(7*2,13,'Values not equal')
end
function testFour(testCase)
testCase.fatalAssertEqual(3+2,6);
end
end
end
The fatal assertion failure in testFour causes the framework to halt and throw an error.
In this example, there are no subsequent tests. If there was a subsequent test, the
framework would not run it.
At the command prompt, create a test suite from the ExampleTest class, and create a
test runner.
import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
suite = TestSuite.fromClass(?ExampleTest);
runner = TestRunner.withNoPlugins;
Create an instance of myPlugin and add it to the test runner. Run the tests.
p = DiagnosticRecorderPlugin;
runner.addPlugin(p)
result = runner.run(suite);
With the failed fatal assertion, the framework throws an error, and the test runner does
not return a TestResult object. However, the DiagnosticRecorderPlugin stores
information about the tests preceding and including the test with the failed assertion.
At the command prompt, view information about the failed tests. The information is saved
in the FailedTestData property of the plugin.
T = p.FailedTestData
34-137
34 Unit Testing
T =
5×6 table
There are many options to archive or post-process this information. For example, you can
save the variable as a MAT-file or use writetable to write the table to various file types,
such as .txt, .csv, or .xls.
T.Stack(3)
ans =
file: 'C:\Work\ExampleTest.m'
name: 'ExampleTest.testTwo'
line: 9
Display the diagnostics that the framework displayed for the fifth test failure.
celldisp(T.FrameworkDiagnostics(5))
ans{1} =
fatalAssertEqual failed.
--> The values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________ _____ __________________
5 6 -1 -0.166666666666667
Actual Value:
5
34-138
See Also
Expected Value:
6
See Also
addlistener | matlab.unittest.TestCase | matlab.unittest.TestRunner |
matlab.unittest.plugins.TestRunnerPlugin
Related Examples
• “Write Plugins to Extend TestRunner” on page 34-113
• “Create Custom Plugin” on page 34-117
• “Plugin to Generate Custom Test Output Format” on page 34-140
34-139
34 Unit Testing
Create Plugin
In a file in your working folder, create a class, ExampleCustomPlugin, that inherits from
the matlab.unittest.plugins.TestRunnerPlugin class. In the plugin class:
• Define a Stream property on the plugin that stores the OutputStream instance. By
default, the plugin writes to standard output.
• Override the default runTestSuite method of TestRunnerPlugin to output text
that indicates the test runner is running a new test session. This information is
especially useful if you are writing to a single log file, as it allows you to differentiate
the test runs.
• Override the default reportFinalizedResult method of TestRunnerPlugin to
write finalized test results to the output stream. You can modify the print method to
output the test results in a format that works for your test logs or continuous
integration system.
methods
function p = ExampleCustomPlugin(stream)
if ~nargin
stream = matlab.unittest.plugins.ToStandardOutput;
end
validateattributes(stream,...
{'matlab.unittest.plugins.OutputStream'},{})
p.Stream = stream;
end
end
methods (Access=protected)
function runTestSuite(plugin,pluginData)
plugin.Stream.print('\n--- NEW TEST SESSION at %s ---\n',...
char(datetime))
runTestSuite@...
34-140
Plugin to Generate Custom Test Output Format
matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);
end
function reportFinalizedResult(plugin,pluginData)
thisResult = pluginData.TestResult;
if thisResult.Passed
status = 'PASSED';
elseif thisResult.Failed
status = 'FAILED';
elseif thisResult.Incomplete
status = 'SKIPPED';
end
plugin.Stream.print(...
'### YPS Company - Test %s ### - %s in %f seconds.\n',...
status,thisResult.Name,thisResult.Duration)
reportFinalizedResult@...
matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData)
end
end
end
In your working folder, create the file ExampleTest.m containing the following test
class. In this test class, two of the tests pass and the others result in a verification or
assumption failure.
34-141
34 Unit Testing
end
end
At the command prompt, create a test suite from the ExampleTest class, and create a
test runner.
import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
suite = TestSuite.fromClass(?ExampleTest);
runner = TestRunner.withNoPlugins;
Create an instance of ExampleCustomPlugin and add it to the test runner. Run the
tests.
import matlab.unittest.plugins.ToFile
fname = 'YPS_test_results.txt';
p = ExampleCustomPlugin(ToFile(fname));
runner.addPlugin(p)
result = runner.run(suite);
type(fname)
Rerun the Incomplete tests using the same test runner. View the contents of the output
file.
suiteFiltered = suite([result.Incomplete]);
result2 = runner.run(suiteFiltered);
type(fname)
34-142
See Also
See Also
ToFile | ToStandardOutput | matlab.unittest.plugins.OutputStream |
matlab.unittest.plugins.TestRunnerPlugin
Related Examples
• “Write Plugins to Extend TestRunner” on page 34-113
• “Write Plugin to Save Diagnostic Details” on page 34-135
34-143
34 Unit Testing
Create the following function that solves roots of the quadratic equation in a file,
quadraticSolver.m, in your working folder.
type quadraticSolver.m
end
Create the following test class in a file, SolverTest.m, in your working folder.
type SolverTest.m
methods (Test)
function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution);
end
function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
34-144
Analyze Test Case Results
end
Running SolverTest
..
Done SolverTest
__________
ans =
TestResult with properties:
Name: 'SolverTest/testRealSolution'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.6790
34-145
34 Unit Testing
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.67896 seconds testing time.
To access functionality available to tables, create one from the TestResult object.
rt = table(result)
rt=2×6 table
Name Passed Failed Incomplete Duration
____________________________________ ______ ______ __________ ________
sortrows(rt,'Duration')
ans=2×6 table
Name Passed Failed Incomplete Duration
____________________________________ ______ ______ __________ ________
writetable(rt,'myTestResults.csv','QuoteStrings',true)
See Also
Related Examples
• “Write Simple Test Case Using Classes” on page 34-51
34-146
Analyze Failed Test Results
Using the SolverTest test case, add a method, testBadRealSolution. This test,
based on testRealSolution, calls the quadraticSolver function with inputs 1,3,2,
but tests the results against an incorrect solution, [2,1].
function testBadRealSolution(testCase)
actSolution = quadraticSolver(1,3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution)
end
Save the updated SolverTest class definition and rerun the tests.
quadTests = matlab.unittest.TestSuite.fromClass(?SolverTest);
result1 = run(quadTests);
Running SolverTest
..
================================================================================
Verification failed in SolverTest/testBadRealSolution.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The values are not equal using "isequaln".
--> Failure table:
Index Actual Expected Error RelativeError
_____ ______ ________ _____ _____________
1 -1 2 -3 -1.5
2 -2 1 -3 -3
Actual Value:
-1 -2
Expected Value:
2 1
34-147
34 Unit Testing
------------------
Stack Information:
------------------
In C:\work\SolverTest.m (SolverTest.testBadRealSolution) at 19
================================================================================
.
Done SolverTest
__________
Failure Summary:
Analyze Results
Actual Value:
-1 -2
Expected Value:
2 1
At this point, you must decide if the error is in quadraticSolver or in your value for
expSolution.
Correct Error
Rerun Tests
failedTests = quadTests([result1.Failed]);
result2 = run(failedTests)
Running SolverTest
.
Done SolverTest
34-148
See Also
__________
result2 =
Name: 'SolverTest/testBadRealSolution'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.0108
Details: [1x1 struct]
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.010813 seconds testing time.
Alternatively, you can rerun failed tests using the (rerun) link in the test results.
See Also
More About
• “Rerun Failed Tests” on page 34-150
34-149
34 Unit Testing
Totals:
1 Passed, 1 Failed (rerun), 0 Incomplete.
0.25382 seconds testing time.
This link allows you to modify your test code or your code under test and quickly rerun
failed tests. However, if you make structural changes to your test class, using the rerun
link does not pick up the changes. Structural changes include adding, deleting, or
renaming a test method, and modifying a test parameter property and its value. In this
case, recreate the entire test suite to pick up the changes.
Create the following function in your current working folder. The function is meant to
compute the square and square root. However, in this example, the function computes the
cube of the value instead of the square.
function testSquare(testCase)
[sqrVal,sqrRootVal] = exampleFunction(3);
verifyEqual(testCase,sqrVal,9);
end
function testSquareRoot(testCase)
[sqrVal,sqrRootVal] = exampleFunction(100);
verifyEqual(testCase,sqrRootVal,10);
end
34-150
Rerun Failed Tests
Create a test suite and run the tests. The testSquare test fails because the
implementation of exampleFunction is incorrect.
suite = testsuite('ExampleTest.m');
results = run(suite)
Running exampleTest
================================================================================
Verification failed in exampleTest/testSquare.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________ _____ _____________
27 9 18 2
Actual Value:
27
Expected Value:
9
------------------
Stack Information:
------------------
In C:\Work\exampleTest.m (testSquare) at 7
================================================================================
..
Done exampleTest
__________
Failure Summary:
results =
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
1 Passed, 1 Failed (rerun), 0 Incomplete.
0.24851 seconds testing time.
34-151
34 Unit Testing
x = n^2; % square
y = sqrt(n); % square root
end
Click the (rerun) link in the command window to rerun the failed test. You cannot rerun
failed tests if the variable that stores the test results is overwritten. If the link is no longer
in the Command Window, you can type results at the prompt to view it.
Running exampleTest
.
Done exampleTest
__________
ans =
Name: 'exampleTest/testSquare'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.0034
Details: [1×1 struct]
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.0033903 seconds testing time.
MATLAB stores the TestResult array associated with tests that you rerun in the ans
variable. results is a 1x2 array that contains all the tests in exampleTest.m, and ans
is a 1x1 array that contains the rerun results from the one failed test.
whos
Name Size Bytes Class Attributes
To programmatically rerun failed tests, use the Failed property on the TestResult
object to create and run a filtered test suite.
34-152
See Also
failedTests = suite([results.Failed]);
result2 = run(failedTests);
Running exampleTest
.
Done exampleTest
__________
To ensure that all passing tests continue to pass, rerun the full test suite.
See Also
More About
• “Analyze Failed Test Results” on page 34-147
34-153
34 Unit Testing
Since filtering test content through the use of assumptions does not produce test failures,
it has the possibility of creating dead test code. Avoiding this requires monitoring of
filtered tests.
Test Methods
If an assumption failure is encountered inside of a TestCase method with the Test
attribute, the entire method is marked as filtered, but MATLAB runs the subsequent Test
methods.
The following class contains an assumption failure in one of the methods in the Test
block.
Since the testB method contains an assumption failure, when you run the test, the
testing framework filters that test and marks it as incomplete. After the assumption
34-154
Dynamically Filtered Tests
failure in testB, the testing framework proceeds and executes testC, which contains a
verification failure.
ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest
.
================================================================================
ExampleTest/testB was filtered.
Details
================================================================================
.
================================================================================
Verification failed in ExampleTest/testC.
---------------------
Framework Diagnostic:
---------------------
verifyFalse failed.
--> The value must evaluate to "false".
Actual logical:
1
------------------
Stack Information:
------------------
In C:\work\ExampleTest.m (ExampleTest.testC) at 11
================================================================================
.
Done ExampleTest
__________
Failure Summary:
If you examine the TestResult, you notice that there is a passed test, a failed test, and a
test that did not complete due to an assumption failure.
34-155
34 Unit Testing
res
res =
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
1 Passed, 1 Failed, 1 Incomplete.
2.4807 seconds testing time.
The testing framework keeps track of incomplete tests so that you can monitor filtered
tests for nonexercised test code. You can see information about these tests within the
TestResult object.
res([res.Incomplete])
ans =
Name: 'ExampleTest/testB'
Passed: 0
Failed: 0
Incomplete: 1
Duration: 2.2578
Details: [1×1 struct]
Totals:
0 Passed, 0 Failed, 1 Incomplete.
2.2578 seconds testing time.
To create a modified test suite from only the filtered tests, select incomplete tests from
the original test suite.
tsFiltered = ts([res.Incomplete])
tsFiltered =
34-156
Dynamically Filtered Tests
Tests Include:
0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
methods(TestMethodSetup)
function setupMethod1(testCase)
testCase.assumeEqual(1,0)
% remaining test code is not exercised
end
function setupMethod2(testCase)
disp('* Running setupMethod2 *')
testCase.assertEqual(1,1)
end
end
34-157
34 Unit Testing
methods(Test)
function testA(testCase)
testCase.verifyTrue(true)
end
function testB(testCase)
testCase.assumeEqual(0,1)
% remaining test code is not exercised
end
function testC(testCase)
testCase.verifyFalse(true)
end
end
end
When you run the test, you see that the framework completes executes all the methods in
the TestMethodSetup block that do not contain the assumption failure, and it marks as
incomplete all methods in the Test block.
ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest
================================================================================
ExampleTest/testA was filtered.
Details
================================================================================
* Running setupMethod2 *
.
================================================================================
ExampleTest/testB was filtered.
Details
================================================================================
* Running setupMethod2 *
.
================================================================================
ExampleTest/testC was filtered.
Details
================================================================================
* Running setupMethod2 *
34-158
Dynamically Filtered Tests
.
Done ExampleTest
__________
Failure Summary:
The Test methods did not change but all 3 are filtered due to an assumption failure in the
TestMethodSetup block. The testing framework executes methods in the
TestMethodSetup block without assumption failures, such as setupMethod2. As
expected, the testing framework executes setupMethod2 3 times, once before each
Test method.
methods(TestClassSetup)
function setupClass(testCase)
testCase.assumeEqual(1,0)
% remaining test code is not exercised
end
end
34-159
34 Unit Testing
end
end
methods(TestMethodSetup)
function setupMethod1(testCase)
testCase.assumeEqual(1,0)
% remaining test code is not exercised
end
function setupMethod2(testCase)
disp('* Running setupMethod2 *')
testCase.assertEqual(1,1)
end
end
methods(Test)
function testA(testCase)
testCase.verifyTrue(true)
end
function testB(testCase)
testCase.assumeEqual(0,1)
% remaining test code is not exercised
end
function testC(testCase)
testCase.verifyFalse(true)
end
end
end
When you run the test, you see that the framework does not execute any of the methods
in the TestMethodSetup or Test.
ts = matlab.unittest.TestSuite.fromClass(?ExampleTest);
res = ts.run;
Running ExampleTest
================================================================================
All tests in ExampleTest were filtered.
Details
================================================================================
Done ExampleTest
__________
Failure Summary:
34-160
See Also
The Test and TestMethodSetup methods did not change but everything is filtered due
to an assumption failure in the TestClassSetup block.
See Also
TestCase | TestResult | matlab.unittest.qualifications.Assumable
34-161
34 Unit Testing
In a file in your current folder, create a class named HasSameSizeAs by deriving from
the matlab.unittest.constraints.Constraint class. The class constructor accepts
a value with an expected size that is compared to the actual size. This value is stored in
the ValueWithExpectedSize property. Because it is recommended that Constraint
implementations be immutable, set the property SetAccess attribute to immutable.
classdef HasSameSizeAs < matlab.unittest.constraints.Constraint
properties(SetAccess = immutable)
ValueWithExpectedSize
end
methods
function constraint = HasSameSizeAs(value)
constraint.ValueWithExpectedSize = value;
end
end
end
Within a methods block, implement satisfiedBy by invoking the helper method. If the
actual size and the expected size are equal, the method returns true.
methods
function bool = satisfiedBy(constraint,actual)
34-162
Create Custom Constraint
bool = constraint.sizeMatchesExpected(actual);
end
end
methods
function diag = getDiagnosticFor(constraint,actual)
import matlab.unittest.diagnostics.StringDiagnostic
if constraint.sizeMatchesExpected(actual)
diag = StringDiagnostic('HasSameSizeAs passed.');
else
diag = StringDiagnostic(sprintf(...
'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
int2str(size(actual)),...
int2str(size(constraint.ValueWithExpectedSize))));
end
end
end
properties(SetAccess = immutable)
ValueWithExpectedSize
end
methods
function constraint = HasSameSizeAs(value)
constraint.ValueWithExpectedSize = value;
end
34-163
34 Unit Testing
int2str(size(actual)),...
int2str(size(constraint.ValueWithExpectedSize))));
end
end
end
methods(Access = private)
function bool = sizeMatchesExpected(constraint,actual)
bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
end
end
end
import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;
testCase.verifyThat(zeros(5),HasSameSizeAs(repmat(1,5)))
Verification passed.
testCase.verifyThat(zeros(5),HasSameSizeAs(ones(1,5)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
HasSameSizeAs failed.
Actual Size: [5 5]
ExpectedSize: [1 5]
See Also
getDiagnosticFor | matlab.unittest.constraints.Constraint | satisfiedBy
Related Examples
• “Create Custom Boolean Constraint” on page 34-165
34-164
Create Custom Boolean Constraint
In a file in your current folder, create a class named HasSameSizeAs by deriving from
the matlab.unittest.constraints.BooleanConstraint class. The class
constructor accepts a value with an expected size that is compared to the actual size. This
value is stored in the ValueWithExpectedSize property. Because it is recommended
that BooleanConstraint implementations be immutable, set the property SetAccess
attribute to immutable.
classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint
properties(SetAccess = immutable)
ValueWithExpectedSize
end
methods
function constraint = HasSameSizeAs(value)
constraint.ValueWithExpectedSize = value;
end
end
end
34-165
34 Unit Testing
bool = constraint.sizeMatchesExpected(actual);
end
In exchange for implementing the required methods, the constraint inherits the
appropriate and, or, and not overloads, so it can be combined with other
BooleanConstraint objects or negated.
properties(SetAccess = immutable)
ValueWithExpectedSize
end
34-166
Create Custom Boolean Constraint
methods
function constraint = HasSameSizeAs(value)
constraint.ValueWithExpectedSize = value;
end
if constraint.sizeMatchesExpected(actual)
diag = StringDiagnostic('HasSameSizeAs passed.');
else
diag = StringDiagnostic(sprintf(...
'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
int2str(size(actual)),...
int2str(size(constraint.ValueWithExpectedSize))));
end
end
end
methods(Access = protected)
function diag = getNegativeDiagnosticFor(constraint,actual)
import matlab.unittest.diagnostics.StringDiagnostic
if constraint.sizeMatchesExpected(actual)
diag = StringDiagnostic(sprintf(...
['Negated HasSameSizeAs failed.\nSize [%s] of '...
'Actual Value and Expected Value were the same '...
'but should not have been.'],int2str(size(actual))));
else
diag = StringDiagnostic('Negated HasSameSizeAs passed.');
end
end
end
methods(Access = private)
function bool = sizeMatchesExpected(constraint,actual)
bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
end
end
end
34-167
34 Unit Testing
Verification passed.
Verification failed.
---------------------
Framework Diagnostic:
---------------------
AndConstraint failed.
--> + [First Condition]:
| HasLength passed.
|
| Actual Value:
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| Expected Length:
| 5
--> AND
+ [Second Condition]:
| Negated HasSameSizeAs failed.
| Size [5 5] of Actual Value and Expected Value were the same but should no
-+---------------------
See Also
getDiagnosticFor | getNegativeDiagnosticFor |
matlab.unittest.constraints.BooleanConstraint |
matlab.unittest.constraints.Constraint | satisfiedBy
Related Examples
• “Create Custom Constraint” on page 34-162
34-168
Create Custom Tolerance
In a file, DNA.m, in your working folder, create a simple class for a DNA sequence.
classdef DNA
properties(SetAccess=immutable)
Sequence
end
methods
function dna = DNA(sequence)
validLetters = ...
sequence == 'A' | ...
sequence == 'C' | ...
sequence == 'T' | ...
sequence == 'G';
if ~all(validLetters(:))
error('Sequence contained a letter not found in DNA.')
end
dna.Sequence = sequence;
end
end
end
In a file in your working folder, create a tolerance class so that you can test that DNA
sequences are within a specified Hamming distance. The constructor requires a Value
property that defines the maximum Hamming distance.
methods
function tolerance = HammingDistance(value)
tolerance.Value = value;
end
34-169
34 Unit Testing
end
end
In a methods block with the HammingDistance class definition, include the following
method so that the tolerance supports DNA objects. Tolerance classes must implement a
supports method.
methods
function tf = supports(~, value)
tf = isa(value, 'DNA');
end
end
In a methods block with the HammingDistance class definition, include the following
method that returns true or false. Tolerance classes must implement a satisfiedBy
method. The testing framework uses this method to determine if two values are within the
tolerance.
methods
function tf = satisfiedBy(tolerance, actual, expected)
if ~isSameSize(actual.Sequence, expected.Sequence)
tf = false;
return
end
tf = hammingDistance(actual.Sequence,expected.Sequence) <= tolerance.Value;
end
end
In the HammingDistance.m file, define the following helper functions outside of the
classdef block. The isSameSize function returns true if two DNA sequences are the
same size, and the hammingDistance function returns the Hamming distance between
two sequences.
The function returns a Diagnostic object with information about the comparison. In a
methods block with the HammingDistance class definition, include the following method
34-170
Create Custom Tolerance
if ~isSameSize(actual.Sequence, expected.Sequence)
str = 'The DNA sequences must be the same length.';
else
str = sprintf('%s%d.\n%s%d.', ...
'The DNA sequences have a Hamming distance of ', ...
hammingDistance(actual.Sequence, expected.Sequence), ...
'The allowable distance is ', ...
tolerance.Value);
end
diag = StringDiagnostic(str);
end
end
methods
function tolerance = HammingDistance(value)
tolerance.Value = value;
end
34-171
34 Unit Testing
import matlab.unittest.diagnostics.StringDiagnostic
if ~isSameSize(actual.Sequence, expected.Sequence)
str = 'The DNA sequences must be the same length.';
else
str = sprintf('%s%d.\n%s%d.', ...
'The DNA sequences have a Hamming distance of ', ...
hammingDistance(actual.Sequence, expected.Sequence), ...
'The allowable distance is ', ...
tolerance.Value);
end
diag = StringDiagnostic(str);
end
end
end
import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
testCase = TestCase.forInteractiveUse;
sampleA = DNA('ACCTGAGTA');
sampleB = DNA('ACCACAGTA');
testCase.verifyThat(sampleA, IsEqualTo(sampleB))
---------------------
Framework Diagnostic:
---------------------
34-172
Create Custom Tolerance
IsEqualTo failed.
--> ObjectComparator failed.
--> The objects are not equal using "isequal".
Actual Object:
DNA with properties:
Sequence: 'ACCTGAGTA'
Expected Object:
DNA with properties:
Sequence: 'ACCACAGTA'
Verify that the DNA sequences are equal to each other within a Hamming distance of 1.
testCase.verifyThat(sampleA, IsEqualTo(sampleB,...
'Within', HammingDistance(1)))
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> ObjectComparator failed.
--> The objects are not equal using "isequal".
--> The DNA sequences have a Hamming distance of 2.
The allowable distance is 1.
Actual Object:
DNA with properties:
Sequence: 'ACCTGAGTA'
Expected Object:
DNA with properties:
Sequence: 'ACCACAGTA'
The sequences are not equal to each other within a tolerance of 1. The testing framework
displays additional diagnostics from the getDiagnosticFor method.
Verify that the DNA sequences are equal to each other within a Hamming distance of 2.
testCase.verifyThat(sampleA, IsEqualTo(sampleB,...
'Within', HammingDistance(2)))
34-173
34 Unit Testing
See Also
matlab.unittest.constraints.Tolerance
34-174
Overview of App Testing Framework
App Testing
Test Creation – Class-based tests can use the app testing framework by subclassing
matlab.uitest.TestCase. Because matlab.uitest.TestCase is a subclass of
matlab.unittest.TestCase, your test has access to the features of the unit testing
framework, such as qualifications, fixtures, and plugins. To experiment with the app
testing framework at the command prompt, create a test case instance using
matlab.uitest.TestCase.forInteractiveUse.
Test Clean Up – It is a best practice to include a tear-down action to delete the app after
the test. Typically, the test method adds this action using the addTeardown method of
matlab.unittest.TestCase.
App Locking – When an app test creates a figure, the framework locks the figure
immediately to prevent external interactions with the components. The app testing
framework does not lock UI components if you create an instance of
matlab.uitest.TestCase.forInteractiveUse for experimentation at the command
prompt.
34-175
34 Unit Testing
34-176
Overview of App Testing Framework
To explore the properties of this app prior to testing, create an instance of the app at the
command prompt.
app = ConfigurePlotAppExample;
This step is not necessary for the tests, but it is helpful to explore the properties used by
the app tests. For example, use app.UpdatePlotButton to access the Update Plot
button within the app object.
methods (Test)
end
end
Create a test method test_SampleSize to test the sample size. The test method
modifies the sample size, updates the plot, and verifies that the surface uses the specified
sample size. The call to addTeardown deletes the app after the test is complete.
classdef testConfigurePlotAppExample < matlab.uitest.TestCase
methods (Test)
function test_SampleSize(testCase)
app = ConfigurePlotAppExample;
testCase.addTeardown(@delete,app);
testCase.type(app.SampleSizeEditField,12);
testCase.press(app.UpdatePlotButton);
ax = app.UIAxes;
surfaceObj = ax.Children;
testCase.verifySize(surfaceObj.ZData,[12 12]);
end
end
34-177
34 Unit Testing
end
Create a second test method test_Colormap to test the colormap. The test method
selects a colormap, updates the plot, and verifies that the plot uses the specified
colormap. The full code is now as follows.
classdef testConfigurePlotAppExample < matlab.uitest.TestCase
methods (Test)
function test_SampleSize(testCase)
app = ConfigurePlotAppExample;
testCase.addTeardown(@delete,app);
testCase.type(app.SampleSizeEditField,12);
testCase.press(app.UpdatePlotButton);
ax = app.UIAxes;
surfaceObj = ax.Children;
testCase.verifySize(surfaceObj.ZData,[12 12]);
end
function test_Colormap(testCase)
app = ConfigurePlotAppExample;
testCase.addTeardown(@delete,app);
testCase.choose(app.ColormapDropDown,'Winter');
testCase.press(app.UpdatePlotButton);
expectedMap = winter;
ax = app.UIAxes;
testCase.verifyEqual(ax.Colormap,expectedMap);
end
end
end
Running testConfigurePlotAppExample
..
Done testConfigurePlotAppExample
__________
34-178
See Also
results =
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 0 Failed, 0 Incomplete.
4.7551 seconds testing time.
See Also
matlab.uitest.TestCase
More About
• “Write Test for App” on page 34-180
• “Write Test That Uses App Testing and Mocking Frameworks” on page 34-185
• “App Designer Components”
34-179
34 Unit Testing
To explore the properties of this app prior to testing, create an instance of the app at the
command prompt.
app = PatientsDisplay;
This step is not necessary for the tests, but it is helpful to explore the properties used by
the app tests. For example, use app.BloodPressureSwitch to access the Blood
Pressure switch within the app object.
Create a test class that inherits from matlab.uitest.TestCase. To test the tab
switching functionality, create a test method test_tab. The test method chooses the
Data tab and then verifies that the selected tab has the correct title. The
TestMethodSetup method creates an app for each test and deletes it after the test is
complete.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties
App
end
methods (TestMethodSetup)
function launchApp(testCase)
testCase.App = PatientsDisplay;
testCase.addTeardown(@delete,testCase.App);
end
end
methods (Test)
function test_tab(testCase)
% Choose Data Tab
dataTab = testCase.App.Tab2;
testCase.choose(dataTab)
end
end
Create a test_plottingOptions method that tests various plotting options. The test
method presses the Histogram radio button and verifies that the x-label changed. Then,
it changes the Bin Width slider and verifies the number of bins.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties
34-180
Write Test for App
App
end
methods (TestMethodSetup)
function launchApp(testCase)
testCase.App = PatientsDisplay;
testCase.addTeardown(@delete,testCase.App);
end
end
methods (Test)
function test_plottingOptions(testCase)
% Press the histogram radio button
testCase.press(testCase.App.HistogramButton)
end
end
Create a test_bloodPressure method that tests blood pressure data and display. The
test method verifies the y-axis label and the values of the scatter points. Then it changes
to Diastolic readings, and verifies the label and data again.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties
App
end
methods (TestMethodSetup)
function launchApp(testCase)
testCase.App = PatientsDisplay;
testCase.addTeardown(@delete,testCase.App);
end
end
methods (Test)
function test_bloodPressure(testCase)
% Extract blood pressure data from app
t = cell2table(testCase.App.Tab2.Children.Data);
t.Var2 = categorical(t.Var2);
allMales = t(t.Var2=='Male',:);
maleDiastolicData = allMales.Var7';
maleSystolicData = allMales.Var8';
34-181
34 Unit Testing
testCase.choose(testCase.App.BloodPressureSwitch,'Diastolic')
end
end
Create a test_gender method that tests gender data and display. The test method
verifies the number of male scatter points and then presses the check box to include
female data. It verifies that two data sets are plotted and the color of the female data is
red. Finally, it clears the male data check box and verifies the number of plotted data sets
and scatter points. This test fails because there are 53 female scatter points instead of 50.
To take a screen shot when the test fails, use a ScreenshotDiagnostic with the
onFailure method.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties
App
end
methods (TestMethodSetup)
function launchApp(testCase)
testCase.App = PatientsDisplay;
testCase.addTeardown(@delete,testCase.App);
end
end
methods (Test)
function test_gender(testCase)
import matlab.unittest.diagnostics.ScreenshotDiagnostic
testCase.onFailure(ScreenshotDiagnostic);
% Verify two data sets display and the female data is red
testCase.assertNumElements(ax.Children,2);
testCase.verifyEqual(ax.Children(1).CData,[1 0 0]);
function test_bloodPressure(testCase)
34-182
Write Test for App
function test_plottingOptions(testCase)
% Press the histogram radio button
testCase.press(testCase.App.HistogramButton)
function test_tab(testCase)
% Choose Data Tab
dataTab = testCase.App.Tab2;
testCase.choose(dataTab)
end
end
results = runtests('TestPatientsDisplay');
Running TestPatientsDisplay
================================================================================
Verification failed in TestPatientsDisplay/test_gender.
---------------------
Framework Diagnostic:
---------------------
verifyNumElements failed.
--> The value did not have the correct number of elements.
34-183
34 Unit Testing
Actual Value:
Columns 1 through 49
131 133 119 142 142 132 128 137 129 131 133 117 137 146 123 143 114
Columns 50 through 53
----------------------
Additional Diagnostic:
----------------------
Screenshot captured to:
--> C:\Temp\54fd8dc0-0637-4926-9c4f-f217fe195fe1\Screenshot_daba8870-adb3-4a1c-ba11-df3d9b51d36f.png
------------------
Stack Information:
------------------
In C:\Work\TestPatientsDisplay.m (TestPatientsDisplay.test_gender) at 34
================================================================================
....
Done TestPatientsDisplay
__________
Failure Summary:
See Also
matlab.uitest.TestCase
More About
• “Overview of App Testing Framework” on page 34-175
• “Write Test That Uses App Testing and Mocking Frameworks” on page 34-185
34-184
Write Test That Uses App Testing and Mocking Frameworks
Create App
Create the launchApp app in your current working folder. The app allows a user to select
an input file and displays the name of the file in the app. The file selection dialog box is a
blocking modal dialog box that waits for user input.
To explore the properties of this app prior to testing, create an instance of the app at the
command prompt. This step is not necessary for the tests, but it is helpful to explore the
properties used by the app tests. For example, use app.Button to access the Input file
button within the app object.
app = launchApp;
34-185
34 Unit Testing
34-186
Write Test That Uses App Testing and Mocking Frameworks
methods(TestClassSetup)
function checkTestFiles(tc)
import matlab.unittest.constraints.IsFile
tc.assumeThat(tc.TestFile,IsFile)
end
end
methods (Test)
function testInputButton(tc)
app = launchApp;
tc.addTeardown(@close,app.UIFigure);
tc.press(app.Button);
tc.verifyEqual(app.Label.Text,tc.TestFile)
end
end
end
Run the test. When the file selection dialog box appears, select input2.txt to allow
MATLAB to proceed with the test. Selecting any other file results in a test failure.
results = runtests('LaunchAppTest');
Running LaunchAppTest
.
Done LaunchAppTest
__________
Create a FileChooser service with an Abstract method that implements the file
selection functionality.
classdef FileChooser
% Interface to choose a file
methods (Abstract)
[file,folder,status] = chooseFile(chooser,varargin)
end
end
34-187
34 Unit Testing
Create a default FileChooser that uses the uigetfile function for file selection.
Change the app to accept an optional FileChooser object. When called with no inputs,
the app uses an instance of DefaultFileChooser.
34-188
Write Test That Uses App Testing and Mocking Frameworks
[mockChooser,behavior] = tc.createMock(?FileChooser);
when(behavior.chooseFile('*.*'),AssignOutputs(fname,pwd,1))
app = launchApp(mockChooser);
tc.addTeardown(@close,app.UIFigure);
tc.press(app.Button);
tc.verifyEqual(app.Label.Text,fname);
end
function testInputButton_Cancel(tc)
import matlab.mock.actions.AssignOutputs
app = launchApp(mockChooser);
tc.addTeardown(@close,app.UIFigure);
tc.press(app.Button);
34-189
34 Unit Testing
tc.verifyCalled(behavior.chooseFile('*.*'));
tc.verifyEqual(app.Label.Text,'No file selected');
end
end
end
Run the tests. The tests run to completion without manual file selection.
results = runtests('LaunchAppTest');
Running LaunchAppTest
..
Done LaunchAppTest
__________
See Also
matlab.mock.TestCase | matlab.uitest.TestCase
More About
• “Overview of App Testing Framework” on page 34-175
• “Write Test for App” on page 34-180
• “Create Mock Object” on page 34-214
34-190
Overview of Performance Testing Framework
The performance test interface leverages the script, function, and class-based unit testing
interfaces. You can perform qualifications within your performance tests to ensure correct
functional behavior while measuring code performance. Also, you can run your
performance tests as standard regression tests to ensure that code changes do not break
performance tests.
34-191
34 Unit Testing
34-192
Overview of Performance Testing Framework
This table summarizes the differences between the frequentist and fixed time
experiments.
34-193
34 Unit Testing
• Use the runperf function to run the tests. This function uses a variable number of
measurements to reach a sample mean with a 0.05 relative margin of error within a
0.95 confidence level. It runs the tests four times to warm up the code and between 4
and 256 times to collect measurements that meet the statistical objectives.
• Generate an explicit test suite using the testsuite function or the methods in the
TestSuite class, and then create and run a time experiment.
You can run your performance tests as regression tests. For more information, see “Run
Tests for Various Workflows” on page 34-105.
When the performance testing framework encounters an invalid test result, it behaves
differently depending on the type of time experiment:
• If you create a frequentist time experiment, then the framework stops measuring for
that test and moves to the next test.
• If you create a fixed time experiment, then the framework continues collecting the
specified number of samples.
34-194
See Also
See Also
matlab.perftest.TimeExperiment |
matlab.unittest.measurement.MeasurementResult | runperf | testsuite
Related Examples
• “Test Performance Using Scripts or Functions” on page 34-196
• “Test Performance Using Classes” on page 34-202
34-195
34 Unit Testing
34-196
Test Performance Using Scripts or Functions
function testForLoop(testCase)
vectorSize = getSize();
for i=1:vectorSize
x(i) = 1;
end
end
results = runperf('preallocationTest.m')
Running preallocationTest
.......... .......... .......... ..
Done preallocationTest
__________
34-197
34 Unit Testing
results =
Name
Valid
Samples
TestActivity
Totals:
4 Valid, 0 Invalid.
10.2561 seconds testing time.
The results variable is a 1-by-4 TimeResult array. Each element in the array
corresponds to one of the tests defined in the code section in preallocationTest.m.
Display the measurement results for the second test. Your results might vary.
results(2)
ans =
Name: 'preallocationTest/testIndexingWithVariable'
Valid: 1
Samples: [4×7 table]
TestActivity: [8×12 table]
Totals:
1 Valid, 0 Invalid.
1.2274 seconds testing time.
>> results(2).Samples
34-198
Test Performance Using Scripts or Functions
ans =
4×7 table
Display the mean measured time for the second test. To exclude data collected in the
warm-up runs, use the values in the Samples field.
sampleTimes = results(2).Samples.MeasuredTime;
meanTest2 = mean(sampleTimes)
meanTest2 =
0.1534
The performance testing framework collected four sample measurements for the second
test. The test took an average of 0.1534 second.
Determine the average time for all the test elements. The preallocationTest test
includes four different methods for allocating a vector of ones. Compare the time for each
method (test element).
Since the performance testing framework returns a Samples table for each test element,
concatenate all these tables into one table. Then group the rows by test element Name,
and compute the mean MeasuredTime for each group.
fullTable = vertcat(results.Samples);
summaryStats = varfun(@mean,fullTable,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStats =
4×3 table
34-199
34 Unit Testing
preallocationTest/testOnes 4 0.041072
preallocationTest/testIndexingWithVariable 4 0.1534
preallocationTest/testIndexingOnLHS 4 0.04677
preallocationTest/testForLoop 4 1.0343
Change the statistical objectives defined by the runperf function by constructing and
running a time experiment. Construct a time experiment with measurements that reach a
sample mean with an 8% relative margin of error within a 97% confidence level.
suite = testsuite('preallocationTest');
Construct a time experiment with a variable number of sample measurements, and run
the tests.
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError('NumWarmups',2,...
'RelativeMarginOfError',0.08, 'ConfidenceLevel', 0.97);
resultsTE = run(experiment,suite);
Running preallocationTest
.......... .......... ....
Done preallocationTest
__________
fullTableTE = vertcat(resultsTE.Samples);
summaryStatsTE = varfun(@mean,fullTableTE,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStatsTE =
4×3 table
preallocationTest/testOnes 4 0.040484
34-200
See Also
preallocationTest/testIndexingWithVariable 4 0.15187
preallocationTest/testIndexingOnLHS 4 0.046224
preallocationTest/testForLoop 4 1.0262
See Also
matlab.perftest.TimeExperiment | matlab.perftest.TimeResult |
matlab.unittest.measurement.DefaultMeasurementResult | runperf |
testsuite
34-201
34 Unit Testing
Consider the following unit (regression) test. You can run this test as a performance test
using runperf('fprintfTest') instead of runtests('fprintfTest').
testCase.addTeardown(@delete,testCase.file);
testCase.addTeardown(@fclose,testCase.fid);
end
end
methods(Test)
function testPrintingToFile(testCase)
textToWrite = repmat('abcdef',1,5000000);
fprintf(testCase.fid,'%s',textToWrite);
testCase.verifyEqual(fileread(testCase.file),textToWrite)
end
function testBytesToFile(testCase)
textToWrite = repmat('tests_',1,5000000);
nbytes = fprintf(testCase.fid,'%s',textToWrite);
testCase.verifyEqual(nbytes,length(textToWrite))
end
end
end
The measured time does not include the time to open and close the file or the assertion
because these activities take place inside a TestMethodSetup block, and not inside a
34-202
Test Performance Using Classes
Test block. However, the measured time includes the time to perform the verifications.
Best practice is to measure a more accurate performance boundary.
Create a performance test in a file, fprintfTest.m, in your current working folder. This
test is similar to the regression test with the following modifications:
testCase.addTeardown(@delete,testCase.file);
testCase.addTeardown(@fclose,testCase.fid);
end
end
methods(Test)
function testPrintingToFile(testCase)
textToWrite = repmat('abcdef',1,5000000);
testCase.startMeasuring();
fprintf(testCase.fid,'%s',textToWrite);
testCase.stopMeasuring();
testCase.verifyEqual(fileread(testCase.file),textToWrite)
end
function testBytesToFile(testCase)
textToWrite = repmat('tests_',1,5000000);
testCase.startMeasuring();
nbytes = fprintf(testCase.fid,'%s',textToWrite);
testCase.stopMeasuring();
34-203
34 Unit Testing
testCase.verifyEqual(nbytes,length(textToWrite))
end
end
end
The measured time for this performance test includes only the call to fprintf, and the
testing framework still evaluates the qualifications.
Run the performance test. Depending on your system, you might see warnings that the
performance testing framework ran the test the maximum number of times, but did not
achieve a 0.05 relative margin of error with a 0.95 confidence level.
results = runperf('fprintfTest')
Running fprintfTest
.......... .......... .
Done fprintfTest
__________
results =
Name
Valid
Samples
TestActivity
Totals:
2 Valid, 0 Invalid.
4.1417 seconds testing time.
Display the measurement results for the first test. Your results might vary.
results(1)
34-204
Test Performance Using Classes
ans =
Name: 'fprintfTest/testPrintingToFile'
Valid: 1
Samples: [4×7 table]
TestActivity: [8×12 table]
Totals:
1 Valid, 0 Invalid.
2.7124 seconds testing time.
results(1).Samples
ans =
4×7 table
Display the mean measured time for the first test. To exclude data collected in the warm-
up runs, use the values in the Samples field.
sampleTimes = results(1).Samples.MeasuredTime;
meanTest = mean(sampleTimes)
meanTest =
0.0681
34-205
34 Unit Testing
Determine the average time for all the test elements. The fprintfTest test includes two
different methods. Compare the time for each method (test element).
Since the performance testing framework returns a Samples table for each test element,
concatenate all these tables into one table. Then group the rows by test element Name,
and compute the mean MeasuredTime for each group.
fullTable = vertcat(results.Samples);
summaryStats = varfun(@mean,fullTable,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStats =
2×3 table
fprintfTest/testPrintingToFile 4 0.068139
fprintfTest/testBytesToFile 9 0.071595
Both test methods write the same amount of data to a file. Therefore, some of the
difference between the mean values is attributed to calling the fprintf function with an
output argument.
Change the statistical objectives defined by the runperf function by constructing and
running a time experiment. Construct a time experiment with measurements that reach a
sample mean with a 3% relative margin of error within a 97% confidence level. Collect 4
warm-up measurements and up to 16 sample measurements.
suite = testsuite('fprintfTest');
Construct a time experiment with a variable number of sample measurements, and run
the tests.
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError('NumWarmups',4,...
'MaxSamples',16,'RelativeMarginOfError',0.03,'ConfidenceLevel',0.97);
resultsTE = run(experiment,suite);
34-206
Test Performance Using Classes
Running fprintfTest
.......... ..........Warning: Target Relative Margin of Error not met after running the
........
Done fprintfTest
__________
In this example output, the performance testing framework is not able to meet the stricter
statistical objectives with the specified number of maximum samples. Your results might
vary.
fullTableTE = vertcat(resultsTE.Samples);
summaryStatsTE = varfun(@mean,fullTableTE,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStatsTE =
2×3 table
fprintfTest/testPrintingToFile 16 0.069482
fprintfTest/testBytesToFile 4 0.067902
Increase the maximum number of samples to 32 and rerun the time experiment.
experiment = TimeExperiment.limitingSamplingError('NumWarmups',4,...
'MaxSamples',32,'RelativeMarginOfError',0.03,'ConfidenceLevel',0.97);
resultsTE = run(experiment,suite);
Running fprintfTest
.......... ......
Done fprintfTest
__________
fullTableTE = vertcat(resultsTE.Samples);
summaryStatsTE = varfun(@mean,fullTableTE,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStatsTE =
2×3 table
34-207
34 Unit Testing
fprintfTest/testPrintingToFile 4 0.067228
fprintfTest/testBytesToFile 4 0.067766
The testing framework achieves the statistical objectives for both tests with 4 samples.
Start a new MATLAB session. A new session ensures that MATLAB has not run the code
contained in your tests.
Measure the first-time cost of your code by creating and running a fixed time experiment
with zero warm-up measurements and one sample measurement.
Construct an explicit test suite. Since you are measuring the first-time cost of the
function, run a single test. To run multiple tests, save the results and start a new MATLAB
session between tests.
suite = testsuite('fprintfTest/testPrintingToFile');
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.withFixedSampleSize(1);
results = run(experiment,suite);
Running fprintfTest
.
Done fprintfTest
__________
Display the results. Observe the TestActivity table to ensure there are no warm-up
samples.
fullTable = results.TestActivity
fullTable =
1×12 table
34-208
See Also
The performance testing framework collects one sample for each test.
See Also
matlab.perftest.TestCase | matlab.perftest.TimeExperiment |
matlab.perftest.TimeResult |
matlab.unittest.measurement.DefaultMeasurementResult | runperf |
testsuite
34-209
34 Unit Testing
function testIndexingWithVariable(testCase)
import matlab.unittest.constraints.IsSameSetAs
testCase.startMeasuring
id = 1:1e5;
x(id) = 1;
testCase.stopMeasuring
testCase.verifyThat(x,IsSameSetAs(1))
end
function testIndexingOnLHS(testCase)
import matlab.unittest.constraints.EveryElementOf
import matlab.unittest.constraints.IsEqualTo
testCase.startMeasuring
x(1:1e5) = 1;
testCase.stopMeasuring
testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))
end
function testForLoop(testCase)
testCase.startMeasuring
for i=1:1e5
x(i) = 1;
34-210
Measure Fast Executing Test Code
end
testCase.stopMeasuring
testCase.verifyNumElements(x,1e5)
end
end
end
Run PreallocationTest as a performance test. Two tests are filtered because the
measurements are too close to the precision of the framework.
results = runperf('PreallocationTest');
Running PreallocationTest
........
================================================================================
PreallocationTest/testOnes was filtered.
Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
.. .......... ....
================================================================================
PreallocationTest/testIndexingOnLHS was filtered.
Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework.
Details
================================================================================
...... ..
Done PreallocationTest
__________
Failure Summary:
To instruct the framework to automatically loop through the measured code and average
the measurement results, modify PreallocationTest to use a keepMeasuring-while
loop instead of startMeasuring and stopMeasuring.
classdef PreallocationTest < matlab.perftest.TestCase
methods(Test)
function testOnes(testCase)
while(testCase.keepMeasuring)
x = ones(1,1e5);
end
testCase.verifyEqual(size(x),[1 1e5])
end
function testIndexingWithVariable(testCase)
import matlab.unittest.constraints.IsSameSetAs
34-211
34 Unit Testing
while(testCase.keepMeasuring)
id = 1:1e5;
x(id) = 1;
end
testCase.verifyThat(x,IsSameSetAs(1))
end
function testIndexingOnLHS(testCase)
import matlab.unittest.constraints.EveryElementOf
import matlab.unittest.constraints.IsEqualTo
while(testCase.keepMeasuring)
x(1:1e5) = 1;
end
testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))
end
function testForLoop(testCase)
while(testCase.keepMeasuring)
for i=1:1e5
x(i) = 1;
end
end
testCase.verifyNumElements(x,1e5)
end
end
end
Running PreallocationTest
.......... .......... .......... ..
Done PreallocationTest
__________
4×7 table
34-212
See Also
See Also
keepMeasuring | runperf
Related Examples
• “Overview of Performance Testing Framework” on page 34-191
• “Test Performance Using Classes” on page 34-202
34-213
34 Unit Testing
For example, suppose you want to test an algorithm for buying stock, but you do not want
to test the entire system. You could use a mock object to replace the functionality of
looking up the stock price, and another mock object to verify that the trader purchased
the stock. The algorithm you are testing does not know that it is operating on mock
objects, and you can test the algorithm isolated from the rest of the system.
34-214
Create Mock Object
Using a mock object, you can define behavior (a process known as stubbing). For
example, you can specify that an object produces predefined responses to queries. You
can also intercept and remember messages sent from the component under test to the
mock object (a process known as spying). For example, you can verify that a particular
method was called or a property was set.
Depended on Components
In this example, the component under test is a simple day-trading algorithm. It is the part
of the system you want to test independent of other components. The day-trading
algorithm has two dependencies: a data service to retrieve the stock price data and a
broker to purchase the stock.
In a file DataService.m in your current working folder, create an abstract class that
includes a lookupPrice method.
classdef DataService
methods (Abstract,Static)
price = lookupPrice(ticker,date)
end
end
34-215
34 Unit Testing
In this example, assume that the broker component has not been developed yet. Once it is
implemented, it will have a buy method that accepts a ticker symbol and a specified
number of shares to buy, and returns a status code. The mock for the broker component
uses an implicit interface, and does not derive from a superclass.
In a file trader.m in your current working folder, create a simple day trading algorithm.
The trader function accepts as inputs a data service object that looks up the price of the
stock, a broker object that defines how the stock is bought, a ticker symbol, and a number
of shares to purchase. If the price from yesterday is less than the price two days ago,
instruct the broker to buy the specified number of shares.
function trader(dataService,broker,ticker,numShares)
yesterday = datetime('yesterday');
priceYesterday = dataService.lookupPrice(ticker,yesterday);
price2DaysAgo = dataService.lookupPrice(ticker,yesterday-days(1));
The mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface. The mock object carries out the actions for
the component under test, such as calling a method or accessing a property.
34-216
Create Mock Object
When you create a mock, you also create an associated behavior object. The behavior
object defines the same methods as the mock object and controls mock behavior. Use the
behavior object to define mock actions and qualify interactions. For example, use it to
define values a mocked method returns, or verify that a property was accessed.
At the command prompt, create a mock test case for interactive use. Using the mock in a
test class instead of at the command prompt is presented later in this example.
import matlab.mock.TestCase
testCase = TestCase.forInteractiveUse;
Create a mock for the data service dependency and examine the methods on it. The data
service mock returns predefined values, replacing the implementation of the service that
provides actual stock prices. Therefore, it exhibits stubbing behavior.
[stubDataService,dataServiceBehavior] = createMock(testCase,?DataService);
methods(stubDataService)
Static methods:
lookupPrice
In the DataService class, the lookupPrice method is abstract and static. The mocking
framework implements this method as concrete and static.
Define behavior for the data service mock. For ticker symbol "FOO", it returns the price
yesterday as $123 and anything before yesterday is $234. Therefore, according to the
trader function, the broker always buys stock "FOO". For the ticker symbol "BAR", it
returns the price yesterday as $765 and anything before yesterday is $543. Therefore, the
broker never buys stock "BAR".
import matlab.unittest.constraints.IsLessThan
yesterday = datetime('yesterday');
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"FOO",yesterday),123);
34-217
34 Unit Testing
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"FOO",IsLessThan(yesterday)),234);
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",yesterday),765);
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",IsLessThan(yesterday)),543);
p1 =
123
p2 =
543
Create a mock for the broker dependency and examine the methods on it. Since the
broker mock is used to verify interactions with the component under test (the trader
function), it exhibits spying behavior. The broker mock has an implicit interface. While the
buy method is not currently implemented, you can create a mock with it.
[spyBroker,brokerBehavior] = createMock(testCase,'AddedMethods',{'buy'});
methods(spyBroker)
buy
34-218
Create Mock Object
s1 = spyBroker.buy
s2 = spyBroker.buy("inputs",[13 42])
s1 =
[]
s2 =
[]
Since the trader function does not use the status return code, the default mock behavior
of returning empty is acceptable. The broker mock is a pure spy, and does not need to
implement any stubbing behavior.
Call the trader function. In addition to the ticker symbol and the number of shares to
buy, the trader function takes as inputs the data service and the broker. Instead of
passing in actual data service and broker objects, pass in the spyBroker and
stubDataService mocks.
trader(stubDataService,spyBroker,"FOO",100)
trader(stubDataService,spyBroker,"FOO",75)
trader(stubDataService,spyBroker,"BAR",100)
Use the broker behavior object (the spy) to verify that the trader function calls the buy
method, as expected.
Use the TestCase.verifyCalled method to verify that the trader function instructed
the buy method to buy 100 shares of the FOO stock.
import matlab.mock.constraints.WasCalled;
testCase.verifyCalled(brokerBehavior.buy("FOO",100))
Verify that FOO stock was purchased two times, regardless of the specified number of
shares. While the verifyCalled method is convenient to specify behavior, there is more
34-219
34 Unit Testing
functionality if you use the WasCalled constraint. For example, you can verify that a
mocked method was called a specified number of times.
import matlab.unittest.constraints.IsAnything
testCase.verifyThat(brokerBehavior.buy("FOO",IsAnything), ...
WasCalled('WithCount',2))
Verify that the buy method was not called requesting 100 shares of the BAR stock.
testCase.verifyNotCalled(brokerBehavior.buy("BAR",100))
Although the trader function was called requesting 100 shares of BAR stock, the stub
defined yesterday's price for BAR to return a higher value than all days prior to yesterday.
Therefore, the broker never buys stock "BAR".
The interactive test case is convenient to experiment with at the command prompt.
However, it is typical to create and use mocks within a test class. In a file in your current
working folder, create the following test class that incorporates the interactive testing
from this example.
% Create mocks
[stubDataService,dataServiceBehavior] = createMock(testCase, ...
?DataService);
[spyBroker,brokerBehavior] = createMock(testCase, ...
'AddedMethods',{'buy'});
% Set up behavior
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"FOO",yesterday),123);
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
34-220
Create Mock Object
"FOO",IsLessThan(yesterday)),234);
% Verify interactions
testCase.verifyCalled(brokerBehavior.buy("FOO",100))
testCase.verifyThat(brokerBehavior.buy("FOO",IsAnything), ...
WasCalled('WithCount',2))
end
function doesNotBuyStockWhenIncreases(testCase)
import matlab.unittest.constraints.IsLessThan
yesterday = datetime('yesterday');
% Create mocks
[stubDataService,dataServiceBehavior] = createMock(testCase, ...
?DataService);
[spyBroker,brokerBehavior] = createMock(testCase, ...
'AddedMethods',{'buy'});
% Set up behavior
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",yesterday),765);
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",IsLessThan(yesterday)),543);
% Verify interactions
testCase.verifyNotCalled(brokerBehavior.buy("BAR",100))
end
end
end
results = runtests('TraderTest');
table(results)
Running TraderTest
..
Done TraderTest
34-221
34 Unit Testing
__________
ans =
2×6 table
See Also
34-222
Specify Mock Object Behavior
In this section...
“Define Mock Method Behavior” on page 34-223
“Define Mock Property Behavior” on page 34-225
“Define Repeating and Subsequent Behavior” on page 34-226
“Summary of Behaviors” on page 34-228
When you create a mock, you create an associated behavior object that controls mock
behavior. Use this object to define mock method and property behavior (stub). For more
information on creating a mock, see “Create Mock Object” on page 34-214.
The mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface.
Create a mock with an implicit interface. The interface includes Name and ID properties
and a findUser method that accepts an identifier and returns a name. While
the interface is not currently implemented, you can create a mock with it.
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behaviorObj] = testCase.createMock('AddedProperties', ...
{'Name','ID'},'AddedMethods',{'findUser'});
Specify that when the findUser method is called with any inputs, it returns "Unknown".
By default, MATLAB returns an empty array when you call the findUser method.
• The assignOutputsWhen method defines return values for the method call.
• The mocked method call (behaviorObj.findUser) implicitly creates a
MethodCallBehavior object.
• The withAnyInputs method of the MethodCallBehavior object specifies that the
behavior applies to a method call with any number of inputs with any value.
34-223
34 Unit Testing
testCase.assignOutputsWhen(withAnyInputs(behaviorObj.findUser),"Unknown")
n = mock.findUser(1)
n =
"Unknown"
Specify that when the input value is 1701, the mock method returns "Jim". This behavior
supersedes the return of "Unknown" for the input value of 1701 only because it was
defined after that specification.
testCase.assignOutputsWhen(behaviorObj.findUser(1701),"Jim")
n = mock.findUser(1701)
n =
"Jim"
Specify that when the findUser method is called with only the object as input, the mock
method returns "Unspecified ID". The withExactInputs method of the
MethodCallBehavior object specifies that the behavior applies to a method call with
the object as the only input value.
testCase.assignOutputsWhen(withExactInputs(behaviorObj.findUser), ...
"Unspecified ID")
n = mock.findUser % equivalent to n = findUser(mock)
n =
"Unspecified ID"
import matlab.unittest.constraints.IsGreaterThan
testCase.throwExceptionWhen(behaviorObj.findUser(IsGreaterThan(5000)));
n = mock.findUser(5001)
Error using
matlab.mock.internal.MockContext/createMockObject/mockMethodCallback (line 323)
The following method call was specified to throw an exception:
findUser([1×1 matlab.mock.classes.Mock], 5001)
34-224
Specify Mock Object Behavior
You can define behavior based on the number of outputs requested in a method call. If the
method call requests two output values, return "??" for the name and -1 for the ID.
testCase.assignOutputsWhen(withNargout(2, ...
withAnyInputs(behaviorObj.findUser)),"??",-1)
[n,id] = mock.findUser(13)
n =
"??"
id =
-1
When defining mock property behavior, keep in mind that displaying a property value in
the command window is a property access (get) operation.
Similar to defining mock method behavior, defining mock property behavior requires an
instance of the PropertyBehavior class. The framework returns an instance of this
class when you access a mock property. To define access behavior, use an instance of
PropertyGetBehavior by calling the get method of the PropertyBehavior class. To
define set behavior, use an instance of the PropertySetBehavior by calling the set or
setToValue method of the PropertyBehavior class.
Specify that when the Name property is set to any value, the testing framework throws an
exception.
34-225
34 Unit Testing
testCase.throwExceptionWhen(set(behaviorObj.Name))
mock.Name = "Sue";
Allow the mock to store the value when the property is set to "David".
testCase.storeValueWhen(setToValue(behaviorObj.Name,"David"));
mock.Name = "David"
mock =
Name: "David"
ID: []
Assign the value of 1138 to the ID property and then throw an exception for property
access.
import matlab.mock.actions.AssignOutputs
import matlab.mock.actions.ThrowException
when(get(behaviorObj.ID),then(AssignOutputs(1138),ThrowException))
id = mock.ID
id = mock.ID
id =
1138
34-226
Specify Mock Object Behavior
Assign the value of 1138 and then 237 to the ID property. Then, throw an exception for
property access. Each call to the then method accepts up to two actions. To specify more
subsequent actions, use multiple calls to then.
when(get(behaviorObj.ID),then(AssignOutputs(1138), ...
then(AssignOutputs(237),ThrowException)))
id = mock.ID
id = mock.ID
id = mock.ID
id =
1138
id =
237
If the object is the only input value, specify the findUser function return the value of
"Phil" twice.
when(withExactInputs(behaviorObj.findUser),repeat(2,AssignOutputs("Phil")))
n = mock.findUser
n = mock.findUser
n =
"Phil"
n =
"Phil"
Call the function a third time. If you repeat an action, and do not follow it with a call to
the then method, the mock continues to return the repeated value.
n = mock.findUser
34-227
34 Unit Testing
n =
"Phil"
Define behavior for setting the value of Name. Throw an exception the first two times and
then store the value.
import matlab.mock.actions.StoreValue
when(set(behaviorObj.Name),then(repeat(2,ThrowException),StoreValue))
mock.Name = "John"
mock.Name = "Penny"
mock.Name = "Tommy"
mock =
Name: "Tommy"
Summary of Behaviors
Behavior TestCase Method matlab.mock.Actions
Class (Allows for
Definition of Repeat and
Subsequent Behavior)
Return specified values for assignOutputsWhen AssignOutputs
method call and property
access.
Return stored value when returnStoredValueWhen ReturnStoredValue
property is accessed.
34-228
See Also
See Also
matlab.mock.TestCase | matlab.mock.actions.AssignOutputs |
matlab.mock.actions.ReturnStoredValue | matlab.mock.actions.StoreValue
| matlab.mock.actions.ThrowException
Related Examples
• “Create Mock Object” on page 34-214
34-229
34 Unit Testing
In the mocking framework, qualifications are functions used to test interactions with the
object. There are four types of qualifications:
The mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface. Create a mock with an implicit interface
for a dice class. The interface includes Color and NumSides properties and a roll
method that accepts a number of dice and returns a value. While the interface is not
currently implemented, you can create a mock with it.
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behaviorObj] = testCase.createMock('AddedProperties', ...
{'NumSides','Color'},'AddedMethods',{'roll'});
34-230
Qualify Mock Object Interaction
val = mock.roll(1);
testCase.verifyCalled(behaviorObj.roll(1))
Verify that the roll method was called with 3 dice. This test fails.
testCase.verifyCalled(behaviorObj.roll(3), ...
'roll method should have been called with input 3.')
----------------
Test Diagnostic:
----------------
roll method should have been called with input 3.
---------------------
Framework Diagnostic:
---------------------
verifyCalled failed.
--> Method 'roll' was not called with the specified signature.
--> Observed method call(s) with any signature:
out = roll([1×1 matlab.mock.classes.Mock], 1)
Verify that the roll method was not called with 2 dice.
testCase.verifyNotCalled(behaviorObj.roll(2))
34-231
34 Unit Testing
testCase.verifyCalled(withAnyInputs(behaviorObj.roll))
Verify that the roll method was not called with 2 outputs and any inputs.
testCase.verifyNotCalled(withNargout(2,withAnyInputs(behaviorObj.roll)))
mock.Color = "red"
mock =
NumSides: []
Color: "red"
testCase.verifySet(behaviorObj.Color)
Verify the color was accessed. This test passes because there is an implicit property
access when MATLAB displays the object.
testCase.verifyAccessed(behaviorObj.Color)
testCase.assertNotSet(behaviorObj.NumSides)
34-232
Qualify Mock Object Interaction
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behaviorObj] = testCase.createMock('AddedProperties', ...
{'NumSides','Color'},'AddedMethods',{'roll'});
Roll 2 dice. Then use a constraint to verify that the roll method was called at least once
with two dice.
val = mock.roll(2);
import matlab.mock.constraints.WasCalled
testCase.verifyThat(behaviorObj.roll(2),WasCalled)
Roll one die. Then verify that the roll method was called at least twice with any inputs.
val = mock.roll(1);
testCase.verifyThat(withAnyInputs(behaviorObj.roll), ...
WasCalled('WithCount',2))
import matlab.mock.constraints.WasAccessed
testCase.verifyThat(behaviorObj.NumSides,~WasAccessed)
Set the color of the dice. Then verify the property was set once.
34-233
34 Unit Testing
mock.Color = "blue";
import matlab.mock.constraints.WasSet
testCase.verifyThat(behaviorObj.Color,WasSet('WithCount',1))
Access the Color property. Then verify that it was not accessed exactly once. This test
fails.
c = mock.Color
testCase.verifyThat(behaviorObj.Color,~WasAccessed('WithCount',1))
c =
"blue"
---------------------
Framework Diagnostic:
---------------------
Negated WasAccessed failed.
--> Property 'Color' was accessed the prohibited number of times.
Set the number of sides. Then, verify that the number of sides was set to 22.
mock.NumSides = 22;
testCase.verifyThat(behaviorObj.NumSides,WasSet('ToValue',22))
34-234
Qualify Mock Object Interaction
import matlab.unittest.constraints.IsLessThanOrEqualTo
testCase.verifyThat(behaviorObj.NumSides, ...
WasSet('ToValue',IsLessThanOrEqualTo(20)))
---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'NumSides' was not set to the specified value.
--> Observed property set(s) to any value:
<Mock>.NumSides = 22
Summary of Qualifications
Type of TestCase Method matlab.mock.constraints Class
Qualification Use With
matlab.unittest.Test matlab.mock.const
Case Method raints Class
Method was verifyCalled or verifyThat WasCalled or
called verifyNotCalled Occurred
assumeCalled or assumeThat
assumeNotCalled
assertCalled or assertThat
assertNotCalled
fatalAssertCall fatalAssertThat
ed or
fatalAssertNotC
alled
Method was Not applicable verifyThat, WasCalled
called a assumeThat,
certain assertThat, or
number of fatalAssertThat
times
34-235
34 Unit Testing
34-236
See Also
See Also
34-237
35
A System object is a specialized MATLAB object. Many toolboxes include System objects.
System objects are designed specifically for implementing and simulating dynamic
systems with inputs that change over time. Many signal processing, communications, and
controls systems are dynamic. In a dynamic system, the values of the output signals
depend on both the instantaneous values of the input signals and on the past behavior of
the system. System objects use internal states to store that past behavior, which is used in
the next computational step. As a result, System objects are optimized for iterative
computations that process large streams of data in segments, such as video and audio
processing systems. This ability to process streaming data provides the advantage of not
having to hold large amounts of data in memory. Use of streaming data also allows you to
use simplified programs that use loops efficiently.
For example, you could use System objects in a system that reads data from a file, filters
that data and then writes the filtered output to another file. Typically, a specified amount
of data is passed to the filter in each loop iteration. The file reader object uses a state to
track where in the file to begin the next data read. Likewise, the file writer object tracks
where it last wrote data to the output file so that data is not overwritten. The filter object
maintains its own internal states to ensure that the filtering is performed correctly. This
diagram represents a single loop of the system.
These advantages make System objects well suited for processing streaming data.
35-2
What Are System Objects?
Note Check the product documentation to confirm fixed-point, code generation, and
MATLAB Compiler support for the specific System objects you want to use.
This separation of creation from execution lets you create multiple, persistent, reusable
objects, each with different settings. Using this approach avoids repeated input validation
and verification, allows for easy use within a programming loop, and improves overall
performance. In contrast, MATLAB functions must validate parameters every time you
call the function.
In addition to the System objects provided with System Toolboxes, you can create your
own System objects. See “Create System Objects”.
If you run a System object without any input arguments, you must include empty
parentheses. For example, asysobj().
When you run a System object, it also performs other important tasks related to data
processing, such as initialization and handling object states.
Note An alternative way to run a System object is to use the step function. For example,
for an object created using dft = dsp.FFT, you can run it using step(dft,x).
35-3
35 System object Usage and Authoring
All System objects support the following object functions. In cases where a function is not
applicable to a particular object, calling that function has no effect on the object.
Function Description
Run the object function, Runs the object to process data using the algorithm defined by
or that object.
step
Example: For the object dft = dsp.FFT;, run the object via:
• y = dft(x)
• y = step(dft,x)
35-4
See Also
Function Description
getDiscreteState Returns a structure of the object discrete states. If the object
has no discrete states, getDiscreteState returns an empty
structure.
clone Creates another object of the same type with the same property
values
isLocked Returns a logical value indicating whether the object has been
called and you have not yet called release on the object.
isDone Applies only to source objects that inherit from
matlab.system.mixin.FiniteSource. Returns a logical
value indicating whether the end of the data file has been
reached. If a particular object does not have end-of-data
capability, this function value always returns false.
info Returns a structure containing characteristic information about
the object. The fields of this structure vary depending on the
object. If a particular object does not have characteristic
information, the structure is empty.
See Also
matlab.System
Related Examples
• “System Objects vs MATLAB Functions” on page 35-6
• “System Design in MATLAB Using System Objects” on page 35-9
• “System Design in Simulink Using System Objects” (Simulink)
35-5
35 System object Usage and Authoring
Building a dynamic system with different execution phases and internal states using only
MATLAB functions would require complex programming. You would need code to
initialize the system, validate data, manage internal states, and reset and terminate the
system. System objects perform many of these managerial operations automatically
during execution. By combining System objects in a program with other MATLAB
functions, you can streamline your code and improve efficiency.
The code reads audio data from a file, filters it, and plays the filtered audio data. The
audio data is read in frames. This code produces the same result as the System objects
code in the next example, allowing you to compare approaches.
fname = 'speech_dft_8kHz.wav';
Obtain the total number of samples and the sampling rate from the source file.
audioInfo = audioinfo(fname);
maxSamples = audioInfo.TotalSamples;
fs = audioInfo.SampleRate;
35-6
System Objects vs MATLAB Functions
Define the amount of audio data to process at one time, and initialize the while loop
index.
frameSize = 1024;
nIdx = 1;
The loop uses explicit indexing and state management, which can be a tedious and error-
prone approach. You must have detailed knowledge of the states, such as, sizes and data
types. Another issue with this MATLAB-only code is that the sound function is not
designed to run in real time. The resulting audio is choppy and barely audible.
The code uses System objects from the DSP System Toolbox™ software to read audio data
from a file, filter it, and then play the filtered audio data. This code produces the same
result as the MATLAB® code shown previously, allowing you to compare approaches.
35-7
35 System object Usage and Authoring
filtLP = dsp.FIRFilter('Numerator',fir1(160,.15));
audioOut = audioDeviceWriter('SampleRate',audioIn.SampleRate);
while ~isDone(audioIn)
audio = audioIn(); % Read audio source file
y = filtLP(audio); % Filter the data
audioOut(y); % Play the filtered data
end
This System objects code avoids the issues present in the MATLAB-only code. Without
requiring explicit indexing, the file reader object manages the data frame sizes while the
filter manages the states. The audio device writer object plays each audio frame as it is
processed.
35-8
System Design in MATLAB Using System Objects
1 “Create Individual Components” on page 35-10 — Create the System objects to use
in your system. “Create Individual Components” on page 35-10. In addition to the
System objects provided with System Toolboxes, you can also create your own
System objects. See “Create System Objects”.
2 “Configure Components” on page 35-10 — If necessary, change the objects’ property
values to model your particular system. All System object properties have default
values that you may be able to use without changing them. See “Configure
Components” on page 35-10.
3 “Assemble Components Into System” on page 35-12 — Write a MATLAB program
that includes those System objects, connecting them using MATLAB variables as
inputs and outputs to simulate your system. See “Connecting System Objects” on
page 35-12.
35-9
35 System object Usage and Authoring
4 “Run Your System” on page 35-12 — Run your program. You can change tunable
properties while your system is running. See “Run Your System” on page 35-12 and
“Reconfiguring Objects” on page 35-13.
This section shows how to set up your system using predefined components from DSP
System Toolbox™ and Audio Toolbox™:
audioIn = dsp.AudioFileReader;
filtLP = dsp.FIRFilter;
audioOut = audioDeviceWriter;
Configure Components
When to Configure Components
If you did not set an object's properties when you created it and do not want to use
default values, you must explicitly set those properties. Some properties allow you to
change their values while your system is running. See “Reconfiguring Objects” on page
35-13 for information.
Most properties are independent of each other. However, some System object properties
enable or disable another property or limit the values of another property. To avoid errors
or warnings, you should set the controlling property before setting the dependent
property.
35-10
System Design in MATLAB Using System Objects
To display the current property values for an object, type that object’s handle name at the
command line (such as audioIn). To display the value of a specific property, type
objecthandle.propertyname (such as audioIn.FileName).
This section shows how to configure the components for your system by setting the
component objects’ properties.
Use this procedure if you have created your components separately from configuring
them. You can also create and configure your components at the same time, as described
in a later example.
For the file reader object, specify the file to read and set the output data type.
For the filter object, specify the filter numerator coefficients using the fir1 function, which
specifies the low pass filter order and the cutoff frequency.
For the audio device writer object, specify the sample rate. In this case, use the same
sample rate as the input data.
audioIn.Filename = "speech_dft_8kHz.wav";
audioIn.OutputDataType = "single";
filtLP.Numerator = fir1(160,.15);
audioOut.SampleRate = audioIn.SampleRate;
Create the file reader object, specify the file to read, and set the output data type.
audioIn = dsp.AudioFileReader("speech_dft_8kHz.wav",...
'OutputDataType',"single");
Create the filter object and specify the filter numerator using the fir1 function. Specify
the low pass filter order and the cutoff frequency of the fir1 function.
filtLP = dsp.FIRFilter('Numerator',fir1(160,.15));
35-11
35 System object Usage and Authoring
Create the audio player object and set the sample rate to the same rate as the input data.
audioOut = audioDeviceWriter('SampleRate',audioIn.SampleRate);
After you have determined the components you need and have created and configured
your System objects, assemble your system. You use the System objects like other
MATLAB variables and include them in MATLAB code. You can pass MATLAB variables
into and out of System objects.
The main difference between using System objects and using functions is that System
objects use a two-step process. First you create the object and set its parameters and
then, you run the object. Running the object initializes it and controls the data flow and
state management of your system. You typically call a System object within a code loop.
You use the output from an object as the input to another object. For some System
objects, you can use properties of those objects to change the inputs or outputs. To verify
that the appropriate number of inputs and outputs are being used, you can use nargin
and nargout on any System object. For information on all available System object
functions, see “System Object Functions” on page 35-4.
This section shows how to connect the components together to read, filter, and play a file
of audio data. The while loop uses the isDone function to read through the entire file.
while ~isDone(audioIn)
audio = audioIn(); % Read audio source file
y = filtLP(audio); % Filter the data
audioOut(y); % Play the filtered data
end
35-12
System Design in MATLAB Using System Objects
The first call to a System object initializes and runs the object. When a System object has
started processing data, you cannot change nontunable properties.
• Input size
• Input complexity
• Input data type
• Tunable property data types
• Discrete state data types
If the System object author has restricted these specifications, you get an error if you try
to change them while the System object is in use.
Reconfiguring Objects
Change Properties
When a System object has started processing data, you cannot change nontunable
properties. You can use isLocked on any System object to verify whether the object is
processing data. When processing is complete, you can use the release function to
release resources and allow changes to nontunable properties.
Some object properties are tunable, which enables you to change them even if the object
is in use. Most System object properties are nontunable. Refer to the object’s reference
page to determine whether an individual property is tunable.
During object usage, after you have called the algorithm, some System objects do not
allow changes in input complexity, size, or data type. If the System object restricts these
specifications, you can call release to change these specifications. Calling release also
resets other aspects of the System object, such as states and Discrete states.
This example shows how to change the filter type to a high-pass filter as the code is
running by modifying the Numerator property of the filter object. The change takes
effect the next time the object is called.
35-13
35 System object Usage and Authoring
35-14
Define Basic System Objects
1 In MATLAB, on the Editor tab, select New > System Object > Basic. A simple
System object template opens.
2 Subclass your object from matlab.System. Replace Untitled with AddOne in the
first line of your file.
System objects are composed of a base class, matlab.System and may include one
or more mixin classes. You specify the base class and mixin classes on the first line of
your class definition file.
3 Save the file and name it AddOne.m.
Define Algorithm
The stepImpl method contains the algorithm to execute when you run your object.
Define this method so that it contains the actions you want the System object to perform.
1 In the basic System object you created, inspect the stepImpl method template.
35-15
35 System object Usage and Authoring
All methods, except static methods, require the System object handle as the first
input argument. The default value, inserted by MATLAB Editor, is obj. You can use
any name for your System object handle.
By default, the number of inputs and outputs are both one. Inputs and outputs can be
added using Inputs/Outputs. You can also use a variable number of inputs or
outputs, see “Change the Number of Inputs” on page 35-18.
Alternatively, if you create your System object by editing a MAT-file, you can add the
stepImpl method using Insert Method > Implement algorithm.
2 Change the computation in the stepImpl method to add 1 to the value of u.
function y = stepImpl(~,u)
y = u + 1;
end
Tip Instead of passing in the object handle, you can use the tilde (~) to indicate that
the object handle is not used in the function. Using the tilde instead of an object
handle prevents warnings about unused variables.
3 Remove unused methods that are included by default in the basic template.
You can modify these methods to add more System object actions and properties. You
can also make no changes, and the System object still operates as intended.
The class definition file now has all the code necessary for this System object.
function y = stepImpl(~,u)
y = u + 1;
end
end
end
35-16
See Also
See Also
getNumInputsImpl | getNumOutputsImpl | matlab.System | stepImpl
Related Examples
• “Change the Number of Inputs” on page 35-18
• “System Design and Simulation in MATLAB” on page 35-9
35-17
35 System object Usage and Authoring
If you have a variable number of inputs or outputs and you intend to use the System
object in Simulink®, you must include the getNumInputsImpl or getNumOutputsImpl
method in your class definition.
These examples show modifications for the number of inputs. If you want to change the
number of outputs, the same principles apply.
As with all System object Impl methods, you always set the getNumInputsImpl and
getNumOutputsImpl method's access to protected because they are internal methods
that are never called directly.
This example shows how to write a System object that allows the number of inputs to
vary.
Update the stepImpl method to accept up to three inputs by adding code to handle one,
two, or three inputs. If you are only using this System object in MATLAB,
getNumInputsImpl and getNumOutputsImpl are not required.
35-18
Change the Number of Inputs
end
end
end
Run this System object with one, two, and three inputs.
addObj = AddTogether;
addObj(2)
ans =
addObj(2,3)
ans =
addObj(2,3,4)
ans =
This example shows how to write a System object that allows changes to the number of
inputs and outputs before running the object. Use this method when your System object
will be included in Simulink:
35-19
35 System object Usage and Authoring
properties (Nontunable)
NumInputs = 3; % Default value
end
methods (Access = protected)
function y = stepImpl(obj,x1,x2,x3)
switch obj.NumInputs
case 1
y = x1;
case 2
y = x1 + x2;
case 3
y = x1 + x2 + x3;
otherwise
y = [];
end
end
function validatePropertiesImpl(obj)
if ((obj.NumInputs < 1) ||...
(obj.NumInputs > 3))
error("Only 1, 2, or 3 inputs allowed.");
end
end
Run this System object with one, two, and three inputs.
addObj = AddTogether2;
addObj.NumInputs = 1;
addObj(2)
ans =
35-20
See Also
release(addObj);
addObj.NumInputs = 2;
addObj(2,3)
ans =
release(addObj);
addObj.NumInputs = 3;
addObj(2,3,4)
ans =
See Also
getNumInputsImpl | getNumOutputsImpl
Related Examples
• “Validate Property and Input Values” on page 35-22
• “Define Basic System Objects” on page 35-15
• “Using ~ as an Input Argument in Method Definitions” on page 35-65
35-21
35 System object Usage and Authoring
Validate Inputs
To validate input values, use the validateInputsImpl method. This example shows
how to validate that the first input is a numeric value.
35-22
Validate Property and Input Values
end
end
35-23
35 System object Usage and Authoring
See Also
validateInputsImpl | validatePropertiesImpl
Related Examples
• “Define Basic System Objects” on page 35-15
• “Change Input Complexity, Dimensions, or Data Type” on page 35-13
• “Summary of Call Sequence” on page 35-58
• “Property Set Methods”
• “Using ~ as an Input Argument in Method Definitions” on page 35-65
35-24
Initialize Properties and Setup One-Time Calculations
In this example, you allocate file resources by opening the file so the System object can
write to that file. You do these initialization tasks one time during setup, rather than every
time you run the object.
In this example, you define the public Filename property and specify the value of that
property as the nontunable character vector, default.bin. Users cannot change
nontunable properties after the setup method has been called.
properties (Nontunable)
Filename = "default.bin"
end
Users cannot access private properties directly, but only through methods of the System
object. In this example, you define the pFileID property as a private property. You also
define this property as hidden to indicate it is an internal property that never displays to
the user.
properties (Hidden,Access = private)
pFileID;
end
Define Setup
You use the setupImpl method to perform setup and initialization tasks. You should
include code in the setupImpl method that you want to execute one time only. The
setupImpl method is called once the first time you run the object. In this example, you
allocate file resources by opening the file for writing binary data.
methods
function setupImpl(obj)
obj.pFileID = fopen(obj.Filename,"wb");
if obj.pFileID < 0
error("Opening the file failed");
end
end
end
35-25
35 System object Usage and Authoring
Although not part of setup, you should close files when your code is done using them. You
use the releaseImpl method to release resources.
% This System
object™ writes the input to the file.
function stepImpl(obj,data)
fwrite(obj.pFileID,data);
end
35-26
See Also
end
end
See Also
matlab.System Constructor | releaseImpl | setupImpl | stepImpl
Related Examples
• “Release System Object Resources” on page 35-30
• “Define Property Attributes” on page 35-32
• “Summary of Call Sequence” on page 35-58
35-27
35 System object Usage and Authoring
Define the System object constructor, which is a method that has the same name as the
class (MyFile in this example). Within that method, you use the setProperties method
to make all public properties available for input when the user constructs the object.
nargin is a MATLAB function that determines the number of input arguments. varargin
indicates all of the object’s public properties.
methods
function obj = MyFile(varargin)
setProperties(obj,nargin,varargin{:});
end
end
methods
% You call setProperties in the constructor to let
% a user specify public properties of object as
% name-value pairs.
function obj = MyFile(varargin)
setProperties(obj,nargin,varargin{:});
35-28
See Also
end
end
See Also
nargin | setProperties
Related Examples
• “Define Property Attributes” on page 35-32
• “Release System Object Resources” on page 35-30
35-29
35 System object Usage and Authoring
35-30
See Also
This method allows you to clear the axes on the Whiteboard figure window while keeping
the figure open.
function releaseImpl(obj)
cla(Whiteboard.getWhiteboard());
hold on
end
For a complete definition of the Whiteboard System object, see “Create a Whiteboard
System object” on page 35-38.
See Also
releaseImpl | resetImpl
More About
• “Summary of Call Sequence” on page 35-58
• “Initialize Properties and Setup One-Time Calculations” on page 35-25
35-31
35 System object Usage and Authoring
Use the Nontunable attribute for a property when the algorithm depends on the value
being constant once data processing starts. Defining a property as nontunable may
improve the efficiency of your algorithm by removing the need to check for or react to
values that change. For code generation, defining a property as nontunable allows the
memory associated with that property to be optimized. You should define all properties
that affect the number of input or output ports as nontunable.
When you use the System object, you can only change nontunable properties before
calling the object or after calling the release function. For example, you define the
InitialValue property as nontunable and set its value to 0.
properties (Nontunable)
InitialValue = 0;
end
35-32
Define Property Attributes
properties (DiscreteState)
Count;
end
properties
% Whether to increment the counter, must be a logical scalar
Increment (1, 1) logical = true
end
properties (DiscreteState)
% Count state variable
Count
end
function c = stepImpl(obj)
if obj.Increment && (obj.Count < obj.MaxValue)
obj.Count = obj.Count + 1;
else
disp(['Max count, ' num2str(obj.MaxValue) ' ,reached'])
end
c = obj.Count;
end
35-33
35 System object Usage and Authoring
See Also
More About
• “Class Attributes”
• “Property Attributes”
• “What You Cannot Change While Your System Is Running” on page 35-13
• “Summary of Call Sequence” on page 35-58
35-34
Hide Inactive Properties
35-35
35 System object Usage and Authoring
See Also
isInactivePropertyImpl
35-36
Limit Property Values to Finite List
For a System object that is used in a MATLAB System block in Simulink you can use
enumerations or property validation. If you use enumerations, enumerations can also
derive from Simulink.IntEnumType. You use this type of enumeration to add attributes
(such as custom headers) to the input or output of the MATLAB System block. See “Use
Enumerated Data in Simulink Models” (Simulink).
This example defines a Style property that can have the values solid, dash, or dot.
The default value is solid and the (1,1) defines the property as a scalar.
properties
Style (1,1) string {mustBeMember(Style, ["solid","dash","dot"])} = "solid";
end
Enumeration Property
To use enumerated data in a System object, you refer to the enumerations as properties in
your System object class definition and define your enumerated class in a separate class
definition file.
This example defines a color enumeration property for a System object. The definition of
the enumeration class ColorValues is:
classdef ColorValues < int32
enumeration
blue (0)
red (1)
35-37
35 System object Usage and Authoring
green (2)
end
end
The ColorValues class inherits from int32 for code generation compatibility.
Enumeration values must be valid MATLAB identifiers on page 1-6.
In the System object, the Color property is defined as a ColorValues object with blue
as the default. The (1,1) defines the Color property as a scalar:
properties
Color (1, 1) ColorValues = ColorValues.blue
end
type('Whiteboard.m')
properties(Nontunable)
Color (1, 1) ColorValues = ColorValues.blue
Style (1,1) string {mustBeMember(Style, ["solid","dash","dot"])} = "solid";
end
35-38
Limit Property Values to Finite List
end
plot(h, randn([2,1]), randn([2,1]), ...
"Color",string(obj.Color), "LineStyle",linestyle);
end
function releaseImpl(~)
cla(Whiteboard.getWhiteboard());
hold on
end
end
methods (Static)
function a = getWhiteboard()
h = findobj('tag','whiteboard');
if isempty(h)
h = figure('tag','whiteboard');
hold on
end
a = gca;
end
end
end
greenInk = Whiteboard;
blueInk = Whiteboard;
greenInk.Color = "green";
blueInk.Color = "blue";
blueInk.Style = "dot";
for i=1:3
greenInk();
blueInk();
end
35-39
35 System object Usage and Authoring
release(greenInk);
release(blueInk);
35-40
See Also
See Also
Related Examples
• “Validate Property Values”
• “Enumerations”
• “Code Generation for Enumerations” (MATLAB Coder)
35-41
35 System object Usage and Authoring
properties
MiddleC = 440
NumNotes = 12
end
35-42
See Also
(1+log(1:obj.NumNotes)/log(12));
end
function hz = stepImpl(obj,noteShift)
% A noteShift value of 1 corresponds to obj.MiddleC
hz = obj.pLookupTable(noteShift);
end
function processTunedPropertiesImpl(obj)
propChange = isChangedProperty(obj,'NumNotes')||...
isChangedProperty(obj,'MiddleC')
if propChange
obj.pLookupTable = obj.MiddleC *...
(1+log(1:obj.NumNotes)/log(12));
end
end
end
See Also
processTunedPropertiesImpl
35-43
35 System object Usage and Authoring
To define a System object from other System objects, store those other objects in your
class definition file as properties. In this example, the highpass and lowpass filters are the
separate System objects defined in their own class-definition files.
properties (Access = private)
% Properties that hold filter System objects
pLowpass
pHighpass
end
function resetImpl(obj)
reset(obj.pLowpass);
35-44
Define Composite System Objects
reset(obj.pHighpass);
end
end
end
properties (Nontunable)
% Filter coefficients
Numerator = [0.006,-0.0133,-0.05,0.26,0.6,0.26,-0.05,-0.0133,0.006];
end
properties (DiscreteState)
State
end
function y = stepImpl(obj,u)
[y,obj.State] = filter(obj.Numerator,1,u,obj.State);
end
function resetImpl(obj)
obj.State = zeros(length(obj.Numerator)-1,1);
end
end
end
properties (Nontunable)
% Filter coefficients
Numerator = [0.006,0.0133,-0.05,-0.26,0.6,-0.26,-0.05,0.0133,0.006];
end
properties (DiscreteState)
35-45
35 System object Usage and Authoring
State
end
function y = stepImpl(obj,u)
[y,obj.State] = filter(obj.Numerator,1,u,obj.State);
end
function resetImpl(obj)
obj.State = zeros(length(obj.Numerator)-1,1);
end
end
end
See Also
nargin
35-46
Define Finite Source Objects
In this section...
“Use the FiniteSource Class and Specify End of the Source” on page 35-47
“Complete Class Definition File with Finite Source” on page 35-47
function y = stepImpl(obj)
if ~obj.isDone()
35-47
35 System object Usage and Authoring
obj.NumSteps = obj.NumSteps + 1;
y = obj.NumSteps;
else
y = 0;
end
end
See Also
matlab.system.mixin.FiniteSource
More About
• “Subclassing Multiple Classes”
• “Using ~ as an Input Argument in Method Definitions” on page 35-65
35-48
Save and Load System Object
obj.protectedprop = s.protectedprop;
obj.pdependentprop = s.pdependentprop;
if isInUse
obj.state = s.state;
end
35-49
35 System object Usage and Authoring
loadObjectImpl@matlab.System(obj,s,isInUse);
end
end
properties (Dependent)
dependentprop
end
35-50
Save and Load System Object
methods
function obj = MySaveLoader(varargin)
obj@matlab.System();
setProperties(obj,nargin,varargin{:});
end
% Serialization
methods (Access = protected)
function s = saveObjectImpl(obj)
% Call the base class method
s = saveObjectImpl@matlab.System(obj);
35-51
35 System object Usage and Authoring
end
end
function loadObjectImpl(obj,s,isInUse)
% Load child System objects
obj.child = matlab.System.loadObject(s.child);
See Also
loadObjectImpl | saveObjectImpl
35-52
Define System Object Information
You can define your own info method to display specific information for your System
object. The default infoImpl method returns an empty struct. This infoImpl method
returns detailed information when info is called using info(x,'details') or only
count information if it is called using info(x).
methods (Access = protected)
function s = infoImpl(obj,varargin)
if nargin>1 && strcmp('details',varargin(1))
s = struct('Name','Counter',
'Properties', struct('CurrentCount',obj.Count, ...
'Threshold',obj.Threshold));
else
s = struct('Count',obj.Count);
end
end
end
properties
Threshold = 1
end
properties (DiscreteState)
Count
end
function resetImpl(obj)
obj.Count = 0;
end
35-53
35 System object Usage and Authoring
function y = stepImpl(obj,u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function s = infoImpl(obj,varargin)
if nargin>1 && strcmp('details',varargin(1))
s = struct('Name','Counter',...
'Properties', struct('CurrentCount', ...
obj.Count,'Threshold',obj.Threshold));
else
s = struct('Count',obj.Count);
end
end
end
end
See Also
infoImpl
35-54
Handle Input Specification Changes
You can also restrict whether the input complexity, data type, or size can change while the
object is in use. Whichever aspects you restrict cannot change until after the user calls
release.
properties(Access = private)
isComplex (1,1) logical = false;
end
35-55
35 System object Usage and Authoring
This CounterSystem object restricts all three aspects of the input specification.
classdef Counter < matlab.System
%Counter Count values above a threshold
properties
Threshold = 1
end
properties (DiscreteState)
Count
end
methods
function obj = Counter(varargin)
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function resetImpl(obj)
obj.Count = 0;
end
35-56
See Also
See Also
isInputSizeMutableImpl
Related Examples
• “What You Cannot Change While Your System Is Running” on page 35-13
35-57
35 System object Usage and Authoring
The diagrams show an abstract view of which actions are performed when you call a
System object. The background color or each action indicates the type of call.
If you want a more detailed call sequence, see “Detailed Call Sequence” on page 35-62.
35-58
Summary of Call Sequence
35-59
35 System object Usage and Authoring
35-60
See Also
See Also
releaseImpl | resetImpl | setupImpl | stepImpl
Related Examples
• “Release System Object Resources” on page 35-30
• “Reset Algorithm State” on page 35-30
• “Set Property Values at Construction Time” on page 35-28
• “Define Basic System Objects” on page 35-15
35-61
35 System object Usage and Authoring
In this section...
“setup Call Sequence” on page 35-62
“Running the Object or step Call Sequence” on page 35-63
“reset Call Sequence” on page 35-63
“release Call Sequence” on page 35-63
The call sequence diagrams show the order in which internal methods are called when
you run the specified method. If your System object does not overwrite a specified
method, the default implementation of that method is used.
If you want a more abstract view of the method calls, see “Summary of Call Sequence” on
page 35-58.
35-62
See Also
1 If the System object is not in use (object was just created or was released),
Else, if the object is in use (object was called and release was not called)
i validatePropertiesImpl
ii processTunedPropertiesImpl
b If the input size, data type, or complexity has changed
i validateInputsImpl
ii processInputSpecificationChangeImpl
1 If the object is in use (object was called and not released), call resetImpl
1 If the object is in use (object was called and not released), call releaseImpl
See Also
releaseImpl | resetImpl | setupImpl | stepImpl
35-63
35 System object Usage and Authoring
Related Examples
• “Release System Object Resources” on page 35-30
• “Reset Algorithm State” on page 35-30
• “Set Property Values at Construction Time” on page 35-28
• “Define Basic System Objects” on page 35-15
35-64
Tips for Defining System Objects
General
• Define all one-time calculations in the setupImpl method and cache the results in a
private property. Use the stepImpl method for repeated calculations.
• Specify Boolean values using true or false instead of 1 or 0, respectively.
• If the variables in a method do not need to retain their values between calls, use local
scope for those variables in that method.
In many examples, instead of passing in the object handle, ~ is used to indicate that the
object handle is not used in the function. Using ~ instead of an object handle prevents
warnings about unused variables.
35-65
35 System object Usage and Authoring
Properties
• For properties that do not change, define them in as Nontunable properties.
Tunable properties have slower access times than Nontunable properties
• Whenever possible, use the protected or private attribute instead of the public
attribute for a property. Some public properties have slower access times than
protected and private properties.
• If properties are accessed more than once in the stepImpl method, cache those
properties as local variables inside the method. A typical example of multiple property
access is a loop. Iterative calculations using cached local variables run faster than
calculations that must access the properties of an object. When the calculations for the
method complete, you can save the local cached results back to the properties of that
System object. Copy frequently used tunable properties into private properties. This
best practice also applies to the updateImpl and outputImpl methods.
For example, in this code k is accessed multiple times in each loop iteration, but is
saved to the object property only once.
function y = stepImpl(obj,x)
k = obj.MyProp;
for p=1:100
y = k * x;
k = k + 0.1;
end
obj.MyProp = k;
end
• Default values of properties are shared across all instances of an object. Two instances
of a class can access the same default value if that property has not been overwritten
by either instance.
Text Comparisons
Do not use character vector comparisons or character vector-based switch statements in
the stepImpl method. Instead, create a method handle in setupImpl. This handle
points to a method in the same class definition file. Use that handle in a loop in
stepImpl.
This example shows how to use method handles and cached local variables in a loop to
implement an efficient object. In setupImpl, choose myMethod1 or myMethod2 based on
a character vector comparison and assign the method handle to the pMethodHandle
35-66
Tips for Defining System Objects
Simulink
For System objects being included in Simulink, add the StrictDefaults attribute. This
attribute sets all the MutableImpl methods to return false by default.
Code Generation
For information about System objects and code generation, see “System Objects in
MATLAB Code Generation” (MATLAB Coder).
35-67
35 System object Usage and Authoring
To access the System object editing options, create a new System object, or open an
existing one.
To add predefined code to your System object, select the code from the appropriate menu.
For example, when you click Insert Property > Numeric, the MATLAB Editor adds the
following code:
properties(Nontunable)
Property
end
The MATLAB Editor inserts the new property with the default name Property, which you
can rename. If you have an existing properties group with the Nontunable attribute, the
MATLAB Editor inserts the new property into that group. If you do not have a property
group, the MATLAB Editor creates one with the correct attribute.
35-68
Insert System Object Code Using MATLAB Editor
Insert Options
Properties Properties of the System object: Numeric, Logical, Enumeration, Positive Integer,
Tunable Numeric, Private, Protected, and Custom. When you select Enumeration or
Custom Properties, a separate dialog box opens to guide you in creating these
properties.
35-69
35 System object Usage and Authoring
Methods Methods commonly used in System object definitions. The MATLAB Editor creates
only the method structure. You specify the actions of that method.
function releaseImpl(obj)
% Release resources, such as file handles
end
If a method from the Insert Method menu is present in the System object code, that
method is shown shaded on the Insert Method menu:
35-70
Insert System Object Code Using MATLAB Editor
35-71
35 System object Usage and Authoring
Inputs / Inputs, outputs, and related methods, such as Validate inputs and Disallow input
Outputs size changes.
When you select an input or output, the MATLAB Editor inserts the specified code in
the stepImpl method. In this example, selecting Insert > Input causes the MATLAB
Editor to insert the required input variable u2. The MATLAB Editor determines the
variable name, but you can change it after it is inserted.
function y = stepImpl(obj,u,u2)
% Implement algorithm. Calculate y as a function of
% input u and discrete states.
y = u;
end
• Fahrenheit
• Celsius
• Kelvin
7 Select Fahrenheit as the default value by clicking Default.
35-72
Insert System Object Code Using MATLAB Editor
In the System object class definition, the following code was added:
properties(Nontunable)
TemperatureUnit (1, 1) TemperatureUnitValues = TemperatureUnitValues.Fahrenheit
end
For more information on enumerations, see “Limit Property Values to Finite List” on page
35-37.
35-73
35 System object Usage and Authoring
4 Click Insert and the following code is inserted into the System object definition:
properties(Nontunable, Constant)
Property
end
5 Replace Property with your property.
35-74
See Also
properties(Nontunable, Constant)
FreezingPointFahrenheit = 32;
end
The MATLAB Editor inserts this code into the System object:
function validateInputsImpl(obj,u)
% Validate inputs to the step method at initialization
end
See Also
Related Examples
• “Analyze System Object Code” on page 35-76
35-75
35 System object Usage and Authoring
• Navigate to a specific input, output, property, state, or method by clicking the name of
that element.
• Expand or collapse element sections with the arrow buttons.
• Identify access levels for properties and custom methods with the + (public), #
(protected), and – (private) symbols.
35-76
Analyze System Object Code
The cursor in the MATLAB Editor window jumps to the resetImpl method.
35-77
35 System object Usage and Authoring
See Also
Related Examples
• “Insert System Object Code Using MATLAB Editor” on page 35-68
35-78
Use Global Variables in System Objects
You set up and use global variables for the MATLAB System block in the same way as you
do for the MATLAB Function block (see “Data Stores” (Simulink) and “Share Data
Globally” (Simulink)). Like the MATLAB Function block, you must also use variable name
matching with a Data Store Memory block to use global variables in Simulink.
For example, this class definition file defines a System object that increments the first row
of a matrix by 1 at each time step. You must include getGlobalNamesImpl if the class
file is P-coded.
35-79
35 System object Usage and Authoring
end
end
This model includes the GlobalSysObjMatrix object in a MATLAB System block and
the associated Data Store Memory block.
35-80
Use Global Variables in System Objects
35-81
35 System object Usage and Authoring
35-82