Function Function
Local Function
Private Function
MOREON
USERDEFINEDFUNCTION
Shameer A Koya
http://electricalenggtutorial.blogspot.com
A function handle is a data type that stores an
association to a function.
the functions can be built-in, user defined, or
anonymous
It is used to call a function indirectly.
To make a function handle for built-in or user-
defined function, put the @ symbol in front of
function name
fhandle = @functionname
function handle for MATLAB ones function is @ones
FUNCTION HANDLE
Typical uses are:
Pass a function to another function
(called function functions).
Specify callback functions. (GUI
programming)
Construct handles to inline functions or
anonymous functions.
Call local functions from outside the main
function
USES OF FUNCTION HANDLE
 Anonymous function
sqr = @(x) x.^2
 Calling function indirect:
function y = Sqr(x)
y = x.^2;
end
f = @Sqr;
a = 2;
b = f(a);
b =
4
 Pass to another function
z = integral(f,0,1);
EXAMPLES
A MATLAB function that accepts another
function as an input is called a function
function
Function handles are used for passing
functions to function functions
Syntax for function function is same as simple
functions, but one or more input arguments
will be function handles.
FUNCTION FUNCTIONS
 From the previous example:
z = quad(f,0,1);
z =
0.33333
 Calculate the integral for the interval specified.
fzero(@sqr,-3,3)
 Finds the zeros of the function with in the range
 Can be used to plot the functions
x = linspace(-3,3,100);
figure;
plot(x,f(x))
EXAMPLES
 Multiple functions within one function file
 Name of function file should be name of main
function.
 Main function can be called from the command
window or any other function.
 Local functions are typed in any order after the
main function.
 Local functions are only visible to other functions in
the same file. (They are not visible to the functions
out side or command window).
LOCAL FUNCTION (SUB-FUNCTION)
function [a, b, c] = basicmath(x, y)
% basicmath is the main function.
a = add(x,y);
b = sub(x,y);
c = mult(x,y);
end
function l = add(u,v)
% add is a local function.
l = u+v;
end
function m = sub(u,v)
% sub is another local function.
m = u-v;
end
function n = mult(u,v)
% sub is another local function.
n = u*v;
end
EXAMPLE
 A private function is a function residing in a
subdirectory with the name private.
 Private functions are visible only to functions in the
parent directory.
 They are useful when we want to limit the scope of
a function.
 You cannot call the private function from the
command line or from functions outside the parent
of the private folder.
PRIVATE FUNCTIONS

User Defined Functions in MATLAB Part-4

  • 1.
    Function Function Local Function PrivateFunction MOREON USERDEFINEDFUNCTION Shameer A Koya http://electricalenggtutorial.blogspot.com
  • 2.
    A function handleis a data type that stores an association to a function. the functions can be built-in, user defined, or anonymous It is used to call a function indirectly. To make a function handle for built-in or user- defined function, put the @ symbol in front of function name fhandle = @functionname function handle for MATLAB ones function is @ones FUNCTION HANDLE
  • 3.
    Typical uses are: Passa function to another function (called function functions). Specify callback functions. (GUI programming) Construct handles to inline functions or anonymous functions. Call local functions from outside the main function USES OF FUNCTION HANDLE
  • 4.
     Anonymous function sqr= @(x) x.^2  Calling function indirect: function y = Sqr(x) y = x.^2; end f = @Sqr; a = 2; b = f(a); b = 4  Pass to another function z = integral(f,0,1); EXAMPLES
  • 5.
    A MATLAB functionthat accepts another function as an input is called a function function Function handles are used for passing functions to function functions Syntax for function function is same as simple functions, but one or more input arguments will be function handles. FUNCTION FUNCTIONS
  • 6.
     From theprevious example: z = quad(f,0,1); z = 0.33333  Calculate the integral for the interval specified. fzero(@sqr,-3,3)  Finds the zeros of the function with in the range  Can be used to plot the functions x = linspace(-3,3,100); figure; plot(x,f(x)) EXAMPLES
  • 7.
     Multiple functionswithin one function file  Name of function file should be name of main function.  Main function can be called from the command window or any other function.  Local functions are typed in any order after the main function.  Local functions are only visible to other functions in the same file. (They are not visible to the functions out side or command window). LOCAL FUNCTION (SUB-FUNCTION)
  • 8.
    function [a, b,c] = basicmath(x, y) % basicmath is the main function. a = add(x,y); b = sub(x,y); c = mult(x,y); end function l = add(u,v) % add is a local function. l = u+v; end function m = sub(u,v) % sub is another local function. m = u-v; end function n = mult(u,v) % sub is another local function. n = u*v; end EXAMPLE
  • 9.
     A privatefunction is a function residing in a subdirectory with the name private.  Private functions are visible only to functions in the parent directory.  They are useful when we want to limit the scope of a function.  You cannot call the private function from the command line or from functions outside the parent of the private folder. PRIVATE FUNCTIONS