Tải bản đầy đủ (.pdf) (865 trang)

Tài liệu MATLAB® 7 Programming: How to Contact The MathWorks docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.11 MB, 865 trang )

MATLAB
®
7
Programming
How to Contact The MathWorks
www.mathworks.c
om
Web
comp.soft-sys.matlab
Newsgroup
www.mathworks.com/contact_TS.html
Technical Support

Product enhancement suggestions

om
Bug reports

Documentation error reports

Order status, license renewals, passcodes

om
Sales, pricing, an
d general information
508-647-7000 (Phone)
508-647-7001 (Fax)
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site.


MATLAB Programming
© COPYRIGHT 1984–2007 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program or
Documentation, the government hereby agrees that this software or documentation qualifies as commercial
computer software or commercial computer software documentation as such terms are used or defined
in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of
this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use,
modification, reproduction, release, performance, display, and disclosure of the Program and Documentation
by the federal government (or other entity acquiring for or through the federal government) and shall
supersede any conflicting contractual terms or conditions. If this License fails to meet the government’s
needs or is inconsistent in any respect with federal procurement law, the government agrees to return the
Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, and xPC TargetBox
are registered trademarks, and SimBiology, SimEvents, and SimHydraulics are trademarks of
The MathWorks, Inc.
Other product or brand names are trademarks or registered trademarks of their respective
holders.
Patents
The MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents
for more information.
Revision History
June 2004 First printing New for MATLAB 7.0 (Release 14)
October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1)
March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2)

June 2005 Second printing Minor revision for MATLAB 7.0.4
September 2005 Online only Revised for MATLAB 7.1 (Release 14SP3)
March 2006 Online only Revised for MATLAB 7.2 (Release R2006a)
September 2006 Online only Revised for MATLAB 7.3 (Release R2006b)
March 2007 Online only Revised for MATLAB 7.4 (Release R2007a)

Contents
Data Structures
1
Creating and Concatenating Matrices
...............
1-3
Constructing a Simple Matrix
.......................
1-3
Specialized Matrix Functions
........................
1-5
Concatenating Matrices
............................
1-7
Matrix Concatenation Functions
.....................
1-8
Generating a Numeric Sequence
.....................
1-10
Combining Unlike Data Types
.......................
1-12

Matrix Indexing
...................................
1-18
Accessing Single Elements
..........................
1-18
Linear Indexing
...................................
1-19
Functions That Control Indexing Style
................
1-19
Accessing Multiple Elements
........................
1-20
Logical Indexing
..................................
1-23
Indexing on Assignment
............................
1-23
Getting Information About a Matrix
.................
1-24
Dimensions of the Matrix
...........................
1-24
Data Types Used in the Matrix
......................
1-25

Data Structures Used in the Matrix
..................
1-26
Resizing and Reshaping Matrices
...................
1-27
Expanding the Size of a Matrix
......................
1-27
Diminishing the Size of a Matrix
.....................
1-31
Reshaping a Matrix
................................
1-32
Preallocating Memory
..............................
1-34
Shifting and Sorting Matrices
......................
1-37
Shift and Sort Functions
............................
1-37
Shifting the Location of Matrix Elements
..............
1-37
Sorting the Data in Each Column
....................
1-39

Sorting the Data in Each Row
.......................
1-39
Sorting Row Vectors
...............................
1-40
v
Operating on Diagonal Matrices
....................
1-42
Constructing a Matrix from a Diagonal Vector
..........
1-42
Returning a Triangular Portion of a Matrix
............
1-43
Concatenating Matrices Diagonally
...................
1-43
Empty Matrices, Scalars, and Vectors
................
1-44
The Empty Matrix
.................................
1-44
Scalars
..........................................
1-47
Vectors
..........................................

1-48
Full and Sparse Matrices
...........................
1-50
Sparse Matrix Functions
...........................
1-50
Multidimensional Arrays
...........................
1-52
Overview
........................................
1-52
Creating Multidimensional Arrays
...................
1-54
Accessing Multidimensional Array Properties
..........
1-57
Indexing Multidimensional Arrays
...................
1-58
Reshaping Multidimensional Arrays
..................
1-62
Permuting Array Dimensions
........................
1-64
Computing with Multidimensional Arrays
.............

