Programming Previous page   Next Page

Data Structures

Creating and Concatenating Matrices

Constructing a Simple Matrix

Specialized Matrix Functions

Concatenating Matrices

Matrix Concatenation Functions

Generating a Numeric Sequence

Combining Unlike Data Types

Matrix Indexing

Accessing Single Elements

Linear Indexing

Functions That Control Indexing Style

Accessing Multiple Elements

Logical Indexing

Indexing on Assignment

Getting Information About a Matrix

Dimensions of the Matrix

Data Types Used in the Matrix

Data Structures Used in the Matrix

Resizing and Reshaping Matrices

Expanding the Size of a Matrix

Diminishing the Size of a Matrix

Reshaping a Matrix

Preallocating Memory

Shifting and Sorting Matrices

Shift and Sort Functions

Shifting the Location of Matrix Elements

Sorting the Data in Each Column

Sorting the Data in Each Row

Sorting Row Vectors

Operating on Diagonal Matrices

Constructing a Matrix from a Diagonal Vector

Returning a Triangular Portion of a Matrix

Concatenating Matrices Diagonally

Empty Matrices, Scalars, and Vectors

The Empty Matrix

Scalars

Vectors

Full and Sparse Matrices

Sparse Matrix Functions

Multidimensional Arrays

Overview

Creating Multidimensional Arrays

Accessing Multidimensional Array Properties

Indexing Multidimensional Arrays

Reshaping Multidimensional Arrays

Permuting Array Dimensions

Computing with Multidimensional Arrays

Organizing Data in Multidimensional Arrays

Multidimensional Cell Arrays

Multidimensional Structure Arrays

Summary of Matrix and Array Functions

Data Types

Overview of MATLAB Data Types

Numeric Types

Integers

Floating-Point Numbers

Complex Numbers

Infinity and NaN

Identifying Numeric Types

Display Format for Numeric Values

Function Summary

Logical Types

Creating a Logical Array

How Logical Arrays Are Used

Identifying Logical Arrays

Characters and Strings

Creating Character Arrays

Cell Arrays of Strings

String Comparisons

Searching and Replacing

Converting from Numeric to String

Converting from String to Numeric

Function Summary

Dates and Times

Types of Date Formats

Conversions Between Date Formats

Date String Formats

Output Formats

Current Date and Time

Function Summary

Structures

Building Structure Arrays

Accessing Data in Structure Arrays

Using Dynamic Field Names

Finding the Size of Structure Arrays

Adding Fields to Structures

Deleting Fields from Structures

Applying Functions and Operators

Writing Functions to Operate on Structures

Organizing Data in Structure Arrays

Nesting Structures

Function Summary

Cell Arrays

Creating Cell Arrays

Obtaining Data from Cell Arrays

Deleting Cells

Reshaping Cell Arrays

Replacing Lists of Variables with Cell Arrays

Applying Functions and Operators

Organizing Data in Cell Arrays

Nesting Cell Arrays

Converting Between Cell and Numeric Arrays

Cell Arrays of Structures

Function Summary

Function Handles

Constructing and Invoking a Function Handle

Calling a Function Using Its Handle

Simple Function Handle Example

MATLAB Classes

Java Classes

Basic Program Components

Variables

Types of Variables

Naming Variables

Guidelines to Using Variables

Scope of a Variable

Lifetime of a Variable

Keywords

Special Values

Operators

Arithmetic Operators

Relational Operators

Logical Operators

Operator Precedence

MATLAB Expressions

String Evaluation

Shell Escape Functions

Regular Expressions

MATLAB Regular Expression Functions

Elements of an Expression

Character Classes

Character Representation

Logical Operators

Lookaround Operators

Quantifiers

Tokens

Handling Multiple Strings

Operator Summary

Comma-Separated Lists

Generating a List from a Cell Array

Generating a List from a Structure

How to Use the Comma-Separated List

Fast Fourier Transform Example

Program Control Statements

Conditional Control -- if, switch

Loop Control -- for, while, continue, break

Error Control -- try, catch

Program Termination -- return

Symbol Reference

Asterisk -- *

At -- @

Colon -- :

Comma -- ,

Curly Braces -- { }

Dot -- .

Dot-Dot -- ..

