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

21 PDFsam Matlab Prog

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

21 PDFsam Matlab Prog

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

Code Folding — Expand and Collapse Code Constructs . . . . . . . . .

24-18
Code Refactoring — Automatically convert selected code to a function
................................................ 24-21

Find and Replace Text in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-22


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

Add Reminders to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-29


Working with TODO/FIXME Reports . . . . . . . . . . . . . . . . . . . . . . . 24-29

MATLAB Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-31


Running the Code Analyzer Report . . . . . . . . . . . . . . . . . . . . . . . . 24-31
Changing Code Based on Code Analyzer Messages . . . . . . . . . . . . 24-32
Other Ways to Access Code Analyzer Messages . . . . . . . . . . . . . . 24-32

MATLAB Code Compatibility Report . . . . . . . . . . . . . . . . . . . . . . . . 24-34


Generate the Code Compatibility Report . . . . . . . . . . . . . . . . . . . . 24-34
Programmatic Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-36
Unsupported Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24-36

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

Protect Your Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-6


Building a Content Obscured Format with P-Code . . . . . . . . . . . . . . 25-6
Building a Standalone Executable . . . . . . . . . . . . . . . . . . . . . . . . . . 25-7

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


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

Create and Share Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-11


Create Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-11
Share Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25-15

xxi
Function Argument Validation
26
Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Introduction to Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Where to Use Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
arguments Block Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-2
Examples of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 26-5
Kinds of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-6
Required and Optional Positional Arguments . . . . . . . . . . . . . . . . . 26-6
Repeating Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-8
Name-Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-10
Name-Value Arguments from Class Properties . . . . . . . . . . . . . . . 26-13
Argument Validation in Class Methods . . . . . . . . . . . . . . . . . . . . . 26-15
Order of Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-15
Avoiding Class and Size Conversions . . . . . . . . . . . . . . . . . . . . . . 26-16
nargin in Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-18
Restrictions on Variable and Function Access . . . . . . . . . . . . . . . . 26-19
Debugging Arguments Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-20

Argument Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-21


Numeric Value Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-21
Comparison with Other Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22
Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-22
Membership and Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-23
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-23
Define Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-23

Ways to Parse Function Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25


Function Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25
validateattributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25
inputParser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26-25

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


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

Software Development

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

xxii Contents
Throw an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-4
Suggestions on How to Throw an Exception . . . . . . . . . . . . . . . . . . 27-4

Respond to an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-6


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

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


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

Issue Warnings and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-13


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

Suppress Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-16


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

Restore Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-18


Disable and Restore a Particular Warning . . . . . . . . . . . . . . . . . . . 27-18
Disable and Restore Multiple Warnings . . . . . . . . . . . . . . . . . . . . . 27-19

Change How Warnings Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27-20


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

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

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

Timer Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28-4


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

Handling Timer Queuing Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . 28-8


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

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

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


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

Determine Code Coverage Using the Profiler . . . . . . . . . . . . . . . . . 29-12

Techniques to Improve Performance . . . . . . . . . . . . . . . . . . . . . . . . 29-14


Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29-14
Programming Practices for Performance . . . . . . . . . . . . . . . . . . . . 29-14
Tips on Specific MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . 29-15

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

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

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

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


Leverage tall Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-6
Leverage the Memory of Multiple Machines . . . . . . . . . . . . . . . . . . 30-7
Load Only as Much Data as You Need . . . . . . . . . . . . . . . . . . . . . . . 30-7
Increase System Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-8
Set the Process Limit on Linux Systems . . . . . . . . . . . . . . . . . . . . . 30-8
Disable Java VM on Linux Systems . . . . . . . . . . . . . . . . . . . . . . . . . 30-9

xxiv Contents
How MATLAB Allocates Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-10

Avoid Unnecessary Copies of Data . . . . . . . . . . . . . . . . . . . . . . . . . . 30-14


Passing Values to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-14
Why Pass-by-Value Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-17
Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30-17

Custom Help and Documentation


31
Create Help for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-2
Help Text from the doc Command . . . . . . . . . . . . . . . . . . . . . . . . . . 31-2
Custom Help Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-3

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

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


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

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


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

Display Custom Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-21


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

Display Custom Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-29


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

Projects
32
Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
What Are Projects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Create Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Open Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-2
Set up Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-3

xxv
Add Files to Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-5
Other Ways to Create Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-6

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


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

Manage Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-10


Automatic Updates When Renaming, Deleting, or Removing Files
................................................ 32-11

Find Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12


