Sunteți pe pagina 1din 1218

MATLAB ® Programming Fundamentals

R2016b

How to Contact MathWorks

How to Contact MathWorks L a t e s t n e w s : S

Latest news:

Sales and services:

User community:

Technical support:

t y : T e c h n i c a l s u p p

Phone:

508-647-7000

The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098www.mathworks.com/support/contact_us 508-647-7000 MATLAB Programming Fundamentals © COPYRIGHT 1984–2016 by

MATLAB Programming Fundamentals © COPYRIGHT 1984–2016 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 and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

Patents

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 2006a)

September 2006

Online only

Revised for MATLAB 7.3 (Release 2006b)

March 2007

Online only

Revised for MATLAB 7.4 (Release 2007a)

September 2007

Online only

Revised for Version 7.5 (Release 2007b)

March 2008

Online only

Revised for Version 7.6 (Release 2008a)

October 2008

Online only

Revised for Version 7.7 (Release 2008b)

March 2009

Online only

Revised for Version 7.8 (Release 2009a)

September 2009

Online only

Revised for Version 7.9 (Release 2009b)

March 2010

Online only

Revised for Version 7.10 (Release 2010a)

September 2010

Online only

Revised for Version 7.11 (Release 2010b)

April 2011

Online only

Revised for Version 7.12 (Release 2011a)

September 2011

Online only

Revised for Version 7.13 (Release 2011b)

March 2012

Online only

Revised for Version 7.14 (Release 2012a)

September 2012

Online only

Revised for Version 8.0 (Release 2012b)

March 2013

Online only

Revised for Version 8.1 (Release 2013a)

September 2013

Online only

Revised for Version 8.2 (Release 2013b)

March 2014

Online only

Revised for Version 8.3 (Release 2014a)

October 2014

Online only

Revised for Version 8.4 (Release 2014b)

March 2015

Online only

Revised for Version 8.5 (Release 2015a)

September 2015

Online only

Revised for Version 8.6 (Release 2015b)

October 2015

Online only

Rereleased for Version 8.5.1 (Release

 

2015aSP1)

March 2016

Online only

Revised for Version 9.0 (Release 2016a)

September 2016

Online only

Revised for Version 9.1 (Release 2016b)

Language

1

Contents

Syntax Basics

Language 1 Contents Syntax Basics Continue Long Statements on Multiple Lines   . . . .

Continue Long Statements on Multiple Lines

 

.

.

.

.

.

.

.

.

1-2

Call Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-3

Ignore Function Outputs

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-4

Variable Names

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-5

Valid Names

Conflicts with Function Names

.

.

 

.

.

 

.

.

 

.

.

 

.

.

 

.

.

 

.

.

.

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-5

1-5

Case and Space Sensitivity

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-7

Command vs. Function Syntax

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-9

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-9

Command and Function Syntaxes Avoid Common Syntax Mistakes

. How MATLAB Recognizes Command Syntax

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-10

1-11

Common Errors When Calling Functions

 

1-13

Conflicting Function and Variable Names

 

.

.

 

.

.

.

.

.

.

.

.

1-13

Undefined Functions or Variables

.

 

.

 

.

 

.

 

.

 

.

 

.

.

.

.

.

.

.

.

.

.

1-13

2

Program Components

2 Program Components Array vs. Matrix Operations Introduction . . . . . . . .

Array vs. Matrix Operations

Introduction

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-2

2-2

. Array Operations

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-2

Matrix Operations

.

.

 

.

 

.

 

.

 

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-5

Compatible Array Sizes for Basic Operations

 

2-8

Inputs with Compatible Sizes

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-8

Inputs with Incompatible Sizes

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-11

Examples

.

.

.

.

.

.

.

.

.

.

.

.

 

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-12

Array Comparison with Relational Operators

 

2-14

Array Comparison

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-14

Logic Statements

.

.

.

 

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-17

Operator Precedence

. Precedence of AND and OR Operators

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-18

2-18

Overriding Default Precedence

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-19

Average Similar Data Points Using a Tolerance

 

2-20

Group Scattered Data Using a Tolerance

.

.

.

.

.

.

.

.

.

.

.

2-23

Special Values

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-26

Conditional Statements

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-28

Loop Control Statements

 

.

 

.

 

.

 

.

 

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-30

Regular Expressions

.

. What Is a Regular Expression?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-32

2-32

Steps for Building Expressions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-34

Operators and Characters

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-37

Lookahead Assertions in Regular Expressions

 

2-47

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-47

Lookahead Assertions Overlapping Matches

. Logical AND Conditions

.

 

.

.

 

.

.

 

.

.

 

.

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-47

2-48

Tokens in Regular Expressions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-50

Introduction

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-50

. Unmatched Tokens

.

Multiple Tokens

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-51

2-52

Tokens in Replacement Text

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-53

Named Capture

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-54

Dynamic Regular Expressions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-56

. Dynamic Match Expressions — (??expr)

Introduction

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-56

2-57

Commands That Modify the Match Expression — (??

 

@cmd)

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-58

Commands That Serve a Functional Purpose — (?

 

@cmd)

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. Commands in Replacement Expressions — ${cmd}

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Comma-Separated Lists

.

.

. What Is a Comma-Separated List?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-59

2-61

2-64

2-64

. Generating a Comma-Separated List

.

.

.

.

.

.

.

.

.

.

.

.

.

2-64

Assigning Output from a Comma-Separated List

 

2-66

Assigning to a Comma-Separated List

.

.

.

.

.

.

.

.

.

.

.

.

.

2-67

How to Use the Comma-Separated Lists

.

.

.

.

.

.

.

.

.

.

.

2-69

Fast Fourier Transform Example

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-71

Alternatives to the eval Function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-73

. Variables with Sequential Names

Why Avoid the eval Function?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-73

2-73

Files with Sequential Names

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-74

Function Names in Variables

.

.

.

.

.

.

.

.

.

.

.

.

.

2-75

Field Names in Variables

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-75

Error Handling

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-76

Symbol Reference

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

 

.

.

.

.

2-77

Asterisk — *

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-77

At — @

. Comma — ,

.

. Colon — :

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-78

2-79

2-80

. Curly Braces — { }

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

 

.

.

.

.

2-80

Dot — .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-81

Dot-Dot —

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-81

Dot-Dot-Dot (Ellipsis) —

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-82

Dot-Parentheses — .( )

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-83

Exclamation Point — !

.

.

.

.

.

.

.

.

 

.

.

.

.

2-83

Parentheses — ( )

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-83

Percent — %

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-84

Percent-Brace — %{ %}

Plus — +

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-84

2-85

. Semicolon — ;

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-85

Single Quotes — ' '

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-86

Space Character

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-86

Slash and Backslash — / \

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-87

Square Brackets — [ ]

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-87

Tilde — ~

.

.

.

.

.

.

.