1-66
Organizing Data in Multidimensional Arrays
...........
1-67
Multidimensional Cell Arrays
.......................
1-69
Multidimensional Structure Arrays
...................
1-70
Summary of Matrix and Array Functions
............
1-72
Data Types
2
Overview of MATLAB Data Types
...................
2-3
Data Type Summary
...............................
2-4
Numeric Types
....................................
2-6
Integers
.........................................
2-6
Floating-Point Numbers
............................
2-14

Complex Numbers
.................................
2-24
Infinity and NaN
..................................
2-25
vi
Contents
Identifying Numeric Types
..........................
2-27
Display Format for Numeric Values
...................
2-27
Function Summary
................................
2-29
Logical Types
.....................................
2-33
Creating a Logical Array
...........................
2-33
How Logical Arrays Are Used
.......................
2-35
Identifying Logical Arrays
..........................
2-37
Characters and Strings

.............................
2-38
Creating Character Arrays
..........................
2-38
Cell Arrays of Strings
..............................
2-40
Formatting Strings
................................
2-43
String Comparisons
...............................
2-56
Searching and Replacing
...........................
2-59
Converting from Numeric to String
...................
2-60
Converting from String to Numeric
...................
2-62
Function Summary
................................
2-64
Dates and Times
...................................
2-67
Types of Date Formats

.............................
2-67
Conversions Between Date Formats
..................
2-69
Date String Formats
...............................
2-69
Output Formats
...................................
2-70
Current Date and Time
.............................
2-72
Function Summary
................................
2-72
Structures
........................................
2-75
Building Structure Arrays
..........................
2-76
Accessing Data in Structure Arrays
...................
2-79
Using Dynamic Field Names
........................
2-81
Finding the Size of Structure Arrays

..................
2-82
Adding Fields to Structures
.........................
2-83
Deleting Fields from Structures
......................
2-84
Applying Functions and Operators
...................
2-84
Writing Functions to Operate on Structures
............
2-85
Organizing Data in Structure Arrays
.................
2-86
Nesting Structures
................................
2-92
Function Summary
................................
2-93
Cell Arrays
........................................
2-94
Cell Array Operators
...............................
2-95
vii

Creating a Cell Array
..............................
2-96
Referencing Cells of a Cell Array
.....................
2-100
Deleting Cells
....................................
2-107
Reshaping Cell Arrays
.............................
2-107
Replacing Lists of Variables with Cell Arrays
...........
2-108
Applying Functions and Operators
...................
2-109
Organizing Data in Cell Arrays
......................
2-110
Nesting Cell Arrays
................................
2-111
Converting Between Cell and Numeric Arrays
..........
2-113
Cell Arrays of Structures
...........................
2-114

Function Summary
................................
2-115
Function Handles
..................................
2-116
Constructing and Invoking a Function Handle
..........
2-116
Calling a Function Using Its Handle
..................
2-116
Simple Function Handle Example
....................
2-117
MATLAB Classes
...................................
2-118
Java Classes
......................................
2-119
Basic Program Components
3
Variables
..........................................
3-3
Types of Variables
.................................
3-3
Naming Variables

.................................
3-7
Guidelines to Using Variables
.......................
3-11
Scope of a Variable
................................
3-11
Lifetime of a Variable
..............................
3-13
Keywords
.........................................
3-14
Special Values
.....................................
3-15
Operators
.........................................
3-17
Arithmetic Operators
..............................
3-17
Relational Operators
...............................
3-18
viii
Contents
Logical Operators
.................................

3-20
Operator Precedence
...............................
3-26
MATLAB Expressions
..............................
3-28
String Evaluation
.................................
3-28
Shell Escape Functions
.............................
3-29
Regular Expressions
...............................
3-31
MATLAB Regular Expression Functions
...............
3-32
Elements of an Expression
..........................
3-33
Character Classes
.................................
3-33
Character Representation
...........................
3-36
Grouping Operators
...............................