Group and Sort Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Search for and Filter Project Files . . . . . . . . . . . . . . . . . . . . . . . . 32-12
Search the Content in Project Files . . . . . . . . . . . . . . . . . . . . . . . . 32-12

Create Shortcuts to Frequent Tasks . . . . . . . . . . . . . . . . . . . . . . . . 32-14


Run Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Create Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14
Organize Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-14

Add Labels to Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16


Add Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
View and Edit Label Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-16
Create Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-17

Create Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18


Create a Custom Task Function . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18
Run a Custom Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-18
Save Custom Task Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-19

Componentize Large Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-20


Add or Remove Reference to a Project . . . . . . . . . . . . . . . . . . . . . 32-20
View, Edit, or Run Referenced Project Files . . . . . . . . . . . . . . . . . 32-20
Extract Folder to Create a Referenced Project . . . . . . . . . . . . . . . 32-21
Manage Changes in Referenced Project Using Checkpoints . . . . . 32-21

Share Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-23


Create an Export Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-26

Upgrade Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-27


Run Upgrade Project Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-27
Examine Upgrade Project Report . . . . . . . . . . . . . . . . . . . . . . . . . 32-28

Analyze Project Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-30


Run a Dependency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-30
Explore the Dependency Graph, Views, and Filters . . . . . . . . . . . . 32-32
Investigate and Resolve Problems . . . . . . . . . . . . . . . . . . . . . . . . . 32-37
Find Required Products and Add-Ons . . . . . . . . . . . . . . . . . . . . . . 32-40
Find File Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-41
Export Dependency Analysis Results . . . . . . . . . . . . . . . . . . . . . . 32-42

Clone from Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-44

xxvi Contents
Use Source Control with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . 32-45
Setup Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-45
Perform Source Control Operations . . . . . . . . . . . . . . . . . . . . . . . 32-47
Work with Derived Files in Projects . . . . . . . . . . . . . . . . . . . . . . . 32-54
Find Project Files With Unsaved Changes . . . . . . . . . . . . . . . . . . . 32-55
Manage Open Files When Closing a Project . . . . . . . . . . . . . . . . . 32-55

Create and Edit Projects Programmatically . . . . . . . . . . . . . . . . . . 32-56

Explore an Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-63

Source Control Interface


33
About MathWorks Source Control Integration . . . . . . . . . . . . . . . . . 33-2
Classic and Distributed Source Control . . . . . . . . . . . . . . . . . . . . . . 33-2

Select or Disable Source Control System . . . . . . . . . . . . . . . . . . . . . 33-4


Select Source Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-4
Disable Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-4

Create New Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-5


Create Git Repository on Your Local System . . . . . . . . . . . . . . . . . . 33-5

Review Changes in Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-7

Mark Files for Addition to Source Control . . . . . . . . . . . . . . . . . . . . 33-8

Resolve Source Control Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-9


Examining and Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . 33-9
Resolve Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-9
Merge Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10
Extract Conflict Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-10

Commit Modified Files to Source Control . . . . . . . . . . . . . . . . . . . . 33-12

Revert Changes in Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-13


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

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


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

Check Out from SVN Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-19


Retrieve Tagged Version of Repository . . . . . . . . . . . . . . . . . . . . . 33-19

xxvii
Update SVN File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . 33-21
Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-21

Get SVN File Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-22


Manage SVN Repository Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-22

Set Up Git Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23


Configure MATLAB on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 33-23
Use SSH Authentication with MATLAB . . . . . . . . . . . . . . . . . . . . . 33-23
Register Binary Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-24
Configure Git Credential Helper . . . . . . . . . . . . . . . . . . . . . . . . . . 33-25

Add Git Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-26


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

Retrieve Files from Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . 33-28

Update Git File Status and Revision . . . . . . . . . . . . . . . . . . . . . . . . 33-29


Refresh Status of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29
Update Revisions of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-29

Branch and Merge with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30


Create Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-30
Switch Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-31
Compare Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-32
Merge Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-32
Revert to Head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-33
Delete Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-33

Pull, Push and Fetch Files with Git . . . . . . . . . . . . . . . . . . . . . . . . . 33-34


Pull and Push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-34
Fetch and Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-35
Use Git Stashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-35

Move, Rename, or Delete Files Under Source Control . . . . . . . . . . 33-37

Customize External Source Control to Use MATLAB for Diff and


Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-38
Finding the Full Paths for MATLAB Diff, Merge, and AutoMerge . . 33-38
Integration with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-39
Integration with SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-40
Integration with Other Source Control Tools . . . . . . . . . . . . . . . . . 33-41

