The function must begin with a line containing the word function, which is followed
by the output arguments, an equal sign, and the name of the function. The input
arguments to the function follow the function name and are enclosed in parentheses.
This first line distinguishes the function file from a script file. The name of the function
should match the name of the M-file. If there is a conflict, it is the name of the M-file
located on the disk that is known to the MATLAB command environment.
A line beginning with the % (the percent symbol) sign is a comment line and a `%' sign
may be put anywhere. Anything after a % in a line is ignored by MATLAB during
execution. The first few lines immediately following the function definition line should
be comments because they will be displayed if help is requested for the function name.
For example, typing
help dft
will display five comment lines from dft.m M-file on the screen as help information if
this M-file is in the current directory. These comment lines are usually used to provide a
brief explanation, define the function calling sequence, and then a definition of the input
and output arguments.
The only information returned from the function is contained in the output argu-
ments, which are matrices. For example, Xk in the dft.m. Multiple output arguments
are also possible if square brackets surround the list of output arguments. MATLAB
returns whatever value is contained in the output matrix when the function completes.
The same matrix names can be used in both a function and the program that
references it since the function makes local copies of its arguments. However, these
local variables disappear after the function completes, thus any values computed in the
function, other than the output arguments, are not accessible from the program.
It is possible to declare a set of variables to be accessible to all or some functions
without passing the variables in the input list. For example,
global input_x;
The global command declares the variable input_x to be global. This command goes
before any executable command in the functions and scripts that need to access the value
of the global variables. Be careful with the names of the global variables to avoid conflict
with other local variables.
B.5 Summary of Useful MATLAB Functions
sin ± sine
cos ± cosine
tan ± tangent
abs ± absolute value
angle ± phase angle
sqrt ± square root
real ± real part of complex variable
imag ± imaginary part of complex variable
conj ± complex conjugate
round ± round towards the nearest integer
466
APPENDIX B: INTRODUCTION OF MATLAB FOR DSP APPLICATIONS