3-37
Nonmatching Operators
............................
3-39
Positional Operators
...............................
3-40
Lookaround Operators
.............................
3-41
Quantifiers
.......................................
3-45
Tokens
..........................................
3-48
Named Capture
...................................
3-53
Conditional Expressions
............................
3-55
Dynamic Regular Expressions
.......................
3-58
String Replacement
................................
3-67
Handling Multiple Strings
..........................

3-69
Operator Summary
................................
3-72
Comma-Separated Lists
............................
3-80
Generating a Comma-Separated List
.................
3-80
Assigning Output from a Comma-Separated List
........
3-82
Assigning to a Comma-Separated List
................
3-83
How to Use the Comma-Separated Lists
...............
3-84
Fast Fourier Transform Example
.....................
3-86
Program Control Statements
.......................
3-88
Conditional Control — if, switch
.....................
3-88
Loop Control — for, while, continue, break
.............

3-92
Error Control — try, catch
..........................
3-95
Program Termination — return
......................
3-96
Symbol Reference
.................................
3-97
Asterisk — *
.....................................
3-98
At — @
..........................................
3-98
Colon — :
........................................
3-99
ix
Comma — ,
......................................
3-100
Curly Braces — { }
.................................
3-101
Dot — .
..........................................
3-102
Dot-Dot — ..

......................................
3-102
Dot-Dot-Dot (Ellipsis) — ...
..........................
3-103
Dot-Parentheses — .( )
.............................
3-103
Exclamation Point — !
.............................
3-104
Parentheses — ( )
.................................
3-104
Percent — %
.....................................
3-105
Percent-Brace — %{ %}
.............................
3-105
Semicolon — ;
....................................
3-106
Single Quotes — ’ ’
.................................
3-107
Space Character
..................................
3-107
Slash and Backslash — / \

..........................
3-108
Square Brackets — [ ]
..............................
3-108
MATLAB Functions
................................
3-110
M-File Functions
..................................
3-110
Built-In Functions
.................................
3-111
Overloaded MATLAB Functions
.....................
3-112
M-File Programming
4
Program D
evelopment
.............................
4-3
Creatin
gaProgram
................................
4-3
Getting
the Bugs Out
..............................

4-4
Cleanin
gUptheProgram
...........................
4-5
Improv
ing Performance
............................
4-6
Checki
ng It In
....................................
4-7
Worki
ng with M-Files
..............................
4-8
Types
of M-Files
..................................
4-8
Basic
Parts of an M-File
............................
4-9
Creat
ing a Simple M-File
...........................
4-13
Prov

iding Help for Your Program
.....................
4-16
Crea
ting P-Code Files
..............................
4-16
M-F
ile Scripts and Functions
.......................
4-1
8
x
Contents
M-File Scripts
....................................
4-18
M-File Functions
..................................
4-19
Types of Functions
................................
4-20
Identifying Dependencies
...........................
4-21
Function Handles
..................................
4-23
Constructing a Function Handle

.....................
4-23
Calling a Function Using Its Handle
..................
4-24
Functions That Operate on Function Handles
..........
4-26
Comparing Function Handles
.......................
4-26
Additional Information on Function Handles
...........
4-31
Function Arguments
...............................
4-33
Checking the Number of Input Arguments
.............
4-33
Passing Variable Numbers of Arguments
..............
4-35
Parsing Inputs with inputParser
.....................
4-37
Passing Optional Arguments to Nested Functions
.......
4-48
Returning Modified Input Arguments

.................
4-51
Calling Functions
..................................
4-53
What Happens When You Call a Function
.............
4-53
Determining Which Function Is Called
................
4-54
MATLAB Calling Syntax
...........................
4-57
Passing Certain Argument Types
....................
4-61
Passing Arguments in Structures or Cell Arrays
........
4-63
Assigning Output Arguments
........................
4-65
Calling External Functions
.........................
4-67
Running External Programs
........................
4-68
Types of Functions

5
Overv
iew of MATLAB Function Types
...............
5-2
Anon
ymous Functions
.............................
5-3
Cons
tructing an Anonymous Function
................
5-3
Arra
ys of Anonymous Functions
.....................
5-6
Outp
uts from Anonymous Functions
..................
5-7
Var
iables Used in the Expression
....................
5-8
xi
Examples of Anonymous Functions
...................
5-11
Primary M-File Functions

