You are on page 1of 2

03.05.

2013

Anonymous Functions

Advanced Features of User- Defined Functions


From the books: 1. MATLAB A Practical Introduction by Stormy Attaway 2. MATLAB Programming with Applications for Engineers, S. J. Chapman

Anonymous Functions: Simple one-line functions that are called using their function handle. It does not have to be stored in an M-file. Anonymous functions can be created in the Command Window or in any script. The syntax for an anonymous function is:
fhandle = @(arguments) functionbody

Function handle is a way of referring to a function. The handle is assigned to this name using the @ operator.
Copyright 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright 2013 Pearson Educati on, Inc. Publishing as Pearson Addison-Wesley

Example: Anonymous Functions


An anonymous function that calculates and returns the area of the circle. >> cirarea = @ (radius) pi * radius.^2;
function handle: cirarea input: (radius) function body: pi * radius.^2;

Anonymous Functions
If no argument is passed to an anonymous function, the parenthesis must still be in the function definition and in the function call. Example: an anonymous function that prints a random real number with two decimal places:
>> prtran = @ () fprintf('%.2f\n',rand); >> prtran() 0.95
Copyright 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The function is called using the handle and passing arguments to it:
>> cirarea(4) ans = 50.2655 >> cirarea(1:4) ans = 3.1416 12.5664 28.2743 50.2655
Copyright 2013 Pearson Educati on, Inc. Publishing as Pearson Addison-Wesley

Saving Anonymous Functions


An anonymous function can be saved to a MAT file, and then it can be loaded when needed.
cirarea = @ (radius) pi * radius.^2; save anonfns cirarea clear load anonfns who

Uses of Function Handles


Function handles can be created for functions other than anonymous functions, both built-in and user defined functions. Create a function handle for the built-in factorial function.
facth = @factorial;

The @ operator gets the handle of the function, which is then stored in a variable facth. Handle could be used to call the function instead of the name of the function itself.
facth(5)
Copyright 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright 2013 Pearson Educati on, Inc. Publishing as Pearson Addison-Wesley

03.05.2013

Function Functions
One reason for using function handles is to be able to pass functions to other functions these are called function functions. In other words they are functions whose input arguments include the names of other functions. The functions that are passed to the function function are normally used during that functions execution.

Example: Function Functions


Example (in handout)
function fnfnexamp( funh) % Example of a function function. The handle of a function is passed % and that function of x is plotted. x = 1:0.25:6; y = funh(x); plot(x,y,'ko'); Test with the following lines: fnfnexamp(sin) % will not work fnfnexamp(@sin) fnfnexamp(@cos)

Copyright 2013 Pearson Educati on, Inc. Publishing as Pearson Addison-Wesley

Copyright 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Built-in Function Functions


fplot: plots a function between limits that are specified. fplot can be called as: fplot(fnhandle, [xmin xmax]) Example: fplot(@sin, [-pi, pi]) fplot is a nice shortcut it is not necessary to create x and y vectors, and it plots a continuous curve rather than discrete points. feval: will evaluate a function handle and execute the function for the specified argument. The following is equivalent to sin(3.2): >> feval(@sin, 3.2)
Copyright 2013 Pearson Educati on, Inc. Publishing as Pearson Addison-Wesley

You might also like