Geoscience Reference
In-Depth Information
commands that are applied to variables in the workspace. MATLAB functions,
however, allow inputs and outputs to be dei ned. h ey do not automatically
import variables from the workspace. To convert the above script into a
function we need to introduce the following modii cations (Fig.2.3):
function y = average(x)
%AVERAGE Average value.
% AVERAGE(X) is the average of the elements in the array X.
% By Martin Trauth, June 27, 2014
[m,n] = size(x);
if m == 1
m = n;
end
y = sum(x)/m;
h e i rst line now contains the keyword function , the function name
average , the input x and the output y . h e next two lines contain comments,
as indicated by the percent sign, separated by an empty line. h e second
comment line contains the author's name and the version of the M-i le. h e
rest of the i le contains the actual operations. h e last line now dei nes the
value of the output variable y , and this line is terminated by a semicolon to
suppress the display of the result in the Command Window. Next we type
Fig. 2.3 Screenshot of the MATLAB Editor showing the function average . h e function
starts with a line containing the keyword function , the name of the function average , the
input variable x , and the output variable y . h e subsequent lines contain the output for help
average , the copyright and version information, and also the actual MATLAB code for
computing the average using this function.
Search WWH ::




Custom Search