..........................
5-15
Nested Functions
..................................
5-16
Writing Nested Functions
...........................
5-16
Calling Nested Functions
...........................
5-17
Variable Scope in Nested Functions
...................
5-19
Using Function Handles with Nested Functions
........
5-21
Restrictions on Assigning to Variables
................
5-26
Examples of Nested Functions
.......................
5-27
Subfunctions
......................................
5-33
Calling Subfunctions
...............................
5-34
Accessing Help for a Subfunction

.....................
5-34
Private Functions
.................................
5-35
Private Directories
................................
5-35
Accessing Help for a Private Function
.................
5-36
Overloaded Functions
..............................
5-37
Class Directories
..................................
5-37
Data Import and Export
6
Overvi
ew
.........................................
6-3
File Ty
pes Supported by MATLAB
...................
6-3
Other M
ATLAB I/O Capabilities
.....................

6-5
Functi
ons Used in File Management
..................
6-7
Using
the Import Wizard
...........................
6-9
Start
ing the Import Wizard
.........................
6-9
Prev
iewing Contents of the File or Clipboard [Text only]
..
6-11
Spec
ifying Delimiters and Header Format [Text only]
....
6-12
Dete
rmining Assignment to Variables
.................
6-13
Auto
mated M-Code Generation
......................
6-16
Wri

ting Data to the Workspace
......................
6-1
9
xii
Contents
Supported File Formats
............................
6-21
Saving and Loading MAT-Files
......................
6-23
Exporting Data to MAT-Files
........................
6-23
Importing Data from MAT-Files
......................
6-30
Accessing Files with Memory-Mapping
..............
6-34
Overview of Memory-Mapping in MATLAB
............
6-34
The memmapfile Class
.............................
6-38
Constructing a memmapfile Object
...................
6-40

Reading a Mapped File
.............................
6-54
Writing to a Mapped File
...........................
6-59
Methods of the memmapfile Class
....................
6-67
Deleting a Memory Map
............................
6-69
Memory-Mapping Demo
............................
6-69
Importing Text Data
...............................
6-75
The MATLAB Import Wizard
........................
6-75
Using Import Functions with Text Data
...............
6-75
Importing Numeric Text Data
.......................
6-78
Importing Delimited ASCII Data Files
................
6-79

Importing Numeric Data with Text Headers
............
6-80
Importing Mixed Alphabetic and Numeric Data
.........
6-81
Importing from XML Documents
.....................
6-83
Exporting Text Data
...............................
6-84
Exporting Delimited ASCII Data Files
................
6-85
UsingthediaryFunctiontoExportData
..............
6-87
Exporting to XML Documents
.......................
6-88
Working with Graphics Files
........................
6-89
Getting Information About Graphics Files
.............
6-89
Importing Graphics Data
...........................
6-90

Exporting Graphics Data
...........................
6-90
Working with Audio and Video Data
.................
6-92
Getting Information About Audio/Video Files
...........
6-92
Importing Audio/Video Data
.........................
6-93
Exporting Audio/Video Data
.........................
6-94
Working with Spreadsheets
.........................
6-97
xiii
Microsoft Excel Spreadsheets
........................
6-97
Lotus 123 Spreadsheets
............................
6-100
Using Low-Level File I/O Functions
.................
6-103
Opening Files
.....................................

6-104
Reading Binary Data
..............................
6-106
Writing Binary Data
...............................
6-108
Controlling Position in a File
........................
6-108
Reading Strings Line by Line from Text Files
...........
6-110
Reading Formatted ASCII Data
......................
6-111
Writing Formatted Text Files
........................
6-113
Closing a File
.....................................
6-114
Exchanging Files over the Internet
..................
6-116
Downloading Web Content and Files
..................
6-116
Creating and Decompressing Zip Archives
.............

6-118
Sending E-Mail
...................................
6-119
Performing FTP File Operations
.....................
6-121
Working with Scientific Data Formats
7
Common Da
ta Format (CDF) Files
...................
7-2
Getting I
nformation About CDF Files
.................
7-2
Importi
ng Data from a CDF File
.....................
7-3
Exporti
ng Data to a CDF File
.......................
7-6
Flexib
le Image Transport System (FITS) Files
........
7-8
Gettin

