Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Section 4.3 Functions <strong>in</strong> <strong>Matlab</strong> 345<br />
call<strong>in</strong>g statement will be filled <strong>in</strong> <strong>the</strong> order def<strong>in</strong>ed <strong>in</strong> <strong>the</strong> function m-file. For<br />
example, <strong>the</strong> follow<strong>in</strong>g command returns and stores <strong>the</strong> area.<br />
>> myarea=area_and_circumference(radius)<br />
myarea =<br />
452.3893<br />
The order <strong>of</strong> <strong>the</strong> output variables <strong>in</strong> <strong>the</strong> function def<strong>in</strong>ition control <strong>the</strong> order <strong>in</strong><br />
which results are returned to <strong>the</strong> call<strong>in</strong>g statement, not <strong>the</strong> names <strong>of</strong> <strong>the</strong> output<br />
variables <strong>in</strong> <strong>the</strong> call<strong>in</strong>g statement.<br />
>> mycircum=area_and_circumference(radius)<br />
mycircum =<br />
452.3893<br />
This could be a crucial error and illustrates why function m-files should conta<strong>in</strong><br />
documentation expla<strong>in</strong><strong>in</strong>g <strong>the</strong>ir use.<br />
Document<strong>in</strong>g Help <strong>in</strong> Function M-Files. The first contiguous block <strong>of</strong><br />
comment files <strong>in</strong> a function m-file are displayed to <strong>the</strong> command w<strong>in</strong>dow when a<br />
user enters help funname, where funname is <strong>the</strong> name <strong>of</strong> <strong>the</strong> function. You term<strong>in</strong>ate<br />
this open<strong>in</strong>g comment block with a blank l<strong>in</strong>e or an executable statement.<br />
After that, any later comments do not appear when you type help funname.<br />
The first l<strong>in</strong>e <strong>of</strong> <strong>the</strong> comment block is special and is used by <strong>Matlab</strong>’s lookfor<br />
and should <strong>the</strong>refore conta<strong>in</strong> <strong>the</strong> function name and a brief description <strong>of</strong> <strong>the</strong><br />
function. Ensu<strong>in</strong>g l<strong>in</strong>es should conta<strong>in</strong> syntax <strong>of</strong> use and sometimes it is useful<br />
to provide examples <strong>of</strong> use. Type help svd for an example <strong>of</strong> a well crafted help<br />
file and <strong>the</strong>n type lookfor svd to see how <strong>the</strong> first l<strong>in</strong>e <strong>of</strong> <strong>the</strong> help file is special.<br />
With <strong>the</strong>se thoughts <strong>in</strong> m<strong>in</strong>d, let’s craft some documentation for our function<br />
area_and_circumference. Add <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es to <strong>the</strong> m-file and save.<br />
function [area,circumference]=area_and_circumference(r)<br />
%AREA_AND_CIRCUMFERENCE f<strong>in</strong>ds <strong>the</strong> area and circumference<br />
%<strong>of</strong> a circle.<br />
% [A,C]=AREA_AND_CIRCUMFERENCE(R) returns <strong>the</strong> area A and<br />
% circumference C <strong>of</strong> a circle <strong>of</strong> radius R.<br />
area=pi*r^2;<br />
circumference=2*pi*r;