The document compares script files and function files in MATLAB, highlighting that both are saved with a .m extension, but function files have local variables and must start with a function definition line, while script files can access workspace variables. It also introduces anonymous functions, which are simple user-defined functions that do not require a separate file and can be defined anywhere in MATLAB, replacing inline functions. Anonymous functions capture the values of predefined variables at the time of their creation and must be redefined to use updated values.
The document compares script files and function files in MATLAB, highlighting that both are saved with a .m extension, but function files have local variables and must start with a function definition line, while script files can access workspace variables. It also introduces anonymous functions, which are simple user-defined functions that do not require a separate file and can be defined anywhere in MATLAB, replacing inline functions. Anonymous functions capture the values of predefined variables at the time of their creation and must be redefined to use updated values.
7 COMPARISON BETWEEN SCRIPT FILES AND FUNCTION FILES
Students who are studying MATLAB for the first time sometimes have difficulty understanding exactly the differences between script and function files since, for many of the problems that they are asked to solve using MATLAB, either type of file can be used. The similarities and differences between script and function files are summarized below. • Both script and function files are saved with the extension .m (that is why they are sometimes called M-files). • The first executable line in a function file is (must be) the function definition line. • The variables in a function file are local. The variables in a script file are recognized in the Command Window. • Script files can use variables that have been defined in the workspace. • Script files contain a sequence of MATLAB commands (statements). • Function files can accept data through input arguments and can return data through output arguments. • When a function file is saved, the name of the file should be the same as the name of the function. 7.8 ANONYMOUS AND INLINE FUNCTIONS User-defined functions written in function files can be used for simple mathematical functions, for large and complicated math functions that require extensive programming, and as subprograms in large computer programs. In cases when the value of a relatively simple mathematical expression has to be determined many times within a program, MATLAB provides the option of using anonymous functions. An anonymous function is a user-defined function that is defined and written within the computer code (not in a separate function file) and is then used in the code. Anonymous functions can be defined in any part of MATLAB (in the Command Window, in script files, and inside regular user-defined functions). Anonymous functions were introduced in MATLAB 7. They replace inline functions that were used for the same purpose in previous versions of MATLAB. Both anonymous and inline functions can be used in MATLAB R2010b). Anonymous functions, however, have several advantages over inline functions, and it is expected that inline functions will gradually be phased out. Anonymous functions are covered in detail in Section 7.8.1, and inline functions are described in the section that follows. 230 Chapter 7: User-Defined Functions and Function Files 7.8.1 Anonymous Functions An anonymous function is a simple (one-line) user-defined function that is defined without creating a separate function file (M-file). Anonymous functions can be constructed in the Command Window, within a script file, or inside a regular user-defined function. An anonymous function is created by typing the following command: A simple example is: cube = @ (x) x^3, which calculates the cube of the input argument. • The command creates the anonymous function and assigns a handle for the function to the variable name on the left-hand side of the = sign. (Function handles provide means for using the function and passing it to other functions; see Section 7.9.1.) • The expr consists of a single valid mathematical MATLAB expression. • The mathematical expression can have one or several independent variables. The independent variable(s) is (are) entered in the (arglist). Multiple independent variables are separated with commas. An example of an anonymous function that has two independent variables is: circle = @ (x,y) 16*x^2+9*y^2 • The mathematical expression can include any built-in or user-defined functions. • The expression must be written according to the dimensions of the arguments (element-by-element or linear algebra calculations). • The expression can include variables that are already defined when the anonymous function is defined. For example, if three variables a, b, and c are defined (have assigned numerical values), then they can be used in the expression of the anonymous function parabola = @ (x) a*x^2+b*x+c. Important note: MATLAB captures the values of the predefined variables when the anonymous function is defined. This means that if new values are subsequently assigned to the predefined variables, the anonymous function is not changed. The anonymous function has to be redefined in order for the new values of the predefined variables to be used in the expression. name = @ (arglist) expr The name of the anonymous function. The @ symbol. A list of input arguments (independent variables). Mathematical expression. 7.8 Anonymous and Inline Functions 231 Using an anonymous function: