Programming

Examples


Use this example index to jump to code examples in the documentation.

Constructing Matrices

Constructing a Simple Matrix
Creating a Magic Square Matrix.
Creating a Random Matrix.
Creating a Diagonal Matrix.
Concatenating Matrices
Replicating a Matrix.
Creating a Block Diagonal Matrix.
Combining Single and Double Types.
Combining Integer and Double Types.
Combining Character and Double Types.
Combining Logical and Double Types.

Accessing Matrix Elements

Accessing Single Elements
Linear Indexing
Functions That Control Indexing Style
Accessing Multiple Elements
Nonconsecutive Elements
Specifying All Elements of a Row or Column
Using a Matrix As an Index

Basic Matrix Operations

Example Using numel
Example Using ndims, numel, and size
Example Using isnumeric and isreal
Expanding the Size of a Matrix
Diminishing the Size of a Matrix
Reshaping a Matrix.
Transposing a Matrix.
Rotating a Matrix.
Flipping a Matrix.
Shifting the Location of Matrix Elements
Sorting the Data in Each Column
Sorting the Data in Each Row
Sorting Row Vectors
Constructing a Matrix from a Diagonal Vector
Returning a Triangular Portion of a Matrix

Multidimensional Arrays

Generating Arrays Using Indexing
Generating Arrays Using MATLAB Functions
Building Multidimensional Arrays with the cat Function
Operating on Planes and Matrices
Multidimensional Cell Arrays
Multidimensional Structure Arrays

Numeric Data Types

Creating Integer Data
Maximum and Minimum Double-Precision Values.
Creating Double-Precision Data.
Converting to Double Precision.
Maximum and Minimum Single-Precision Values.
Creating Single-Precision Data.
Converting to Single Precision.
Creating Complex Numbers
Display Format Examples
Setting Numeric Format in a Program

Logical Data Types

Logical Operations on an Array
Sparse Logical Arrays
Using Logicals in Conditional Statements
Logical Indexing Example 1.
Logical Indexing Example 2.

Character Arrays (Strings)

Creating Character Arrays
Creating Two-Dimensional Character Arrays
Converting to a Cell Array of Strings
Comparing Strings for Equality
Comparing for Equality Using Operators
Categorizing Characters Within a String
Searching and Replacing
Converting to a Character Equivalent
Converting to a String of Numbers
Converting from a Character Equivalent
Converting from a Numeric String

Dates and Times

Date Strings
Conversions Between Date Formats
Date String Formats
Converting Output Format with datestr
Current Date and Time

Structures

Building Structure Arrays Using Assignment Statements
Accessing Data in Structure Arrays
Dynamic Field Names Example
Adding Fields to Structures
Deleting Fields from Structures
Applying Functions and Operators
Writing Functions to Operate on Structures
Organizing Data in Structure Arrays
Example -- A Simple Database
Building Nested Structures with the struct Function

Cell Arrays

Creating Cell Arrays with Assignment Statements
Cell Array Syntax: Using Braces
Preallocating Cell Arrays with the cell Function
Accessing Cell Contents Using Content Indexing
Accessing a Subset of Cells Using Cell Indexing
Reshaping Cell Arrays
Replacing Lists of Variables with Cell Arrays
Applying Functions and Operators
Building Nested Arrays with Nested Curly Braces
Building Nested Arrays with the cell Function
Indexing Nested Cell Arrays
Converting Between Cell and Numeric Arrays
Cell Arrays of Structures

MATLAB Operators

Arithmetic Operators and Arrays
Relational Operators and Arrays
Element-Wise Operators and Functions
Logical Expressions Using the find Function.
Bit-Wise Functions
Overriding Default Precedence

Regular Expressions

Character Classes
Character Representation
Logical Operators
Lookaround Operators
Quantifiers
Using the token Parameter
Using Tokens -- Example 1
Using Tokens -- Example 2
Using Tokens in a Replacement String
Named Capture -- (?<name>expr)
Conditional Expressions -- (?(token)expr1|expr2)
Finding a Single Pattern in Multiple Strings
Finding Multiple Patterns in Multiple Strings
Replacing Multiple Strings

Comma-Separated Lists

Generating a List from a Cell Array
Generating a List from a Structure
Constructing Arrays
Displaying Arrays
Concatenation
Function Call Arguments
Function Return Values
Fast Fourier Transform Example

M-File Programming

Basic Parts of an M-File
Creating a Simple M-File
Simple Script Example
Simple Function Example
Detailed Display of M-File Dependencies
Checking the Number of Input Arguments
Passing Variable Numbers of Arguments
Unpacking varargin Contents
Packing varargout Contents
varargin and varargout in Argument Lists
Simple Function Handle Example
Querying Which Function MATLAB Will Call
MATLAB Command Syntax
MATLAB Function Syntax
Passing Arguments -- Example 1.
Passing Arguments -- Example 2.
Passing Strings
Passing Filenames
Passing Function Handles
Passing Arguments in a Structure
Passing Arguments in a Cell Array
Subfunctions
Vectorizing Loops

Anonymous Functions

Constructing an Anonymous Function
A Two-Input Example
Evaluating With No Input Arguments
Changing Variables Used in an Anonymous Function
Example 1 -- Passing a Function to quad
Example 2 -- Multiple Anonymous Functions

Nested Functions

Writing Nested Functions
Example -- More Than One Nested Function
Example -- Multiply Nested Functions
Calling Nested Functions
Variable Scope in Nested Functions
The Scope of Output Variables
Using Function Handles with Nested Functions
Defining Variables When Calling Via Function Handle.
Example Using Externally Scoped Variables
Separate Instances of Externally Scoped Variables
Example 1 -- Creating a Function Handle for a Nested Function
Example 2 -- Function-Generating Functions

Import Wizard

Specifying the Delimiter
Selecting the Variables to Import
Changing the Variable Selection.
Viewing the Variables

Saving and Loading

Using the save Function
Saving Structures
Using the load Function
Previewing MAT-File Contents
Loading into a Structure

Importing and Exporting Text

Using Import Functions with Text Data
Importing Numeric Text Data
Importing Delimited ASCII Data Files
Importing Numeric Data with Text Headers
Importing Mixed Alphabetic and Numeric Data
Using the save Function
Using the dlmwrite Function
Using the diary Function to Export Data

Importing and Exporting Spreadsheets

Example -- Querying an XLS File.
Example -- Writing To an XLS File.
Example -- Reading from an XLS File.
Example -- Querying a WK1 File.
Example -- Writing to a WK1 File.
Example -- Reading from a WK1 File.

Error Handling

Checking for Errors with try-catch
Nested try-catch Blocks
Reporting an Error
Message Identifiers
Formatted String Conversion
Example Using lasterr
Identifier Format
Returning a Message Identifier from lasterr
Formatted Message Strings
Message Identifiers
Example 1 -- Enabling a Selected Warning
Example 2 -- Disabling the Most Recent Warning
Output from Control Statements
Output Structure Array
Example 1 -- Performing an Explicit Query
Example 2 -- Performing an Implicit Query
Example 1 -- Entering Debug Mode on a Specific Warning
Example 2 -- Enabling Verbose Warnings

MATLAB Classes and Objects

How to Write subsref
Example -- A Polynomial Class
Example -- Assets and Asset Subclasses
Designing the Stock Class
Example -- The Portfolio Container
Example -- Defining saveobj and loadobj for Portfolio

Events and Callbacks

Writing a Callback Function

Improving Performance

Stopwatch Timer Functions
Simple Example of Vectorizing
Advanced Example of Vectorizing