Documentation

  • All
  • Examples
  • Functions
  • More

    • All
    • Examples
    • Functions


    • Trial Software
    • Product Updates

    This is machine translation

    Translated by Microsoft

    Mouseover text to see original. Click the button below to return to the English version of the page.


    Note: This page has been translated by MathWorks. Click here to see
    To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

    Translate This Page

    MathWorks Machine Translation

    The automated translation of this page is provided by a general purpose third party translator tool.

    MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.

    Nested Functions

    What Are Nested Functions?

    A nested function is a function that is completely contained within a parent
    function. Any function in a program file can include a nested function.

    For example, this function named parent contains a nested function
    named nestedfx:

    function parent
    disp('This is the parent function')
    nestedfx function nestedfx disp('This is the nested function') endend

    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.

    Requirements for Nested Functions

    • 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
      feval), or using a function 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 .)

    Sharing Variables Between Parent and Nested Functions

    In general, variables in one function workspace are not available to other functions.
    However, nested functions can access and modify variables in the workspaces of the
    functions that contain them.

    This means that both a nested function and a function that contains it can modify the
    same variable without passing that variable as an argument. For example, in each of
    these functions, main1 and main2, both the main
    function and the nested function can access variable x:

    function main1
    x = 5;
    nestfun1 function nestfun1 x = x + 1; end end
    function main2
    nestfun2 function nestfun2 x = 5; end
    x = x + 1;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 x that cannot interact
    with each other:

    function main nestedfun1 nestedfun2 function nestedfun1 x = 1; end function nestedfun2 x = 2; endend

    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

    • Variables defined within the nested function

    • Variables defined in a parent function, also called externally
      scoped
      variables

    When you create a function handle for a nested function, that handle stores not only
    the name of the function, but also the values of externally scoped variables.

    For example, create a function in a file named makeParabola.m.
    This function accepts several polynomial coefficients, and returns a handle to a nested
    function that calculates the value of that polynomial.

    function p = makeParabola(a,b,c)
    p = @parabola; function y = parabola(x) y = a*x.^2 + b*x + c; endend

    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 onfplot(firstp,range)
    fplot(secondp,range,'r:')
    hold off

    Visibility of Nested Functions

    Every function has a certain scope, that is, a set of other
    functions to which it is visible. A nested function is available:

    • 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 B can call D, 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) % Main functionB(x,y)
      D(y) function B(x,y) % Nested in A C(x) D(y) function C(x) % Nested in B D(x) end end function D(x) % Nested in A E(x) function E(x) % Nested in D disp(x) end endend

    The easiest way to extend the scope of a nested function is to create a function
    handle and return it as an output argument, as shown in Using Handles to Store Function Parameters . Only functions that can call a nested function
    can create a handle to it.

    Related Topics

    • Variables in Nested and Anonymous Functions
    • Create Function Handle
    • Argument Checking in Nested Functions

    MathWorks

    Select a Web Site

    Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

    Select web site

    You can also select a web site from the following list:

    How to Get Best Site Performance

    Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

    Americas

    • América Latina (Español)
    • Canada (English)
    • United States (English)

    Europe

    • Belgium (English)
    • Denmark (English)
    • Deutschland (Deutsch)
    • España (Español)
    • Finland (English)
    • France (Français)
    • Ireland (English)
    • Italia (Italiano)
    • Luxembourg (English)
    • Netherlands (English)
    • Norway (English)
    • Österreich (Deutsch)
    • Portugal (English)
    • Sweden (English)
    • Switzerland
      • Deutsch
      • English
      • Français
    • United Kingdom (English)

    Asia Pacific

    • Australia (English)
    • India (English)
    • New Zealand (English)
    • 中国
      • 简体中文
      • English
    • 日本 (日本語)
    • 한국 (한국어)

    Contact your local office

    • Trial Software
    • Product Updates

    MATLAB Documentation
    • Examples
    • Functions
    • Release Notes
    • PDF Documentation
    Support
    • MATLAB Answers
    • Installation Help
    • Bug Reports
    • Product Requirements
    • Software Downloads
    Introducing Deep Learning with MATLAB

    Introducing Deep Learning with MATLAB

    Download eBook

    MATLAB Answers

    • MATLAB Central

      • Community Home
      • MATLAB Answers
      • File Exchange
      • Cody
      • Blogs
      • ThingSpeak
      • Maker Community
    • Home
    • Ask
    • Answer
    • Browse
    • More

      • Contributors
      • Recent Activity
      • Flagged Content
      • Flagged as Spam
    • Help
    • Trial software

    You are now following this question

    • You will see updates in your activity feed .
    • You may receive emails, depending on your notification preferences .
    Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    Using helper function for multiple callbacks in appdesigner

    Asked by Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    on 29 May 2018

    Latest activity Edited by Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    on 5 Jun 2018

    Accepted Answer by Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    15 views (last 30 days)
    15 views (last 30 days)
    Hi,
    I would like to update the limits of a numeric field (Max Value), when the value of another numeric field (min Value) changes.
    I have multiple min & max value numeric filed sets. I want to create a helper function that would do the above job, when I pass on the component and event information.
    Is it possible? If yes, can someone help me out with it? I’m using matlab 2016b
    Thanks and Best Regards
    Karthik

      0 Comments

    ShowHide all comments

    Sign in to comment.

    Tags

    • appdesigner
    • helper functions
    • event callbacks

    Products

    • MATLAB

    Release

    R2016b

    Related Content


    1 Answer

    Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

      Link

      Direct link to this answer:

      https://www.mathworks.com/matlabcentral/answers/403016-using-helper-function-for-multiple-callbacks-in-appdesigner#answer_323322

      Cancel

    Answer by Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    on 5 Jun 2018

    Edited by Karthik Vemireddy

    Karthik Vemireddy (view profile)


    • 16 questions asked

    • 6 answers

    • 2 accepted answers

    • reputation: 0

    on 5 Jun 2018

     Accepted Answer


    I have figured it out and its quite simple. All you need to do is, pass the component as input argument.
    function HelperFcn(app, Component)
    Selvalue= Component.Value;
    end
    In the function, you can access the elements of the component through the passed variable.
    use
    SelVal=Component.value;
    instead of
    SelVal=app.listbox.value;
    HTH!

      0 Comments

    ShowHide all comments

    Sign in to comment.


    Sign in to answer this question.

    Related Content

    Tags

    • appdesigner
    • helper functions
    • event callbacks

    Products

    • MATLAB

    Release

    R2016b

    Discover what MATLAB® can do for your career.

    Opportunities for recent engineering grads.

    Apply Today

    An Error Occurred

    Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


    MathWorks

    Select a Web Site

    Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

    Select web site

    You can also select a web site from the following list:

    How to Get Best Site Performance

    Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

    Americas

    • América Latina (Español)
    • Canada (English)
    • United States (English)

    Europe

    • Belgium (English)
    • Denmark (English)
    • Deutschland (Deutsch)
    • España (Español)
    • Finland (English)
    • France (Français)
    • Ireland (English)
    • Italia (Italiano)
    • Luxembourg (English)
    • Netherlands (English)
    • Norway (English)
    • Österreich (Deutsch)
    • Portugal (English)
    • Sweden (English)
    • Switzerland
      • Deutsch
      • English
      • Français
    • United Kingdom (English)

    Asia Pacific

    • Australia (English)
    • India (English)
    • New Zealand (English)
    • 中国
      • 简体中文
      • English
    • 日本 (日本語)
    • 한국 (한국어)

    Contact your local office

    • Trial software