8.11: Editing Files and Creating Functions (PC)
You should write a MATLAB program whenever you anticipate executing 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 most word 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:
≫ !EDIX
The exclamation point may be used to execute any DOS command or program from MATLAB. When the command or program finishes, your MATLAB variables 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 the file with extension
.m
in the directory where you will run MATLAB. Such MATLAB programs are called m-files. You may run your m-file by typing the file name (without the
.m
extension) at the MATLAB command prompt (\(\gg\)).
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 \(\gg\) prompt. Your m-file will automatically be a script file unless you specify otherwise, as described later. Practice by entering, saving, and running
plotsin.m
as listed next:
t = -6:.2:6; y = sin(t); plot(t,y) title('SINE') pause grid xlabel('t') ylabel('sin(t)')
When the pause is executed, you will need to press a key to go on. If you type whos after running
plotsin
, you will see that the variables
t
and
y
remain in memory. Comments are important to a script file. They are marked with the symbol %. Anything following this symbol on a line is assumed to be a comment and is ignored by the MATLAB program interpreter.
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
:
≫ type triu
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. The input variables of
triu
are
x
and
k
, meaning that the first input argument will be referred to as
x
and the second as
k
within the function. Likewise, the function line designates
y
as the output. There is nothing special about the variable names
x
,
k
, and
y
when the function is used. It is only that whatever inputs and output you use will be referred to as
x
,
k
, and
y
inside the function. The variables
m
,
n
,
j
, and
i
are created temporarily when
triu
runs and disappear when it terminates. They are local variables and have no effect on variables with the same names outside the function. In contrast, a script file has no local variables and does no substitution of input and output variable names.
As an exercise, enter and save the function
perp.m
:
function y=perp(x) % PERP(x) is a complex number perpendicular to x. j = sqrt(-1); y = j*real(x)-imag(x);
Evaluate
perp
on various complex numbers. Replace the last line by
y = x*j;
. Do you get the same result? Why?
Printing Files and Graphics
To display an m-file on the screen, use the instruction
≫ type filename
. To make a copy at a printer, use the DOS command
≫ !print filename.m
. Graphics hardcopy is available through the commands meta and
gpp
. See the MATLAB manual for more information.