5.1.1.4 The Body Text
The function body contains all the Matlab code that performs computations
and assigns values to output arguments. The statements in the function
body can consist of function calls, programming constructs like flow control
and interactive input/output, calculations, assignments, comments, and
blank lines.
For example, the body of the
D
D
i
i
a
a
g
g
E
E
x
x
t
t
r
r
a
a
c
c
t
t function contains a number of
simple programming statements:
“% Author: Panayiotis Andreou, Sept. 2003
% This function uses the error build-in function that displays a message
% and aborts the function's execution
[m n]=size(X); %Saving the size of the matrix
if isempty(X) %Checking if it is an empty matrix
error('You have not passed a square matrix');
elseif (m~=n) %Checking if matrix is square
error('You have enter a non square matrix');
elseif (m==n & m==1) %Checking if it is a single value
error('You have enter a single value');
else
Ind=(1:n:n^2)+(0:n-1); %Finding the indeces of the diagonal elements
V=X(Ind); %Extracting to V the diagonal elements
end ”
5.2 Scripts
On the other hand, scripts can operate on existing data in the workspace, or
they can create new data on which to operate. Although scripts do not
return output arguments, any variables that they create remain in the
workspace, to be used in subsequent computations. In addition, scripts can
produce graphical output using functions. Scripts are useful for automating a
series of steps that are needed to be performed many times (e.g. to create a
Comentarios a estos manuales