<< Chapter < Page | Chapter >> Page > |
You should write a MATLAB program whenever you anticipate exe- cuting some sequence of statements several times or again in a later session.On an IBM PC, you may use any text editor to write a program, as long as the file can be saved in ASCII format without the control codes used by mostword processors. Applicable text editors include Edix, Wordstar, XTree Pro, and Turbo Pascal's editor.
Editing Files. If you have enough memory, you can run your editor without leaving MATLAB by using the exclamation point (!), like this:
The exclamation point may be used to execute any DOS command or program
from MATLAB. When the command or program finishes, your MATLABvariables are just as you left them. Use your editor to write program lines
just as you would type them in MATLAB's command mode. Then save thefile with extensionin the directory where you will run MATLAB. Such
MATLAB programs are called m-files. You may run your m-file by typing thefile name (without theextension) at the MATLAB command prompt
.
.m
Script Files. There are two kinds of m-files, called
script files and
functions . Running a script file is exactly like typing the commands it contains
at the
prompt. Your m-file will automatically be a script file unless you
specify otherwise, as described later. Practice by entering, saving, and runningas listed next:
plotsin.m
When the pause is executed, you will need to press a key to go on. If you type
whos after running, you will see that the variablesandremain
in memory. Comments are important to a script file. They are marked withthe symbol %. Anything following this symbol on a line is assumed to be a
comment and is ignored by the MATLAB program interpreter.
y
Functions. Functions differ from script files in that they have designated input and output variables. Any other variables used within a function are local variables, which do not remain after the function terminates and which have no effect on variables outside the function. Many of the functions supplied with MATLAB are actually m-files. A good example is:
triu.m
The word function at the beginning of the file makes it a function rather than
a script file. The function name in this line must match the file name. Theinput variables ofareand, meaning that the first input argument
will be referred to asand the second aswithin the function. Likewise,
the function line designatesas the output. There is nothing special about
the variable names,, andwhen the function is used. It is only that
whatever inputs and output you use will be referred to as,, andinside
the function. The variables,,, andare created temporarily whenruns and disappear when it terminates. They are local variables and have no
effect on variables with the same names outside the function. In contrast, ascript file has no local variables and does no substitution of input and output
variable names.
triu
As an exercise, enter and save the function:
perp.m
Evaluateon various complex numbers. Replace the last line by. Do you get the same result? Why?
y = x*j;
Printing Files and Graphics. To display an m-file on the screen,
use the instruction. To make a copy at a printer, use
the DOS command. Graphics hardcopy is available
through the commands meta and. See the MATLAB manual for more
information.
gpp
Notification Switch
Would you like to follow the 'A first course in electrical and computer engineering' conversation and receive update notifications?