Dot-Dot-Dot (Ellipsis) -- ...

Dot-Parentheses -- .( )

Exclamation Point -- !

Parentheses -- ( )

Percent -- %

Percent-Brace -- %{ %}

Semicolon -- ;

Single Quotes -- ' '

Space Character

Slash and Backslash -- / \

Square Brackets -- [ ]

MATLAB Functions

M-File Functions

Built-In Functions

Overloaded MATLAB Functions

M-File Programming

Program Development

Creating a Program

Getting the Bugs Out

Cleaning Up the Program

Improving Performance

Checking It In

Working with M-Files

Types of M-Files

Basic Parts of an M-File

Creating a Simple M-File

Providing Help for Your Program

Creating P-Code Files

M-File Scripts and Functions

M-File Scripts

M-File Functions

Types of Functions

Identifying Dependencies

Function Arguments

Checking the Number of Input Arguments

Passing Variable Numbers of Arguments

Passing Optional Arguments to Nested Functions

Returning Output Arguments

Function Handles

Constructing a Function Handle

Calling a Function Using Its Handle

Functions That Operate on Function Handles

Additional Information on Function Handles

Calling Functions

What Happens When You Call a Function

Determining Which Function Is Called

MATLAB Calling Syntax

Passing Certain Argument Types

Passing Arguments in Structures or Cell Arrays

Calling External Functions

Running External Programs

Types of Functions

Overview of MATLAB Function Types

Anonymous Functions

Constructing an Anonymous Function

Arrays of Anonymous Functions

Outputs from Anonymous Functions

Variables Used in the Expression

Examples of Anonymous Functions

Primary M-File Functions

Nested Functions

Writing Nested Functions

Calling Nested Functions

Variable Scope in Nested Functions

Using Function Handles with Nested Functions

Examples of Nested Functions

Subfunctions

Calling Subfunctions

Accessing Help for a Subfunction

Private Functions

Private Directories

Accessing Help for a Private Function

Overloaded Functions

Class Directories

Data Import and Export

Overview

Text Data

Graphics Files

Audio and Audio/Video Data

Spreadsheets

Scientific Formats

The Internet

Low-Level File I/O

Large Data Sets

Toolboxes for Importing Data

Using the Import Wizard

Using the Import Wizard with Text Data

Using the Import Wizard with Binary Data

Supported File Formats

Saving and Loading MAT-Files

Exporting Data to MAT-Files

Importing Data from MAT-Files

Accessing Files with Memory-Mapping

Overview of Memory-Mapping in MATLAB

The memmapfile Class

Constructing a memmapfile Object

Reading a Mapped File

Writing to a Mapped File

Methods of the memmapfile Class

Deleting a Memory Map

Memory Mapping Demo

Importing Text Data

The MATLAB Import Wizard

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

Importing from XML Documents

Exporting Text Data

Exporting Delimited ASCII Data Files

Using the diary Function to Export Data

Exporting to XML Documents

Working with Graphics Files

Getting Information About Graphics Files

Importing Graphics Data

Exporting Graphics Data

Working with Audio and Video Data

Getting Information About Audio/Video Files

Importing Audio/Video Data

Exporting Audio/Video Data

Working with Spreadsheets

Microsoft Excel Spreadsheets

Lotus 123 Spreadsheets

Working with Scientific Data Formats

Working with Common Data Format (CDF) Files

Working with Flexible Image Transport System (FITS) Files

Working with Hierarchical Data Format (HDF5) Files

Importing HDF4 and HDF-EOS Data

Using the HDF Import Tool

Using the HDF4 Import Tool Subsetting Options

Using the MATLAB hdfread Function

Using the HDF4 Command-Line Interface

Exporting MATLAB Data to an HDF4 File

Example: Exporting Data to an HDF4 File

Using the MATLAB HDF Utility API

Using Low-Level File I/O Functions

Opening Files

Reading Binary Data

Writing Binary Data

Controlling Position in a File

Reading Strings Line by Line from Text Files

Reading Formatted ASCII Data

Writing Formatted Text Files

Closing a File

Exchanging Files over the Internet

Downloading Web Content and Files

Creating and Uncompressing Zip Archives

Sending E-Mail

Performing FTP File Operations