MSSCCI Source Control Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 33-43

Set Up MSSCCI Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-44


Create Projects in Source Control System . . . . . . . . . . . . . . . . . . . 33-44
Specify Source Control System with MATLAB Software . . . . . . . . 33-45
Register Source Control Project with MATLAB Software . . . . . . . . 33-46
Add Files to Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-48

xxviii Contents
Check Files In and Out from MSSCCI Source Control . . . . . . . . . . 33-49
Check Files Into Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . 33-49
Check Files Out of Source Control . . . . . . . . . . . . . . . . . . . . . . . . 33-49
Undoing the Checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-50

Additional MSSCCI Source Control Actions . . . . . . . . . . . . . . . . . . 33-51


Getting the Latest Version of Files for Viewing or Compiling . . . . . 33-51
Removing Files from the Source Control System . . . . . . . . . . . . . . 33-52
Showing File History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-52
Comparing the Working Copy of a File to the Latest Version in Source
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33-53
Viewing Source Control Properties of a File . . . . . . . . . . . . . . . . . 33-54
Starting the Source Control System . . . . . . . . . . . . . . . . . . . . . . . 33-55

Access MSSCCI Source Control from Editors . . . . . . . . . . . . . . . . . 33-57

Troubleshoot MSSCCI Source Control Problems . . . . . . . . . . . . . . 33-58


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

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

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

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

Extend Script-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-14


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

Run Tests in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-17

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


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

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

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

Extend Function-Based Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-32


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

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

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


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

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

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


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

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

Tag Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-47


Tag Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-47
Select and Run Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-48

Write Tests Using Shared Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . 34-51

Create Basic Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-54

Create Advanced Custom Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-56

Use Parameters in Class-Based Tests . . . . . . . . . . . . . . . . . . . . . . . 34-61


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

Create Basic Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-66

Create Advanced Parameterized Test . . . . . . . . . . . . . . . . . . . . . . . . 34-71

Use External Parameters in Parameterized Test . . . . . . . . . . . . . . . 34-78

Define Parameters at Suite Creation Time . . . . . . . . . . . . . . . . . . . 34-82

Create Simple Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-89

Run Tests for Various Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-91


Set Up Example Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-91
Run All Tests in Class or Function . . . . . . . . . . . . . . . . . . . . . . . . . 34-91
Run Single Test in Class or Function . . . . . . . . . . . . . . . . . . . . . . . 34-91
Run Test Suites by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-92
Run Test Suites from Test Array . . . . . . . . . . . . . . . . . . . . . . . . . . 34-92
Run Tests with Customized Test Runner . . . . . . . . . . . . . . . . . . . . 34-93

xxx Contents
Programmatically Access Test Diagnostics . . . . . . . . . . . . . . . . . . . 34-94

Add Plugin to Test Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-95

Write Plugins to Extend TestRunner . . . . . . . . . . . . . . . . . . . . . . . . 34-97


Custom Plugins Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-97
Extending Test Session Level Plugin Methods . . . . . . . . . . . . . . . . 34-97
Extending Test Suite Level Plugin Methods . . . . . . . . . . . . . . . . . . 34-98
Extending Test Class Level Plugin Methods . . . . . . . . . . . . . . . . . 34-98
Extending Test Level Plugin Methods . . . . . . . . . . . . . . . . . . . . . . 34-99

Create Custom Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-100

Run Tests in Parallel with Custom Plugin . . . . . . . . . . . . . . . . . . . 34-105

Write Plugin to Add Data to Test Results . . . . . . . . . . . . . . . . . . . 34-113

Write Plugin to Save Diagnostic Details . . . . . . . . . . . . . . . . . . . . 34-118

Plugin to Generate Custom Test Output Format . . . . . . . . . . . . . . 34-122

Analyze Test Case Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-125

Analyze Failed Test Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-128

Rerun Failed Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-130

Dynamically Filtered Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-133


Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-133
Method Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . 34-135
Class Setup and Teardown Code . . . . . . . . . . . . . . . . . . . . . . . . . 34-136

Create Custom Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-139

Create Custom Boolean Constraint . . . . . . . . . . . . . . . . . . . . . . . . 34-142

Create Custom Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-146

Overview of App Testing Framework . . . . . . . . . . . . . . . . . . . . . . . 34-150


App Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-150
Gesture Support of UI Components . . . . . . . . . . . . . . . . . . . . . . 34-150
Write a Test for an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-152

