Professional Documents
Culture Documents
and Beyond
BASICS OF
MATLAB
and Beyond
Andrew Knight
BASICS OF
This book contains information obtained from authentic and highly regarded sources. Reprinted material is quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts have hen made to publish reliable data and information, but the author and the publisher cannot assume responsibility for the validity of all materials or for the consequences of their use. Neither this book nor any part may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, microtihning, and recording, or by any information storage or retrieval system, without prior permission in writing from the publisher. The consent of CRC Press LLC does not extend to copying for general distribution, for promotion, for creating new works, or for resale. Specific permission must be obtained in writing from CRC Press LLC for such copying. Direct all inquiries to CRC Press LLC, 2000 N.W. Corporate Blvd., Boca Raton, Plorida 33431. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation, without intent to infringe.
Preface
This book arose from notes written for matlab R training courses run within the Australian Defence Science and Technology Organisation. The book is in two parts. Each part was originally a two-day course, designed assuming that students were seated at a computer with matlab running. Part 1 is an introductory course suitable for those with no experience at all with matlab. It is written in a self contained way; if you go through the notes, all the new commands and ideas are explained as they are introduced. Part 2 is a more advanced course suitable for those who are already familiar with the basics of matlab. It covers a variety of topics, some of which you may not be interested in; if so, you should be able to skip that section without detriment to other sections. You can get the m-les that accompany this book from the Download section of the CRC Press web site (www.crcpress.com). The les are available in zip or gzipped tar format, and can be extracted using WinZip on a PC, or by using gunzip and tar on unix. You will need to put them in a directory where matlab will be able to nd them. You can either use the cd command to move matlabs working directory to the directory you extract the les to, or add that directory to matlabs search path. (You can display matlabs current working directory by
matlab is a registered trademark of The MathWorks, Inc. For product information, please contact: The MathWorks, Inc. 24 Prime Park Way Natick, MA 01760-1500 USA Tel: Fax: E-mail: Web: 508-647-7000 508-647-7107 info@mathworks.com www.mathworks.com
typing pwd (print working directory) in the command window.) On a PC or Macintosh, you can add directories to matlabs path by clicking on the path browser button at the top of the matlab command window (it is the button with two folders on it to the left of the question mark button). In the path browser, select the menu PathAdd to path, then select the directory containing the extracted les using the browse button (on PCs it is the one with three dots on it), then check the add to back option before pressing OK. Then click FileSave Path before you exit the path browser. If you are using another platform you can use the path command from within matlab (type help path for instructions). You can install this path each time you start matlab by putting an appropriate path command in a le called startup.m in a directory called matlab situated immediately below your home directory. Many of the graphical examples in this book assume that the gure window is empty. To ensure an empty gure window issue the command: clf which stands for clear gure. If you nd that the gure window is obscured by your command window, try shrinking both windows. Or you can type: shg (show graphic) to bring the graphics window to the front. The companion software implements an even shorter abbreviation; type s to bring the graphics window to the front If, on a PC or Macintosh, the gure window is at the front of the screen, or if it has the current focus, just start typing and matlab will switch to the command window and accept your typing. Words appearing in this book in typewriter font, for example, type, represent matlab commands that you can type in, or output produced by matlab. Andrew Knight
Contents
I Basics of MATLAB
1 First Steps in MATLAB 1.1 Starting MATLAB 1.2 First Steps 1.3 Matrices 1.4 Variables 1.5 The Colon Operator 1.6 Linspace 1.7 Plotting Vectors 2 Typing into MATLAB 2.1 Command Line Editing 2.2 Smart Recall 2.3 Long Lines 2.4 Copying and Pasting 3 Matrices 3.1 Typing Matrices 3.2 Concatenating Matrices 3.3 Useful Matrix Generators 3.4 Subscripting 3.5 End as a subscript 3.6 Deleting Rows or Columns 3.7 Matrix Arithmetic 3.8 Transpose 4 Basic Graphics 4.1 Plotting Many Lines 4.2 Adding Plots 4.3 Plotting Matrices 4.4 Clearing the Figure Window 4.5 Subplots
5 More Matrix Algebra 6 Basic Data Analysis 7 Graphics of Functions of Two Variables 7.1 Basic Plots 7.2 Colour Maps 7.3 Colour Bar 7.4 Good and Bad Colour Maps 7.5 Extracting Logical Domains 7.6 Nonrectangular Surface Domains 8 M-Files 8.1 Scripts 8.2 Functions 8.3 Flow Control 8.4 Comparing Strings 9 Data Files 9.1 MATLAB Format 9.2 ASCII Format 9.3 Other Formats 10 Directories 11 Startup 12 Using MATLAB on Dierent Platforms 13 Log Scales 14 Curve FittingMatrix Division 15 Missing Data 16 Polar Plots 17 Fourier Transform 18 Power Spectrum 19 Sounds in MATLAB
20 Time-Frequency Analysis 21 Line Animation 22 SPTool 23 Handle Graphics 23.1 Custom Plotting Functions 23.2 Set and Get 23.3 Graphical Object Hierarchy 24 Demos
31 Handle Graphics 31.1 Get and Set 31.2 Default Object Properties 31.3 Current Objects 32 Axes Eects 32.1 The Axis Command 32.2 Tick Marks and Labels 32.3 Subplots 32.4 Double Axes 32.5 Axes Labels 33 Text in Graphics 33.1 Symbols and Greek Letters 33.2 Symbols in Tick Labels 33.3 Global Object Placement 34 Graphical User Interfaces 34.1 Callbacks 34.2 UIControls 34.3 Exclusive Radio Buttons 34.4 Variables in GUIs 34.5 The Tag Property 34.6 UIMenus 34.7 Fast Drawing 34.8 Guide 34.9 Other Aids 35 Printing Graphics 35.1 Print Size: Orient 35.2 Print Size: WYSIWYG 35.3 Including Figures in Other Applications 36 Irregular Grids 36.1 Interpolation over a Rectangular Grid 36.2 Triangular Gridding 37 Three-dimensional Modelling 37.1 Patches 37.2 Light Objects 38 MATLAB Programming 38.1 Vectorising Code 38.2 M-File Subfunctions 38.3 Debugging 38.4 Proler