Nested Function and Function Handle Example
Nested Function and Function Handle Example
R2012b
MATLAB
Programming Scripts and Functions
Functions
Function Basics
Nested Functions
On this page
function parent
disp('This is the parent function')
nestedfx
function nestedfx
disp('This is the nested function')
end
end
The primary difference between nested functions and other types of functions is that they can access and
modify variables that are defined in their parent functions. As a result:
Nested functions can use variables that are not explicitly passed as input arguments.
In a parent function, you can create a handle to a nested function that contains the data necessary
to run the nested function.
Typically, functions do not require an end statement. However, to nest any function in a program
file, all functions in that file must use an end statement.
You cannot define a nested function inside any of the MATLAB program control statements, such
as if/elseif/else, switch/case, for, while, or try/catch.
You must call a nested function either directly by name (without using
handle that you created using the @ operator (and not str2func).
All of the variables in nested functions or the functions that contain them must be explicitly defined.
That is, you cannot call a function or script that assigns values to variables unless those variables already
exist in the function workspace. (For more information, see Variables in Nested and Anonymous Functions.)
function main1
function main2
x = 5;
nestfun2;
nestfun1;
function nestfun2
function nestfun1
x = 5;
x = x + 1;
end
end
x = x + 1;
end
end
When parent functions do not use a given variable, the variable remains local to the nested function. For
example, in this function named main, the two nested functions have their own versions of
interact with each other:
function main
nestedfun1;
nestedfun2;
x that cannot
function nestedfun1
x = 1;
end
function nestedfun2
x = 2;
end
end
Functions that return output arguments have variables for the outputs in their workspace. However, parent
functions only have variables for the output of nested functions if they explicitly request them. For example,
this function parentfun does not have variable y in its workspace:
function parentfun
x = 5;
nestfun;
function y = nestfun
y = x + 1;
end
end
If you modify the code as follows, variable z is in the workspace of parentfun:
function parentfun
x = 5;
z = nestfun;
function y = nestfun
y = x + 1;
end
end
Using Handles to Store Function Parameters
Nested functions can use variables from three sources:
Input arguments
function p = makeParabola(a,b,c)
p = @parabola;
function y = parabola(x)
y = a*x.^2 + b*x + c;
end
end
The makeParabola function returns a handle to the parabola function that includes values for
coefficients a, b, and c.
At the command line, call the makeParabola function with coefficient values of 1.3, .2, and 30. Use
the returned function handle p to evaluate the polynomial at a particular point:
p = makeParabola(1.3,.2,30);
X = 25;
Y = p(X)
Y =
847.5000
Many MATLAB functions accept function handle inputs to evaluate functions over a range of values. For
example, plot the parabolic equation from -25 to +25:
fplot(p,[-25,25])
You can create multiple handles to the parabola function that each use different polynomial coefficients:
firstp = makeParabola(0.8,1.6,32);
secondp = makeParabola(3,4,50);
range = [-25,25];
figure
hold on
fplot(firstp,range)
fplot(secondp,range,'r:')
hold off
From the level immediately above it. (In the following code, function A can call B or D, but not C or
E.)
From a function nested at the same level within the same parent function. (Function
and D can call B.)
From a function at any lower level. (Function C can call B or D, but not E.)
function A(x, y)
B(x,y);
D(y);
% Main function
function B(x,y)
C(x);
D(y);
% Nested in A
B can call D,
function C(x)
D(x);
end
% Nested in B
end
function D(x)
E(x);
% Nested in A
function E(x)
disp(x)
end
end
end
% Nested in D