g Information About FITS Files
................
7-8
Import
ing Data from a FITS File
.....................
7-9
Hiera
rchical Data Format (HDF5) Files
..............
7-11
Using
the MATLAB High-Level HDF5 Functions
.......
7-11
Using
the MATLAB Low-Level HDF5 Functions
........
7-26
Hier
archical Data Format (HDF4) Files
..............
7-35
Usin
gtheHDFImportTool
.........................
7-35
Usi
ng the HDF Import Tool Subsetting Options
.........

7-4
0
xiv
Contents
Using the MATLAB HDF4 High-Level Functions
.......
7-52
Using the HDF4 Low-Level Functions
................
7-55
Error Handling
8
Checking for Errors with try-catch
..................
8-2
Nested try-catch Blocks
............................
8-3
Handling and Recovering from an Error
.............
8-4
Reporting an Error
................................
8-4
Identifying the Cause
..............................
8-5
Regenerating an Error
.............................
8-8

Message Identifiers
................................
8-10
Identifier Format
..................................
8-10
Using Message Identifiers with lasterror
..............
8-11
Warnings
.........................................
8-14
Reporting a Warning
...............................
8-14
Identifying the Cause
..............................
8-15
Warning Control
...................................
8-16
Warning Statements
...............................
8-17
Warning Control Statements
........................
8-17
Output from Control Statements
.....................
8-19

Saving and Restoring State
.........................
8-22
Backtrace and Verbose Modes
.......................
8-23
Debugging Errors and Warnings
....................
8-26
Classes and Objects
9
Classes and Objects: An Overview
...................
9-2
xv
Features of Object-Oriented Programming
.............
9-3
MATLAB Data Class Hierarchy
......................
9-3
Creating Objects
..................................
9-4
Invoking Methods on Objects
........................
9-4
Private Methods
..................................
9-5

Helper Functions
..................................
9-6
Debugging Class Methods
..........................
9-6
Setting Up Class Directories
........................
9-6
Data Structure
...................................
9-7
Tips for C++ and Java Programmers
..................
9-8
Designing User Classes in MATLAB
.................
9-9
The MATLAB Canonical Class
.......................
9-9
The Class Constructor Method
.......................
9-10
Examples of Constructor Methods
....................
9-12
Identifying Objects Outside the Class Directory
.........
9-12

The display Method
................................
9-13
Accessing Object Data
..............................
9-13
The set and get Methods
............................
9-14
Indexed Reference Using subsref and subsasgn
.........
9-15
Handling Subscripted Reference
.....................
9-16
Handling Subscripted Assignment
....................
9-19
Object Indexing Within Methods
.....................
9-20
Defining end Indexing for an Object
..................
9-20
Indexing an Object with Another Object
...............
9-21
Converter Methods
................................
9-22

Overloading Operators and Functions
...............
9-23
Overloading Operators
.............................
9-23
Overloading Functions
.............................
9-25
Example — A Polynomial Class
.....................
9-26
Polynom Data Structure
............................
9-26
Polynom Methods
.................................
9-26
The Polynom Constructor Method
....................
9-27
Converter Methods for the Polynom Class
.............
9-28
The Polynom display Method
........................
9-30
The Polynom subsref Method
........................
9-31

Overloading Arithmetic Operators for polynom
.........
9-32
Overloading Functions for the Polynom Class
..........
9-34
Listing Class Methods
.............................
9-36
xvi
Contents
Building on Other Classes
..........................
9-38
Simple Inheritance
................................
9-38
Multiple Inheritance
...............................
9-40
Aggregation
......................................
9-40
Example — Assets and Asset Subclasses
.............
9-41
Inheritance Model for the Asset Class
.................
9-42
Asset Class Design

................................
9-42
Other Asset Methods
..............................
9-43
The Asset Constructor Method
.......................
9-43
The Asset get Method
..............................
9-44
The Asset set Method
..............................
9-45
The Asset subsref Method
..........................
9-46
The Asset subsasgn Method
.........................
9-47
The Asset display Method
...........................
9-48
The Asset fieldcount Method
........................
9-49
Designing the Stock Class
..........................
9-49
The Stock Constructor Method

