ENGR 1181 MATLAB 15: Functions 2
Preparation Material

ENGR 1181 | MATLAB 15: Functions 2Preparation Material

Learning Objectives

  1. Integrate user-written functions into the same file with the main program
  2. Identify good function conventions, (e.g., the importance of placing functions at the end

Topics

Students will read Chapter 6 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading.

Students will learn more advanced techniques and syntax for creating and utilizing functions in MATLAB. This material contains the following:

  1. Functions in Script Files
  2. Using function main
  3. Nested Functions

1. Functions in Script Files

Previously, we discussed creating user-defined function files, that similar to a built-in function, is a stand alone script file that is accessed from within an executable MATLAB program. There are also ways to imbed function files directly inside the same script file. While there are certain limitations and constraints, this allows a script file to access a function without saving additional files. This reduces issues when sharing programs with others, or moving files, because there is only one file to keep track of rather than having multiple files.

Limitations of Functions in Script Files
  • Only one line functions can be included inside a script file
  • There are two kinds of one line functions

Inline functions

Anonymous functions

Inline Functions

This is an older format that may soon become obsolete. It is briefly covered for breadth of knowledge, but it is recommended to focus on Anonymous Functions for this class. The general format of an inline function is as follows:

name =inline('math expression as a string') or

name =inline('math expression as a string','arg1','arg2')

- where order of arguments is specified; the default is alphabetical order

The following is an example of an inline function:

MofInertia=inline('b*h^3/12', 'h', 'b') % not alphabetical

h=3; b=5;

I=MofInertia(h,b); % define function before using it

Some important notes about inline functions:

  • First, define the function, then enter the values for input, and then call the function including the proper variable.
  • Any letter except i and j may be used for independent variables.
  • The one line math expression can use any built-in or user-defined functions.
  • Include dot operators if the input variables can be arrays.
  • The expression CANNOT use predefined variables (variables are defined AFTER defining the inline functions). A wrong approach is:
  • a=3; b=5;
  • Royal_Mess=inline('sqrt(a*x.^2+b*y.^2) ')
Anonymous Functions

This is a newer format for a function and is more readily accepted where the name becomes the function handle. This means that it points to where the function is stored in the computer. The general format of an anonymous function is as follows:

name = @ (arglist) math_expression

The following is an example of an anonymous function:

MofInertia=@ (h,b) b*h^3/12

h=3; b=5;

I=MofInertia(h,b);% again, function is defined before being used

Some important notes about anonymous functions:

  • First, define the function, then enter the values for input, and then call the function including the proper variable.
  • The one line math expression can use any built-in or user-defined functions
  • Include dot operators when using arrays for input
  • The math expression CAN use predefined variables, a=3; b=5;

Royal_Mess=@ (x,y) sqrt(a*x.^2+b*y.^2)

NOTE: The predefined variables must be defined before the anonymous function is defined and NOT when it is used. New values for the predefined variables mean redefining the function!

2. Using function main

Typically, only one-line functions can be included within a script file. MATLAB will allow a multi-line function to be included at the end of a script file if function main is used properly. Sending a directory of files to a user can be confusing for them and a pain to e-mail. Using function main streamlines this and allows users to save, use, and share just one file!

Insert ‘function main’in the top line of your script file, i.e. a function with no input variables and no output variables. Then, you can add multi-line user defined functions to the file. This top level still acts like, and should be used as, a script file! It is imperative that MATLAB knows when the routine ends, so it also requires an ‘end’ statement.

One drawback of function main is that a function created inside that script file is not recognized by other script files. It must be manually copied into another script file or a separate function file made from it.

Function Main Example
Function Programming Insight

•Once ‘function main’ and ‘end’ are placed at the beginning and the end of the script, respectively, the script file can now use saved and ran. This will run the top level script file (function main) and call each included function when that line of the script file is reached.

•ALL functions in a file will only be understood inside that file, not in other files.

•Because functions are only understood inside of their respective file, general purpose functions should be as separate files in a user library.

•Include user-defined functions AFTER the end statement for “function main”, not before!

3. Nested Functions

Functions can be placed inside other functions (before the end statement). These are called nested functions and follow the same basic layout as nested loops. An example of nested functions:

•Nested Functions share variable names and values, just like between script files but with limitations. This can cause problems if you do not want such interactions!

•For example, a variable defined in a primary function is recognized and can be redefined by any functions contained within it (before the end statement for the primary function).

•Place user-defined functions separately, each after the end statement of the previous one if you want the functions to behave independently and not share variable names and storage.

•Nest functions if you do want interaction, which is more similar to separate script files – see the book and help menus for more details.

1