Error Handling

Checking for Errors with try-catch

Nested try-catch Blocks

Handling and Recovering from an Error

Reporting an Error

Identifying the Cause

Regenerating an Error

Message Identifiers

Identifier Format

Using Message Identifiers with lasterr

Warnings

Reporting a Warning

Identifying the Cause

Warning Control

Warning Statements

Warning Control Statements

Output from Control Statements

Saving and Restoring State

Debug, Backtrace, and Verbose Modes

Debugging Errors and Warnings

Classes and Objects

Classes and Objects: An Overview

Features of Object-Oriented Programming

MATLAB Data Class Hierarchy

Creating Objects

Invoking Methods on Objects

Private Methods

Helper Functions

Debugging Class Methods

Setting Up Class Directories

Data Structure

Tips for C++ and Java Programmers

Designing User Classes in MATLAB

The MATLAB Canonical Class

The Class Constructor Method

Examples of Constructor Methods

Identifying Objects Outside the Class Directory

The display Method

Accessing Object Data

The set and get Methods

Indexed Reference Using subsref and subsasgn

Handling Subscripted Reference

Handling Subscripted Assignment

Object Indexing Within Methods

Defining end Indexing for an Object

Indexing an Object with Another Object

Converter Methods

Overloading Operators and Functions

Overloading Operators

Overloading Functions

Example -- A Polynomial Class

Polynom Data Structure

Polynom Methods

The Polynom Constructor Method

Converter Methods for the Polynom Class

The Polynom display Method

The Polynom subsref Method

Overloading Arithmetic Operators for polynom

Overloading Functions for the Polynom Class

Listing Class Methods

Building on Other Classes

Simple Inheritance

Multiple Inheritance

Aggregation

Example -- Assets and Asset Subclasses

Inheritance Model for the Asset Class

Asset Class Design

Other Asset Methods

The Asset Constructor Method

The Asset get Method

The Asset set Method

The Asset subsref Method

The Asset subsasgn Method

The Asset display Method

The Asset fieldcount Method

Designing the Stock Class

The Stock Constructor Method

The Stock get Method

The Stock set Method

The Stock subsref Method

The Stock subsasgn Method

The Stock display Method

Example -- The Portfolio Container

Designing the Portfolio Class

The Portfolio Constructor Method

The Portfolio display Method

The Portfolio pie3 Method

Creating a Portfolio

Saving and Loading Objects

Modifying Objects During Save or Load

Example -- Defining saveobj and loadobj for Portfolio

Summary of Code Changes

The saveobj Method

The loadobj Method

Changing the Portfolio Constructor

The Portfolio subsref Method

Object Precedence

Specifying Precedence of User-Defined Classes

How MATLAB Determines Which Method to Call

Selecting a Method

Querying Which Method MATLAB Will Call

Scheduling Program Execution with Timers

Using a MATLAB Timer Object

Example: Displaying a Message

Creating Timer Objects

Timer Object Naming

Working with Timer Object Properties

Retrieving the Value of Timer Object Properties

Setting the Value of Timer Object Properties

Starting and Stopping Timers

Starting a Timer

Starting a Timer at a Specified Time

Stopping Timer Objects

Blocking the MATLAB Command Line

Creating and Executing Callback Functions

Associating Commands with Timer Object Events

Creating Callback Functions

Specifying the Value of Callback Function Properties

Timer Object Execution Modes

Executing a Timer Callback Function Once

Executing a Timer Callback Function Multiple Times

Handling Callback Function Queuing Conflicts

Deleting Timer Objects from Memory

Testing the Validity of a Timer Object

Deleting All Existing Timer Objects

Finding All Timer Objects in Memory

Finding Invisible Timer Objects

Improving Performance and Memory Usage

Analyzing Your Program's Performance

The M-File Profiler Utility

Stopwatch Timer Functions

Techniques for Improving Performance

Vectorizing Loops

Preallocating Arrays

Coding Loops in a MEX-File

Assigning to Variables

Operating on Real Data

Using Appropriate Logical Operators

Overloading Built-In Functions

Functions Are Generally Faster Than Scripts

Load and Save Are Faster Than File I/O Functions

Avoid Large Background Processes

Using Memory Efficiently

Memory Allocation for Arrays