.......................
9-50
The Stock get Method
..............................
9-52
The Stock set Method
..............................
9-53
The Stock subsref Method
..........................
9-54
The Stock subsasgn Method
.........................
9-55
The Stock display Method
...........................
9-57
Example — The Portfolio Container
.................
9-58
Designing the Portfolio Class
........................
9-58
The Portfolio Constructor Method
....................
9-59
The Portfolio display Method
........................
9-60
The Portfolio pie3 Method

...........................
9-61
Creating a Portfolio
................................
9-62
Saving and Loading Objects
........................
9-64
Modifying Objects During Save or Load
...............
9-64
Example — Defining saveobj and loadobj for
Portfolio
........................................
9-65
Summary of Code Changes
..........................
9-65
The saveobj Method
...............................
9-66
The loadobj Method
................................
9-66
Changing the Portfolio Constructor
...................
9-67
The Portfolio subsref Method
........................
9-68

xvii
Object Precedence
.................................
9-70
Specifying Precedence of User-Defined Classes
.........
9-70
How MATLAB Determines Which Method to Call
.....
9-72
Selecting a Method
................................
9-72
Querying Which Method MATLAB Will Call
...........
9-75
Scheduling Program Execution with Timers
10
Using a MATLAB Timer Object
......................
10-3
Example: Displaying a Message
.....................
10-4
Creating Timer Objects
............................
10-5
Timer Object Naming
..............................
10-6

Working with Timer Object Properties
..............
10-7
Retrieving the Value of Timer Object Properties
........
10-7
Setting the Value of Timer Object Properties
...........
10-8
Starting and Stopping Timers
......................
10-10
Starting a Timer
..................................
10-10
Starting a Timer at a Specified Time
..................
10-11
Stopping Timer Objects
............................
10-11
Blocking the MATLAB Command Line
................
10-12
Creating and Executing Callback Functions
.........
10-14
Associating Commands with Timer Object Events
.......
10-14

Creating Callback Functions
........................
10-15
Specifying the Value of Callback Function Properties
....
10-17
Timer Object Execution Modes
......................
10-19
Executing a Timer Callback Function Once
............
10-19
Executing a Timer Callback Function Multiple Times
....
10-20
Handling Callback Function Queuing Conflicts
.........
10-21
Deleting Timer Objects from Memory
...............
10-23
xviii
Contents
Testing the Validity of a Timer Object
.................
10-23
Deleting All Existing Timer Objects
..................
10-23
Finding All Timer Objects in Memory

................
10-24
Finding Invisible Timer Objects
......................
10-24
Improving Performance and Memory Usage
11
Analyzing Your
Program’s Performance
.............
11-2
The M-File Pro
filer Utility
..........................
11-2
Stopwatch Tim
er Functions
.........................
11-2
Techniques f
or Improving Performance
.............
11-4
Multithread
edComputationinMATLAB
..............
11-4
Vectorizin
gLoops
.................................

11-4
Preallocat
ing Arrays
...............................
11-7
Coding Loop
sinaMEX-File
.........................
11-9
Assigning
to Variables
.............................
11-9
Operating
on Real Data
............................
11-10
Using Appr
opriate Logical Operators
.................
11-10
Overload
ing Built-In Functions
......................
11-11
Function
s Are Generally Faster Than Scripts
...........
11-11
Load and S

ave Are Faster Than File I/O Functions
......
11-11
Avoid La
rge Background Processes
...................
11-11
Using Me
mory Efficiently
..........................
11-12
Memory
Allocation for Arrays
.......................
11-12
Data St
ructures and Memory
........................
11-16
Memory
Management Functions
.....................
11-17
Strate
gies for Efficient Use of Memory
................
11-18
Resol
ving “Out of Memory” Errors
..................

