Appendix A.pdf

(147 KB) Pobierz
Appendix A
Introduction to M
ATLAB
A.1 What Is M
ATLAB
?
MATLAB is a technical computing environment developed by The Math-
Works, Inc. for computation and data visualization. It is both an interactive
system and a programming language, whose basic data element is an array:
scalar, vector, matrix or multi-dimensional array. Besides basic array opera-
tions, it offers programming features similar to those of other computing lan-
guages (e.g., functions, control flow, etc.).
In this appendix, we provide a brief summary of MATLAB to help the
reader understand the algorithms in the text. We do not claim that this intro-
duction is complete, and we urge the reader to learn more about MATLAB
from other sources. The documentation that comes with MATLAB is excel-
lent, and the reader should find the tutorials helpful. For a comprehensive
overview of MATLAB, we also recommend Hanselman and Littlefield [1998,
2001]. If the reader needs to understand more about the graphics and GUI
capabilities in MATLAB, Marchand [1999] is the one to use.
MATLAB will execute on Windows, UNIX, and Linux systems. Here we
focus on the Windows version, but most of the information applies to all sys-
tems. The main MATLAB software package contains many functions for ana-
lyzing data. There are also specialty toolboxes extending the capabilities of
MATLAB that are available from The MathWorks and third party vendors.
Some toolboxes are also on the internet for free downloading. For more infor-
mation on these toolboxes, see
http://www.mathworks.com.
In this text, we use the latest releases of MATLAB (Version 6) and the Sta-
tistics Toolbox (Version 3). However, most of the following discussion applies
to all versions of MATLAB. We alert the reader to places where they differ.
We assume that readers know how to start MATLAB for their particular
platform. When MATLAB is started, you will have a command window with
a prompt where you can enter commands. In MATLAB 6, other windows
come up (help window, history window, etc.), but we do not cover those here.
© 2002 by Chapman & Hall/CRC
512
Computational Statistics Handbook with M
ATLAB
A.2 Getting Help in M
ATLAB
One useful and important aspect of MATLAB is the
Help
feature. There are
many ways to get information about a MATLAB function. Not only does the
Help
provide information about the function, but it also gives references for
other related functions. We discuss below the various ways to get help in
MATLAB.
Command Line:
Typing
help
and then the function name at the
command line will, in most cases, tell you everything you need to
know about the function. In this text, we do not write about all the
capabilities or uses of a function. The reader is strongly encouraged
to use command line
help
to find out more. As an example, typing
help plot
at the command line provides lots of useful information
about the basic
plot
function. Note that the command line
help
works with the Computational Statistics Toolbox as well.
Help Menu:
The
help
files can also be accessed via the usual
Help
menu. This opens up a separate
help
window. Information can be
obtained by clicking on links or searching the index (Version 6). In
MATLAB 5, you can get a similar window by accessing the
Help
Desk
via the
Help
menu.
A.3 File and Workspace Management
We can enter commands interactively at the command line or save them in an
M-file. So, it is important to know some commands for file management. The
commands shown in
Table A.1
can be used to list, view and delete files.
MATLAB remembers the commands that you enter and all of the values of
any variable you create for that session. These variables live in the MATLAB
workspace.
You can recall the variable at any time by typing in the variable
name with no punctuation at the end. Note that MATLAB is case sensitive, so
Temp, temp,
and
TEMP
represent different variables.
In MATLAB 6, there is a separate command history window. The arrow
keys can be used in all versions of MATLAB to recall and edit commands. The
up-arrow and down-arrow keys scroll through the commands. The left and
right arrows move through the present command. By using these keys, the
user can recall commands and edit them using common editing keystrokes.
We can view the contents of the current workspace using the
Workspace
Browser.
This is accessed through the
File
menu or the toolbar. All vari-
ables in the workspace are listed in the window. The variables can be viewed
© 2002 by Chapman & Hall/CRC
Appendix A:
Introduction to M
ATLAB
513
File Management Commands
Command
dir, ls
delete
filename
cd, pwd
cd dir, chdir
Usage
Shows the files in the present directory.
Deletes
filename.
Show the present directory.
Changes the directory. In MATLAB 6, there is a
pop-up menu on the toolbar that allows the user
to change directory.
Lists the contents of
filename.
Brings up
filename
in the editor.
Displays the path to
filename.
This can help
determine whether a file is part of the standard
MATLAB package.
Lists the
.m
files and
.mat
files that are in the
current directory.
type
filename
edit
filename
which
filename
what
M
ATLAB
Commands for Workspace Management
Command
who
whos
Usage
Lists all variables in the workspace.
Lists all variables in the workspace along with the
size in bytes, array dimensions, and object type.
Removes all variables from the workspace.
Removes variables
x
and
y
from the workspace.
clear
clear x y
© 2002 by Chapman & Hall/CRC
1 . A EL B A T
1 . A EL B A T
1 . A EL B A T
1 . A EL B A T
2 . A EL B A T
2 . A EL B A T
2 . A EL B A T
2 . A EL B A T
514
Computational Statistics Handbook with M
ATLAB
and edited in a spreadsheet-like window format by double-clicking on the
variable name.
The commands contained in
Table A.2
help manage the workspace. It is
important to be able to get data into MATLAB and to save it. We outline
below some of the ways to get data in and out of MATLAB. These are not the
only options for file I/O. For example, see
help
on
fprintf, fscanf,
and
textread
for more possibilities.
Command Line:
The
save
and
load
commands are the main way
to perform file I/O in MATLAB. We give some examples of how
to use the
save
command. The
load
command works similarly.
Command
save
filename
Usage
Saves all variables in
filename.mat.
Saves only variables
var1
var2
in
filename.mat.
Saves
var1
in ASCII
format in
filename.
save
filename
var1 var2
save
filename
var1 -ascii
File Menu:
There are commands in the
File
menu for saving and
loading the workspace.
Import Wizard:
In MATLAB 6, there is a spreadsheet-like window
for inputting data. To execute the wizard, type
uiimport
at the
command line.
A.4 Punctuation in M
ATLAB
Table A.3
contains some of the common punctuation characters in MATLAB,
and how they are used.
A.5 Arithmetic Operators
Arithmetic operators (*, /, +, -, ^) in MATLAB follow the convention in linear
algebra. If we are multiplying two matrices,
A
and
B,
they must be dimen-
sionally correct. In other words, the number of columns of
A
must be equal
to the number of rows of
B.
To multiply, we simply use
A*B.
It is important
© 2002 by Chapman & Hall/CRC
Appendix A:
Introduction to M
ATLAB
515
List of M
ATLAB
Punctuation
Punctuation
%
Usage
A percent sign denotes a comment line. Information after
the % is ignored.
A comma tells MATLAB to display the results. A blank
space works similarly. It also concatenates array elements
along a row.
A semi-colon suppresses printing the contents of the
variable to the screen. It also concatenates array elements
along a column.
Three periods denotes the continuation of a statement.
Comment statements and variable names cannot be
continued with this punctuation.
An exclamation tells MATLAB to execute the following
as an operating system command.
The colon specifies a range of numbers. For example, 1:10
means the numbers 1 through 10. A colon in an array
dimension accesses all elements in that dimension.
The period before an operator tells MATLAB to perform
the corresponding operation on each element in the array.
to remember that the default interpretation of an operation is to perform the
corresponding array operation.
MATLAB follows the usual order of operations. The precedence can be
changed by using parentheses, as in other programming languages.
It is often useful to operate on an array element-by-element. For instance,
we might want to square each element of an array. To accomplish this, we add
a period before the operator. As an example, to square each element of array
A,
we use
A.^2.
These operators are summarized below in
Table A.4.
© 2002 by Chapman & Hall/CRC
3 . A E LB A T
3 . A E LB A T
3 . A E LB A T
3 . A E LB A T
,
;
...
!
:
.
Zgłoś jeśli naruszono regulamin