Page 49 - Basics of MATLAB and Beyond
P. 49
% Do the valleys:
subplot(222)
ind = find(z>0);
zplot = z;
zplot(ind) = zeros(size(ind));
mesh(zplot)
axis tight
Now try this in the matlab window:
mfile1
matlab has executed the instructions in mfile1.m just as if you had
typed them in. The lines beginning with the percent sign % are ignored
by matlab so they can be used to put comments in your code. Blank
lines can be used to improve readability.
Any variables created by a script m-file are available in the command
window after the m-file completes running:
>> clear
>> whos
>> mfile1
>> whos
Name Size Bytes Class
ind 1544x1 12352 double array
z 49x49 19208 double array
zplot 49x49 19208 double array
Grand total is 6346 elements using 50768 bytes
These variables are said to exist in the matlab workspace. Scripts can
also operate on variables that already exist in the workspace.
You can type the name of a script file within another script file. For
example you could create another file called mfile2 that contains the
text line mfile1; the contents of mfile1 will then be executed at that
point within mfile2.
8.2 Functions
Functions are m-files that can be used to extend the matlab language.
Functions can accept input arguments and produce output arguments.
Many of matlab’s own commands are implemented as m-files; try typ-
ing type mean to see how matlab calculates the mean. Functions use
c 2000 by CRC Press LLC