11-21
Gener
al Suggestions for Reclaiming Memory
...........
11-21
Comp
ressing Data in Memory
.......................
11-2
2
Incr
easing System Swap Space
......................
11-2
2
Free
ing Up System Resources on Windows Systems
.....
11-2
3
Relo
ading Variables on UNIX Systems
................
11-2
3
xix
Programming Tips
12
Command and Function Syntax
.....................

12-3
Syntax Help
......................................
12-3
Command and Function Syntaxes
....................
12-3
Command Line Continuation
........................
12-3
Completing Commands Using the Tab Key
.............
12-4
Recalling Commands
..............................
12-4
Clearing Commands
...............................
12-5
Suppressing Output to the Screen
....................
12-5
Help
..............................................
12-6
Using the Help Browser
............................
12-6
Help on Functions from the Help Browser
.............

12-7
Help on Functions from the Command Window
.........
12-7
Topical Help
......................................
12-7
Paged Output
.....................................
12-8
Writing Your Own Help
............................
12-9
Help for Subfunctions and Private Functions
...........
12-9
Help for Methods and Overloaded Functions
...........
12-9
Development Environment
.........................
12-11
Workspace Browser
................................
12-11
Using the Find and Replace Utility
...................
12-11
Commenting Out a Block of Code
....................

12-12
Creating M-Files from Command History
..............
12-12
Editing M-Files in EMACS
..........................
12-12
M-File Functions
..................................
12-13
M-File Structure
..................................
12-13
Using Lowercase for Function Names
.................
12-13
Getting a Function’s Name and Path
..................
12-14
What M-Files Does a Function Use?
..................
12-14
Dependent Functions, Built-Ins, Classes
...............
12-15
Function Arguments
...............................
12-16
Getting the Input and Output Arguments
.............

12-16
Variable Numbers of Arguments
.....................
12-16
String or Numeric Arguments
.......................
12-17
Passing Arguments in a Structure
....................
12-17
xx
Contents
Passing Arguments in a Cell Array
...................
12-18
Program Development
.............................
12-19
Planning the Program
..............................
12-19
Using Pseudo-Code
................................
12-19
Selecting the Right Data Structures
..................
12-19
General Coding Practices
...........................
12-20

Naming a Function Uniquely
........................
12-20
The Importance of Comments
.......................
12-20
Coding in Steps
...................................
12-21
Making Modifications in Steps
.......................
12-21
Functions with One Calling Function
.................
12-21
Testing the Final Program
..........................
12-21
Debugging
........................................
12-22
The MATLAB Debug Functions
......................
12-22
More Debug Functions
.............................
12-22
The MATLAB Graphical Debugger
...................
12-23

A Quick Way to Examine Variables
...................
12-23
Setting Breakpoints from the Command Line
..........
12-24
Finding Line Numbers to Set Breakpoints
.............
12-24
Stopping Execution on an Error or Warning
............
12-24
Locating an Error from the Error Message
.............
12-24
Using Warnings to Help Debug
......................
12-25
Making Code Execution Visible
......................
12-25
Debugging Scripts
.................................
12-25
Variables
..........................................
12-26
Rules for Variable Names
...........................
12-26

Making Sure Variable Names Are Valid
...............
12-26
Don’t Use Function Names for Variables
...............
12-27
Checking for Reserved Keywords
.....................
12-27
Avoid Using i and j for Variables
.....................
12-28
Avoid Overwriting Variables in Scripts
................
12-28
Persistent Variables
...............................
12-28
Protecting Persistent Variables
......................
12-28
Global Variables
..................................
12-29
Strings
...........................................
12-30
Creating Strings with Concatenation
.................
12-30

Comparing Methods of Concatenation
.................
12-30
Store Arrays of Strings in a Cell Array
................
12-31
xxi
Converting Between Strings and Cell Arrays
...........
12-31
Search and Replace Using Regular Expressions
.........
12-31
Evaluating Expressions
............................
12-33
Find Alternatives to Using eval
......................
12-33
Assigning to a Series of Variables
....................
12-33
Short-Circuit Logical Operators
......................
12-33
Changing the Counter Variable within a for Loop
.......
12-34
MATLAB Path
.....................................