Data Structures and Memory

Memory Management Functions

Strategies for Efficient Use of Memory

Resolving "Out of Memory" Errors

General Suggestions for Reclaiming Memory

Compressing Data in Memory

Increasing System Swap Space

Freeing Up System Resources on Windows Systems

Reloading Variables on UNIX Systems

Programming Tips

Command and Function Syntax

Syntax Help

Command and Function Syntaxes

Command Line Continuation

Completing Commands Using the Tab Key

Recalling Commands

Clearing Commands

Suppressing Output to the Screen

Help

Using the Help Browser

Help on Functions from the Help Browser

Help on Functions from the Command Window

Topical Help

Paged Output

Writing Your Own Help

Help for Subfunctions and Private Functions

Help for Methods and Overloaded Functions

Development Environment

Workspace Browser

Using the Find and Replace Utility

Commenting Out a Block of Code

Creating M-Files from Command History

Editing M-Files in EMACS

M-File Functions

M-File Structure

Using Lowercase for Function Names

Getting a Function's Name and Path

What M-Files Does a Function Use?

Dependent Functions, Built-Ins, Classes

Function Arguments

Getting the Input and Output Arguments

Variable Numbers of Arguments

String or Numeric Arguments

Passing Arguments in a Structure

Passing Arguments in a Cell Array

Program Development

Planning the Program

Using Pseudo-Code

Selecting the Right Data Structures

General Coding Practices

Naming a Function Uniquely

The Importance of Comments

Coding in Steps

Making Modifications in Steps

Functions with One Calling Function

Testing the Final Program

Debugging

The MATLAB Debug Functions

More Debug Functions

The MATLAB Graphical Debugger

A Quick Way to Examine Variables

Setting Breakpoints from the Command Line

Finding Line Numbers to Set Breakpoints

Stopping Execution on an Error or Warning

Locating an Error from the Error Message

Using Warnings to Help Debug

Making Code Execution Visible

Debugging Scripts

Variables

Rules for Variable Names

Making Sure Variable Names Are Valid

Don't Use Function Names for Variables

Checking for Reserved Keywords

Avoid Using i and j for Variables

Avoid Overwriting Variables in Scripts

Persistent Variables

Protecting Persistent Variables

Global Variables

Strings

Creating Strings with Concatenation

Comparing Methods of Concatenation

Store Arrays of Strings in a Cell Array

Converting Between Strings and Cell Arrays

Search and Replace Using Regular Expressions

Evaluating Expressions

Find Alternatives to Using eval

Assigning to a Series of Variables

Short-Circuit Logical Operators

Changing the Counter Variable within a for Loop

MATLAB Path

Precedence Rules

File Precedence

Adding a Directory to the Search Path

Handles to Functions Not on the Path

Making Toolbox File Changes Visible to MATLAB

Making Nontoolbox File Changes Visible to MATLAB

Change Notification on Windows

Program Control

Using break, continue, and return

Using switch Versus if

MATLAB case Evaluates Strings

Multiple Conditions in a case Statement

Implicit Break in switch-case

Variable Scope in a switch

Catching Errors with try-catch

Nested try-catch Blocks

Forcing an Early Return from a Function

Save and Load

Saving Data from the Workspace

Loading Data into the Workspace

Viewing Variables in a MAT-File

Appending to a MAT-File

Save and Load on Startup or Quit

Saving to an ASCII File

Files and Filenames

Naming M-files

Naming Other Files

Passing Filenames as Arguments

Passing Filenames to ASCII Files

Determining Filenames at Run-Time

Returning the Size of a File

Input/Output

File I/O Function Overview

Common I/O Functions

Readable File Formats

Using the Import Wizard

Loading Mixed Format Data

Reading Files with Different Formats

Reading ASCII Data into a Cell Array

Interactive Input into Your Program

Starting MATLAB

Getting MATLAB to Start Up Faster

Operating System Compatibility

Executing O/S Commands from MATLAB

Searching Text with grep

Constructing Paths and Filenames

Finding the MATLAB Root Directory

Temporary Directories and Filenames

Demos

Demos Available with MATLAB

For More Information


Previous page  Programming Data Structures Next page

© 1994-2005 The MathWorks, Inc.