Write Test for App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-155

Write Test That Uses App Testing and Mocking Frameworks . . . 34-159
Create App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-159
Test App With Manual Intervention . . . . . . . . . . . . . . . . . . . . . . . 34-160
Create Fully Automated Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-161

Overview of Performance Testing Framework . . . . . . . . . . . . . . . . 34-164


Determine Bounds of Measured Code . . . . . . . . . . . . . . . . . . . . . 34-164
Types of Time Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-165
Write Performance Tests with Measurement Boundaries . . . . . . . 34-165

xxxi
Run Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-166
Understand Invalid Test Results . . . . . . . . . . . . . . . . . . . . . . . . . 34-166

Test Performance Using Scripts or Functions . . . . . . . . . . . . . . . . 34-168

Test Performance Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 34-172

Measure Fast Executing Test Code . . . . . . . . . . . . . . . . . . . . . . . . 34-178

Create Mock Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-181

Specify Mock Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-188


Define Mock Method Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 34-188
Define Mock Property Behavior . . . . . . . . . . . . . . . . . . . . . . . . . 34-189
Define Repeating and Subsequent Behavior . . . . . . . . . . . . . . . . 34-190
Summary of Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-192

Qualify Mock Object Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . 34-193


Qualify Mock Method Interaction . . . . . . . . . . . . . . . . . . . . . . . . 34-193
Qualify Mock Property Interaction . . . . . . . . . . . . . . . . . . . . . . . 34-194
Use Mock Object Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-195
Summary of Qualifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-197

Ways to Write Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-199


Script-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-199
Function-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-200
Class-Based Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-200
Extend Unit Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . 34-201

Compile MATLAB Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-202


Run Tests with Standalone Applications . . . . . . . . . . . . . . . . . . . 34-202
Run Tests in Parallel with Standalone Applications . . . . . . . . . . . 34-203
TestRand Class Definition Summary . . . . . . . . . . . . . . . . . . . . . . 34-203

Develop and Integrate Software with Continuous Integration . . 34-205


Continuous Integration Workflow . . . . . . . . . . . . . . . . . . . . . . . . 34-205
Continuous Integration with MathWorks Products . . . . . . . . . . . 34-207

Generate Artifacts Using MATLAB Unit Test Plugins . . . . . . . . . . 34-209

Continuous Integration with MATLAB on CI Platforms . . . . . . . . 34-213


Azure DevOps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-213
CircleCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-213
GitHub Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-213
Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-213
Travis CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-214
Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-214

xxxii Contents
System object Usage and Authoring
35
What Are System Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-2
Running a System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-3
System Object Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-3

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


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

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


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

Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-11


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

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

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


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

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

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

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


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

Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-24


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

Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-26


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

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


Property Validation with mustBeMember . . . . . . . . . . . . . . . . . . . . . . . 35-28
Enumeration Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-28
Create a Whiteboard System object . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-29

xxxiii
Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-32

Define Composite System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-34

Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-36


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

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


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

Define System Object Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-41

Handle Input Specification Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-43


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

Summary of Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-45


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

Detailed Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-48


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

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


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

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


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

Analyze System Object Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-58


View and Navigate System object Code . . . . . . . . . . . . . . . . . . . . . . . . 35-58
Example: Go to StepImpl Method Using Analyzer . . . . . . . . . . . . . . . . . 35-58

Use Global Variables in System Objects . . . . . . . . . . . . . . . . . . . . . . . . . 35-60


System Object Global Variables in MATLAB . . . . . . . . . . . . . . . . . . . . . 35-60
System Object Global Variables in Simulink . . . . . . . . . . . . . . . . . . . . . 35-60

Create Moving Average System object . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-64

xxxiv Contents
Create New System Objects for File Input and Output . . . . . . . . . . . . . 35-69

Create Composite System object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35-75

xxxv
Language

37
1

Syntax Basics

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


• “Ignore Function Outputs” on page 1-3
• “Variable Names” on page 1-4
• “Case and Space Sensitivity” on page 1-5
• “Choose Command Syntax or Function Syntax” on page 1-6
• “Resolve Error: Undefined Function or Variable” on page 1-9
1 Syntax Basics

Continue Long Statements on Multiple Lines


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

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


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

Build a long character vector by concatenating shorter vectors together:

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


'engineering and science'];

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

mytext = 'Accelerating the pace of ...


engineering and science'

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

x = [1.23...
4.56];

is the same as

x = [1.23 4.56];

1-2

You might also like