12-35
Precedence Rules
..................................
12-35
File Precedence
...................................
12-36
Adding a Directory to the Search Path
................
12-36
Handles to Functions Not on the Path
.................
12-36
Making Toolbox File Changes Visible to MATLAB
.......
12-37
Making Nontoolbox File Changes Visible to MATLAB
....
12-38
Change Notification on Windows
.....................
12-38
Program Control
..................................
12-39
Using break, continue, and return
....................
12-39
Using switch Versus if
..............................

12-40
MATLAB case Evaluates Strings
.....................
12-40
Multiple Conditions in a case Statement
...............
12-40
Implicit Break in switch-case
........................
12-40
Variable Scope in a switch
..........................
12-41
Catching Errors with try-catch
......................
12-41
Nested try-catch Blocks
............................
12-42
Forcing an Early Return from a Function
..............
12-42
Save and Load
.....................................
12-43
Saving Data from the Workspace
.....................
12-43
Loading Data into the Workspace
....................

12-43
Viewing Variables in a MAT-File
.....................
12-44
Appending to a MAT-File
...........................
12-44
SaveandLoadonStartuporQuit
....................
12-45
Saving to an ASCII File
............................
12-45
Files and Filenames
................................
12-46
Naming M-files
...................................
12-46
Naming Other Files
...............................
12-46
Passing Filenames as Arguments
....................
12-47
Passing Filenames to ASCII Files
....................
12-47
xxii
Contents

Determining Filenames at Run-Time
.................
12-47
Returning the Size of a File
.........................
12-47
Input/Output
......................................
12-49
File I/O Function Overview
.........................
12-49
Common I/O Functions
.............................
12-49
Readable File Formats
.............................
12-50
Using the Import Wizard
...........................
12-50
Loading Mixed Format Data
........................
12-50
Reading Files with Different Formats
.................
12-51
Reading ASCII Data into a Cell Array
.................
12-51

Interactive Input into Your Program
..................
12-51
Starting MATLAB
..................................
12-52
Getting MATLAB to Start Up Faster
..................
12-52
Operating System Compatibility
....................
12-53
Executing O/S Commands from MATLAB
.............
12-53
Searching Text with grep
...........................
12-53
Constructing Paths and Filenames
...................
12-53
Finding the MATLAB Root Directory
.................
12-54
Temporary Directories and Filenames
.................
12-54
Demos
............................................
12-55

Demos Available with MATLAB
.....................
12-55
For More Information
..............................
12-56
Current CSSM
....................................
12-56
Archived CSSM
...................................
12-56
MATLAB Technical Support
.........................
12-56
Tech Notes
.......................................
12-56
MATLAB Central
.................................
12-56
MATLAB Newsletters (Digest, News & Notes)
..........
12-56
MATLAB Documentation
...........................
12-56
MATLAB Index of Examples
........................
12-56

xxiii
External Interfaces
A
Finding the Documentation in Online Help
..........
A-1
Index
xxiv
Contents
1
Data Structures
The most basic data structure in MATLAB
®
is the matrix: a two-dimensional,
rectangularly shaped data structure capable of storing multiple elements of
data in an easily accessible format. These data elements can be numbers,
characters, logical states of
true
or
false
, or even other MATLAB structure
types. MATLAB uses these two-dimensional matrices to store single numbers
and linear series of numbers as well. In these cases, the dimensions are 1-by-1
and 1-by-n respectively, where
n
is the length of the numeric series. MATLAB
also supports data structures that have more than two dimensions. These
data structures are referred to as arrays in the MATLAB documentation.
Creating and Concatenating
Matrices (p. 1-3)

Create a matrix or construct one
from other matrices.
Matrix Indexing (p. 1-18)
Access or assign to elements of a
matrix using methods of row and
column indexing.
Getting Information About a Matrix
(p. 1-24)
Retrieve information about the
structure or contents of a matrix.
Resizing and Reshaping Matrices
(p. 1-27)
Change the size, shape, or
arrangement of elements in an
existing matrix.
Shifting and Sorting Matrices
(p. 1-37)
Shift matrix elements along one or
more dimensions, or sort them into
an ascending or descending order.
Operating on Diagonal Matrices
(p. 1-42)
Construct and manipulate matrices
along a diagonal of the rectangular
shape.

×