Environmental Engineering Reference
In-Depth Information
17.5 MATLAB
®
Functions
In the accompanying software the
'georef.m'
M-file has a different header than
most of the other M-files developed in the topic. The header line
function
[xx0,yy0,xx1,yy1,xline,yline,xloc,yloc] = georef ()
defines a MATLAB
function with name
georef
. The name appears on the right
side of the equals sign. As a rule, the file has the same name, with extension '
.m
'.
A function may be called from the MATLAB
®
command line or another M-file.
That is not the main point, as all M-files can be called that way. The difference lies
in the connection between calling module and function via both input- and output
parameters. When an M-file, which is not a function, is called, parameters and
variables in the called file are taken from the calling program. If there is a variable
in the M-file, it is taken from the pool of
global variables
, defined at an upper level
by the calling program. If it does not exist there, an error message results. In
MATLAB
®
such files are called
scripts
.
Functions are distinguished from scripts, because functions are working within
their own local data environment with variables that are locally defined only:
local
variables
. Of course, data can be transferred from and to the calling M-file. This is
done via a list of reference arguments. This point is best discussed by an example.
The
'georef.m'
file, included in the accompanying software, starts with the function
command, given above. On the left side of the equals sign, in square brackets, the
output parameters appear, i.e. those values that are computed or manipulated within
the M-file and which are needed for further processing in the calling module. There
are eight parameters in this example, starting with
xx0
and ending with
yloc
.
Input parameters appear on the right side of the function name in round brackets.
In the example there are no such variables: the brackets are empty. Empty brackets
can also be omitted in m-language. The same parameter may appear both in the
input and in the output list.
The user has to make sure that the types of the variables in the function M-file
and in the calling command fit to each other. Not only the number of variables
needs to be the same, the sequence and the types of each of the corresponding
variables need to be identical as well. Exceptions from that general rule exist, but
are not discussed here. The interested MATLAB
®
user may have a look into the
help system under
nargin
,
nargout
. The example
georef
function could be called
by the command:
®
[A,B,C,D,xbound,ybound,xwell,ywell] = georef;
by which the variable
xx0
of the function becomes
A
in the calling routine. In this
example command, all variables have different names within and outside of the
function.
A
must be a double value, as
xx0
is one, and
xbound
must be a 1D array, as
the corresponding
xline
is a 1D array. The function
georef
can be called from the
MATLAB
command window, or from any M-file in which geo-referencing is
®