You are on page 1of 11

MATLAB

and Beyond

BASICS OF

c 2000 by CRC Press LLC

MATLAB
and Beyond
Andrew Knight

BASICS OF

CHAPMAN & HALL/CRC


Boca Raton London New York Washington, D.C.

Library of Congress Cataloging-in-Publication Data


Knight, Andrew (Andrew James), 1961 Basics of MATLAB and beyond / Andrew Knight. p. cm. Includes index. (alk. paper) ISBNO-8493-2039-9 1. Engitteering mathematics-Data prccessing. 2. MATLAB. TA345.K63 1999 620.0015 118-dc2 1

I. Title. 99-31210 CIP

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.

Visit tbe CRC Press Web site at www.crcpress.com


2000 by CRC Press LLC No claim to original U.S. Government works International Standard Bcok Number 0-8493-2039-9 Library of Congress Card Number 99-31210 Printed in the United States of America 3 4 5 6 1 8 9 0 Printed on acid-free paper MATLAB is a registered trademark of The MathWorks, Inc.

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

c 2000 by CRC Press LLC

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

c 2000 by CRC Press LLC

About the Author


The author completed a Ph.D. in plasma physics at the Flinders University of South Australia in the days before matlab. Consequently, he knows how much time can be saved when you dont have to write your own matrix inversion or polynomial evaluation routines in fortran. His rst exposure to matlab was at the Centre for Plasma Physics Research at the Swiss Federal Institute of Technology (Ecole Polytechnique Fdrale) in Lausanne, Switzerland, where he continued his e e research in plasma physics. On his return to Australia to take up a position with the Maritime Operations Division of the Defence Science and Technology Organisation, he was given responsibility for research in the ow noise problem of towed sonar arrays. His current research interests include sonar signal processing and information displays. He has been largely responsible for the growth in the use of matlab in his division, and has conducted training courses in matlab.

c 2000 by CRC Press LLC

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

c 2000 by CRC Press LLC

4.6 4.7 4.8

Three-Dimensional Plots Axes Labels

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

c 2000 by CRC Press LLC

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

II Beyond the Basics


25 Sparse Arrays 25.1 Example: Airfoil 25.2 Example: Communication Network 26 Text Strings 26.1 String Matrices 26.2 Comparing Strings 26.3 String Manipulations 26.4 Converting Numbers to Strings 26.5 Using Strings as Commands 27 Cell Arrays 28 Structures 28.1 Example: Meteorological Database 28.2 Example: Capturing the List of Variables 29 Multidimensional Arrays 29.1 Generating Multidimensional Grids 29.2 Operations with Multidimensional Arrays 29.3 RGB Images 29.4 Example: Sonar 29.5 Multidimensional Cell Arrays 29.6 Multidimensional Structures 30 Saving and Loading Data 30.1 MATLAB Format 30.2 Other Formats

c 2000 by CRC Press LLC

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

c 2000 by CRC Press LLC

39 Answers to Exercises (Part I) 40 Answers to Exercises (Part II)

c 2000 by CRC Press LLC

You might also like