Versions Compared

Key

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

...

Functions can be called from within a function. You can put multiple functions in one M-file as shown in the following example from the MATLAB help:
example located in this documentation page.

Home (tab) > Documentation (icon) > Search: Subfunctions > Local Functions

Please go through the subfunction page in its entirety before watching the following tutorial video. Image Removed

In the above example, there are three functions: the main function is called newsstats. Within this function, there are two subfunctions; one called mean and the other, median. The subfunction is defined the exact same way as the main function; but the difference is that the subfunction is available to functions in this M-file only - you cannot call your subfunctions in the command window. In the above example, the main function calls both of the subfunctions, making the code much shorter and simpler. For longer, more complex code, this can save you a lot of debugging time by breaking down your code into smaller pieces.

Open up beam5.m. We will use the subfunction concept to incorporate the main program (beam5.m) and the function bending_stress into one M-file. We are going to designate beam5.m as a function. So at the top of the code, include the following code:

...