15.11.2014 Views

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

SHOW MORE
SHOW LESS

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.5 Subfunctions <strong>in</strong> <strong>Matlab</strong> 383<br />

Run <strong>the</strong> script at <strong>the</strong> <strong>Matlab</strong> prompt <strong>in</strong> <strong>the</strong> command w<strong>in</strong>dow.<br />

>> rationalArithmetic<br />

In response to <strong>the</strong> prompt, try enter<strong>in</strong>g choices 1 through 5. Note that <strong>in</strong> each<br />

case, <strong>the</strong> menu refreshes and you are prompted for ano<strong>the</strong>r choice. Select<strong>in</strong>g choice<br />

#6 exits <strong>the</strong> script and returns you to <strong>the</strong> <strong>Matlab</strong> command prompt. When <strong>the</strong><br />

user makes <strong>the</strong> choice to exit <strong>the</strong> program, <strong>the</strong> variable f<strong>in</strong>ished is assg<strong>in</strong>ed <strong>the</strong><br />

value true and <strong>the</strong> while loop term<strong>in</strong>ates.<br />

Hopefully, readers can now ascerta<strong>in</strong> where we’re head<strong>in</strong>g with this structure.<br />

We code specific <strong>in</strong>dividual tasks as functions and save <strong>the</strong>m as function M-files<br />

<strong>in</strong> <strong>the</strong> same directory conta<strong>in</strong><strong>in</strong>g <strong>the</strong> script rationalArithmetic. We <strong>the</strong>n can<br />

access <strong>the</strong>se functions by call<strong>in</strong>g <strong>the</strong>m from <strong>the</strong> script. Once <strong>the</strong> program is fully<br />

implemented, we’ll have a number <strong>of</strong> function M-files collected <strong>in</strong> a directory that<br />

are driven from a script resid<strong>in</strong>g <strong>in</strong> <strong>the</strong> same directory.<br />

We will pursue this design and structure no fur<strong>the</strong>r (functions and a master<br />

script file), as we will now consider how we can implement a similar design with<br />

subfunctions.<br />

Subfunctions<br />

To use subfunctions to accomplish our programm<strong>in</strong>g task, we make a simple but<br />

subtle change to our structure. We take <strong>the</strong> script file and change it <strong>in</strong>to a function<br />

by adjust<strong>in</strong>g <strong>the</strong> first l<strong>in</strong>e so that it conta<strong>in</strong>s <strong>the</strong> keyword function.<br />

function rationalArithmetic<br />

f<strong>in</strong>ished=false;<br />

while (~f<strong>in</strong>ished)<br />

n=rationalMenu;<br />

switch n<br />

case 1<br />

case 2<br />

case 3<br />

case 4<br />

case 5<br />

case 6<br />

f<strong>in</strong>ished=true;<br />

end<br />

end

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!