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
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Section 4.5 Subfunctions <strong>in</strong> <strong>Matlab</strong> 379<br />
4.5 Subfunctions <strong>in</strong> <strong>Matlab</strong><br />
Up to this po<strong>in</strong>t, our programs have been fairly simple, usually tak<strong>in</strong>g on<br />
a s<strong>in</strong>gle task, such as solv<strong>in</strong>g a quadratic equation. As programs become more<br />
complex and perform multiple tasks, modern programmers break programs <strong>in</strong>to a<br />
series <strong>of</strong> self conta<strong>in</strong>ed modules. In this manner, various parts <strong>of</strong> <strong>the</strong> program can<br />
be written separately, tested, and once stable, can be <strong>in</strong>tegrated <strong>in</strong>to <strong>the</strong> program<br />
as a whole.<br />
In older programm<strong>in</strong>g languages, <strong>the</strong>se modules were known as subrout<strong>in</strong>es.<br />
In modern, object oriented languages such as C++ and Java, <strong>the</strong> modules are<br />
objects, created from classes, that conta<strong>in</strong> <strong>the</strong>ir own variables and methods for<br />
manipulat<strong>in</strong>g data and perform<strong>in</strong>g tasks. These objects can <strong>the</strong>n be reused and<br />
assembled to create sophisticated programs.<br />
To attack a complex programm<strong>in</strong>g task <strong>in</strong> <strong>Matlab</strong>, we must first break <strong>the</strong><br />
task up <strong>in</strong>to manageable units. Each <strong>of</strong> <strong>the</strong>se units or modules are <strong>the</strong>n coded as<br />
functions and tested until we are certa<strong>in</strong> <strong>the</strong>y perform <strong>the</strong> simple task for which<br />
<strong>the</strong>y are designed. Once satisfied that <strong>the</strong> <strong>in</strong>dividual functions are perform<strong>in</strong>g as<br />
designed, we must <strong>the</strong>n <strong>in</strong>tegrate <strong>the</strong>m <strong>in</strong>to a complete and function<strong>in</strong>g program.<br />
In this section we will discuss two ways that we can accomplish this modular<br />
design.<br />
1. We can write each <strong>of</strong> <strong>the</strong> modules as functions <strong>in</strong> separate files. We <strong>the</strong>n write<br />
a master script file which calls each <strong>of</strong> <strong>the</strong> function M-files as needed.<br />
2. We can write a primary function <strong>in</strong> place <strong>of</strong> <strong>the</strong> script file. We can <strong>the</strong>n<br />
<strong>in</strong>corporate <strong>the</strong> modules as subfunctions directly <strong>in</strong> this primary function file.<br />
That is, we write one self conta<strong>in</strong>ed file.<br />
Actually <strong>the</strong>re is also a third way to proceed. We can write a primary file<br />
<strong>in</strong> place <strong>of</strong> a script file. However, by signall<strong>in</strong>g <strong>the</strong> end <strong>of</strong> this primary function<br />
with <strong>the</strong> <strong>Matlab</strong> keyword end, we signal <strong>Matlab</strong> that out subfunctions (each <strong>of</strong><br />
which must also use <strong>the</strong> keyword end) are nested functions. A primary function<br />
written <strong>in</strong> this manner emulates some <strong>of</strong> <strong>the</strong> behavior <strong>of</strong> an object <strong>in</strong> an object<br />
oriented language, with its own variables and methods (subfunctions). We’ll see<br />
<strong>in</strong> a later section that <strong>the</strong> rules for variable scope will change significantly when<br />
us<strong>in</strong>g nested functions.<br />
We will <strong>in</strong>vestigate each <strong>of</strong> <strong>the</strong>se options <strong>in</strong> some detail. In <strong>the</strong> next section,<br />
we <strong>in</strong>vestigate <strong>the</strong> first <strong>of</strong> <strong>the</strong>se options, call<strong>in</strong>g a function M-file from with<strong>in</strong> <strong>the</strong><br />
body <strong>of</strong> a script M-file.<br />
12 Copyrighted material. See: http://msenux.redwoods.edu/Math4Textbook/