MATLAB®
Primer
R2016a
How to Contact MathWorks
Latest news:
Sales and services:
User community:
Technical support:
Phone:
www.mathworks.com
www.mathworks.com/sales_and_services
www.mathworks.com/matlabcentral
www.mathworks.com/support/contact_us
508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
MATLAB® Primer
© 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
December 1996
First printing
May 1997
Second printing
September 1998 Third printing
September 2000 Fourth printing
June 2001
July 2002
August 2002
June 2004
October 2004
March 2005
June 2005
Online only
Online only
Fifth printing
Sixth printing
Online only
Online only
Seventh printing
September 2005 Online only
March 2006
Online only
September 2006 Eighth printing
March 2007
Ninth printing
September 2007 Tenth printing
March 2008
Eleventh printing
October 2008
Twelfth printing
March 2009
Thirteenth printing
September 2009 Fourteenth printing
March 2010
Fifteenth printing
September 2010
April 2011
September 2011
March 2012
Sixteenth printing
Online only
Seventeenth printing
Eighteenth printing
September 2012 Nineteenth printing
March 2013
Twentieth printing
September 2013 Twenty-first printing
March 2014
October 2014
March 2015
September 2015 Twenty-fifth printing
March 2016
Twenty-sixth printing
Twenty-second printing
Twenty-third printing
Twenty-fourth printing
For MATLAB 5
For MATLAB 5.1
For MATLAB 5.3
Revised for MATLAB 6 (Release 12)
Revised for MATLAB 6.1 (Release 12.1)
Revised for MATLAB 6.5 (Release 13)
Revised for MATLAB 6.5
Revised for MATLAB 7.0 (Release 14)
Revised for MATLAB 7.0.1 (Release 14SP1)
Revised for MATLAB 7.0.4 (Release 14SP2)
Minor revision for MATLAB 7.0.4 (Release
14SP2)
Minor revision for MATLAB 7.1 (Release
14SP3)
Minor revision for MATLAB 7.2 (Release
2006a)
Minor revision for MATLAB 7.3 (Release
2006b)
Minor revision for MATLAB 7.4 (Release
2007a)
Minor revision for MATLAB 7.5 (Release
2007b)
Minor revision for MATLAB 7.6 (Release
2008a)
Minor revision for MATLAB 7.7 (Release
2008b)
Minor revision for MATLAB 7.8 (Release
2009a)
Minor revision for MATLAB 7.9 (Release
2009b)
Minor revision for MATLAB 7.10 (Release
2010a)
Revised for MATLAB 7.11 (R2010b)
Revised for MATLAB 7.12 (R2011a)
Revised for MATLAB 7.13 (R2011b)
Revised for Version 7.14 (R2012a)
(Renamed from MATLAB Getting Started
Guide)
Revised for Version 8.0 (R2012b)
Revised for Version 8.1 (R2013a)
Revised for Version 8.2 (R2013b)
Revised for Version 8.3 (R2014a)
Revised for Version 8.4 (R2014b)
Revised for Version 8.5 (R2015a)
Revised for Version 8.6 (R2015b)
Revised for Version 9.0 (R2016a)
Contents
Quick Start
1
MATLAB Product Description . . . . . . . . . . . . . . . . . . . . . . . . .
Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Desktop Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Array Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Workspace Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-D and 3-D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Line Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Programming and Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sample Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Loops and Conditional Statements . . . . . . . . . . . . . . . . . . .
Script Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Help and Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-2
1-3
1-5
1-10
1-13
1-15
1-17
1-19
1-19
1-23
1-25
1-27
1-27
1-28
1-30
1-31
v
2
Language Fundamentals
Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . .
About Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sum, transpose, and diag . . . . . . . . . . . . . . . . . . . . . . . . . . .
The magic Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrix Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Array Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Examples of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The format Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Suppressing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering Long Statements . . . . . . . . . . . . . . . . . . . . . . . . .
Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Subscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Colon Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . .
Scalar Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Logical Subscripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The find Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-2
2-2
2-4
2-5
2-7
2-8
2-9
2-9
2-10
2-11
2-11
2-13
2-14
2-16
2-16
2-17
2-17
2-18
2-19
2-19
2-20
2-21
2-22
2-22
2-23
2-24
2-26
2-26
2-28
2-30
2-33
vi
Contents
3
4
Mathematics
Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrices in the MATLAB Environment . . . . . . . . . . . . . . . . .
Systems of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . .
Inverses and Determinants . . . . . . . . . . . . . . . . . . . . . . . . .
Factorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Powers and Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . .
Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Singular Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operations on Nonlinear Functions . . . . . . . . . . . . . . . . . . .
Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preprocessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Visualizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modeling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-2
3-2
3-10
3-21
3-25
3-32
3-35
3-38
3-43
3-43
3-43
3-46
3-47
3-47
3-47
3-53
3-57
3-68
Graphics
Basic Plotting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Plotting Multiple Data Sets in One Graph . . . . . . . . . . . . . .
Specifying Line Styles and Colors . . . . . . . . . . . . . . . . . . . . .
Plotting Lines and Markers . . . . . . . . . . . . . . . . . . . . . . . . .
Graphing Imaginary and Complex Data . . . . . . . . . . . . . . . .
Adding Plots to an Existing Graph . . . . . . . . . . . . . . . . . . .
Figure Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Multiple Plots in One Figure . . . . . . . . . . . . . . .
Controlling the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Axis Labels and Titles . . . . . . . . . . . . . . . . . . . . . .
4-2
4-2
4-4
4-6
4-7
4-9
4-10
4-12
4-13
4-14
4-16
vii
Saving Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Workspace Data . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Mesh and Surface Plots . . . . . . . . . . . . . . . . . . . . .
About Mesh and Surface Plots . . . . . . . . . . . . . . . . . . . . . .
Visualizing Functions of Two Variables . . . . . . . . . . . . . . . .
Display Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Image Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading and Writing Images . . . . . . . . . . . . . . . . . . . . . . . .
Printing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Printing from the File Menu . . . . . . . . . . . . . . . . . . . . . . . .
Exporting the Figure to a Graphics File . . . . . . . . . . . . . . .
Using the Print Command . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Graphics Objects . . . . . . . . . . . . . . . . . . . . . . .
Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions for Working with Objects . . . . . . . . . . . . . . . . . .
Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding the Handles of Existing Objects . . . . . . . . . . . . . . .
4-17
4-18
4-19
4-19
4-19
4-25
4-25
4-27
4-28
4-28
4-28
4-28
4-29
4-31
4-31
4-34
4-36
4-37
4-38
5
Programming
Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Conditional Control — if, else, switch . . . . . . . . . . . . . . . . . .
Loop Control — for, while, continue, break . . . . . . . . . . . . . .
Program Termination — return . . . . . . . . . . . . . . . . . . . . . .
Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-2
5-2
5-5
5-7
5-7
5-8
5-9
5-9
5-10
5-11
5-12
5-14
viii
Contents