Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


Add this to beam5.m. Run the file and check the output  You should get the same plot you got with beam3.m.

Subfunctions

Functions can be called within a function. As crazy as this sounds, this sometimes makes very complex code appear much more manageable by sectionalizing the code. To explain how to create a subfunction, let's look to the matlab help for an example:

function avg, med = newstats(u) % Primary function
% NEWSTATS Find mean and median with internal functions.
n = length(u);
avg = mean(u, n);
end

function a = mean(v, n) % Subfunction
% Calculate average.
a = sum(v)/n;
end

In the above example, there are two functions: the main function is called "newsstats". Within this function, there is a subfunction called "mean". The subfunction is defined the exact same way as the main function; but the difference is the subfunction can only used when the main function is used - you can not call your subfunctions in the command window.

That brings us to the end of this tour. Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

...