Examples


Use this list to find 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

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 Classes

Creating Integer Data
Largest and Smallest Values for Integer Classes
Querying the Present Warning State
Turning the Warning On
Turning the Warning Off
Turning Warnings On or Off Temporarily
Creating Floating-Point Data
Arithmetic Operations on Floating-Point Numbers
Largest and Smallest Values for Floating-Point Classes
Accuracy of Floating-Point Data
Example 1 — Round-Off or What You Get Is Not What You Expect
Example 2 — Catastrophic Cancellation
Example 3 — Floating-Point Operations and Linear Algebra
Creating Complex Numbers
Infinity
NaN
Display Format Examples
Setting Numeric Format in a Program

Logical Classes

Overview of Logical Classes
Examples of Identifying Logical Arrays
Examples of Functions that Return a Logical Result
Using Logical Arrays in Conditional Statements

Logical Data Types

Functions that Return a Logical Result

Characters and Strings

Creating a Character String
Creating an Array of Strings
Creating Character Arrays by Concatenation
Working with Space Characters
Converting to a Cell Array of Strings
Formatting Strings
Sequential and Numbered Argument Specification
The Formatting Operator
Specifying Field Width and Precision Outside the format String
Using Identifiers In the Width and Precision Fields
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

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

Building Cell Arrays with Concatenation
Preallocating Cell Arrays with the cell Function
Referencing Cells of a Cell Array
Displaying Parts of the Cell Array
Assigning Cells
Assigning Multiple Cells
Working With Structures Within Cells
Working With Cell Arrays Within Cells
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

Function Handles

Handles to Anonymous Functions
Function Handle Arrays
Calling a Function Using Its Handle
Handling Values Returned From a Call
Example 1 — Run quad on Varying Functions
Example 2 — Run quad on Anonymous Functions
Example 3 — Compare quad Results on Different Functions
Example 1 — Constructing a Function Handle to Hold Data
Example 2 — Varying Data Values Stored in a Function Handle
Example 3 — You Cannot Vary Data in a Handle to an Anonymous Function
Examining a Function Handle
Converting a String to a Function Handle
Converting a Function Handle to a String
Comparing Handles Constructed from a Named Function
Comparing Handles to Anonymous Functions
Comparing Handles to Nested Functions
Comparing Handles Saved to a MAT-File

Map Containers

Constructing an Empty Map Object
Constructing An Initialized Map Object
Examining the Contents of the Map
Reading From the Map
Adding Key/Value Pairs
Building a Map with Concatenation
Removing Keys and Values from the Map
Modifying Values
Modifying Keys
Mapping to a Structure Array
Mapping to a Cell Array

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

Comma-Separated Lists

Generating a List from a Cell Array
Generating a List from a Structure
Assigning Output from a Comma-Separated List
Assigning to a Comma-Separated List
Constructing Arrays
Displaying Arrays
Concatenation
Function Call Arguments
Function Return Values
Fast Fourier Transform Example

Dates and Times

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

Regular Expressions

Character Classes
Grouping Operators
Positional Operators
Lookaround Operators
Using Lookaround as a Logical Operator
Quantifiers
Using Tokens — Example 1
Using Tokens — Example 2
Named Capture
Labeling Your Output
Example of a Dynamic Expression
Dynamic Expressions that Modify the Match Expression — (??expr)
Dynamic Commands that Modify the Match Expression — (??@cmd)
Dynamic Commands that Serve a Functional Purpose — (?@cmd)
Dynamic Commands that Modify the Replacement Expression — ${cmd}
Finding a Single Pattern in Multiple Strings
Finding Multiple Patterns in Multiple Strings
Replacing Multiple Strings

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
MATLAB Command Syntax
MATLAB Function Syntax
Example 1 — Simple Variable Comparison
Querying Which Function Gets Called
Passing Strings
Passing Filenames
Passing Function Handles
Passing Arguments in a Structure
Passing Arguments in a Cell Array
Checking the Number of Input Arguments
Passing Variable Numbers of Arguments
Unpacking varargin Contents
Packing varargout Contents
varargin and varargout in Argument Lists
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

Example of Selecting Variables to Load
Example of M-Code Generation
Example 1—Text Data
Example 2— Partial Text File with Row Vectors
Example 3 — Binary Data Assigned to a Structure

Memory Mapping

Constructing a memmapfile Object
Changing Property Values
Mapping Multiple Data Types and Arrays
Repeating a Format Scheme
Example 1 — Reading a Single Data Type
Example 2 — Formatting File Data as a Matrix
Example 3 — Reading Multiple Data Types
Example 4 — Modifying Map Parameters
Writing to a Mapped File
When Memory Is Mapped in Nonstructure Format
When Memory Is Mapped in Scalar Structure Format
When Memory Is Mapped in Nonscalar Structure Format
Working with Copies of the Mapped Data
Using the disp Method
Using the get Method
Memory-Mapping Demo

Memory-Mapping

Constructing the Object with Default Property Values
Mapping a Single Data Type
Formatting the Mapped Data to an Array

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

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 — Displaying a Stack Trace on a Specific Warning
Example 2 — Enabling Verbose Warnings

Events and Callbacks

Writing a Callback Function

Improving Performance

Stopwatch Timer Functions
Simple Example of Vectorizing
Advanced Example of Vectorizing

  


 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS