Doug’s MATLAB Video Tutorials

February 1st, 2008

MATLAB Basics: Nested functions

In a previous video we covered functions in MATLAB. This week we will be covering nested subfunctions. This style of subfunction allows you to share the workspace of the calling function without explicitly passing variables. Loren did a nice job covering this topic also. [click here] As always, she is looking at more advanced topics in MATLAB than I tend to. You should stop by her main page to see what she is up to each week. [click here]
iconFiles.jpgiconFree.jpgiconPod.jpglazy.jpg

14 Responses to “MATLAB Basics: Nested functions”

  1. quan replied on :

    Nice tutorial Doug. I’ve never used nested subfunctions, but I can see them being very useful.

  2. Maria C Marcano replied on :

    Is there a way to suscribe to these demos as podcasts, or do I need to come and download them from the page each week?
    Thanks!

  3. Doug replied on :

    In iTunes, you can subscribe to the RSS feed and it will find the podcasts that I embed into the pages.

    Doug

  4. adnyana replied on :

    MR. Doug, i want ask you something. I have a problem in doing
    subdivision of segmented image into piecewise of images. but this
    program(code) need much time to complete computation of image 512×512
    pixel(need about 3 thousand second for one iteration). here is the code
    :

    function hasil = Subdivision1(cls,img,img_asli,sigma)

    global Wr pxl kls rw img_asl img_result cpt;

    …..etc….

    [Long code deleted from comment -Doug]

    Mr. Doug please help me
    i need your suggestion

    thanks a lot
    best regard

    adnyana

  5. Doug replied on :

    Adnyana,

    I think that you would want to run the profiler on this code, it will tell you where the slow parts are. From there, we could figure out how to speed it up.

    profiler:
    http://blogs.mathworks.com/pick/2006/10/19/profiler-to-find-code-bottlenecks/

    Doug

  6. adnyana replied on :

    yes sir, i’ve run profiler on this code.
    but the most time was spent on the ‘ismember’ function.

    is there other way or other function that run faster than ‘ismember’.

    i use ‘ismember’ function to check if one set of data is member of other set, like this :

    ismember(ind,kls)== 1 && ismember(ind,pxl) == 0

    thanks a lot sir for helping me

    best regard

    adnyana

  7. adnyana replied on :

    sorry sir the image not appear, here i try again :

    http://img151.imageshack.us/img151/2596/profilersp5.jpg

  8. Doug replied on :

    Adnyana,

    This is great. Now, all I need to know is “What are the typical values of:

    ind
    kls
    pxl

    Are these scalars, vectors, matrices? What are the typical sizes. Do the values change often? If they do not change, can you make these calculations once and then store the value?

    Doug

  9. adnyana replied on :

    ind is scalar, kls and pxl are vectors
    yes of course, all of those variables’ values are change very often, as stated in my code. kls store indexes of one class of image, pxl store indexes of one region of that class.

    here is segment of the code :

    function IncludeInRjPixel_1(ind) %horizontal & vertical
    global Wr pxl kls rw cpt;
    flag = 0;
    % cpt = 0;
    f = 0;
    while cpt index of image matrix
    case 1
    ind = ind + 1;
    case 2
    ind = ind - rw;
    case 3
    ind = ind + rw;
    end;
    if f >=4
    break;
    end;
    end;
    includePixel(pxl); % calculate average of pixel value of index stored in pxl

    this code is called very often until computation is complete

    so i can’t make this calculation once. so what do you think of this case sir?

    thanks sir for your reply

    best regard

    adnyana

  10. Doug replied on :

    Adnyana,

    It looks like you are spending 22 hours on this one function, but you are calling it so often that each call is 0.001 seconds. My guess is this is actually quite efficient. Maybe your algorithm can be changed to call ismember.m less often? ismember.m has some decisions internally to decide which algorithm to use based on the lengths of the inputs. You might be able to tweak these.

    My other suggestion is this looks like a problem that might call for a computer cluster. You can really cut down on the time needed by using more computing power. Check out the Parallel Computing Toolbox.

    Doug

  11. ALT replied on :

    Thanks for this video it was very useful.
    I was using 3 functions, using global variables, but now its just one function with two nested subfunctions.

    You are doing a great job with your videos, they are very helpful especially for beginners like me.

    Altino

  12. Jack replied on :

    Doug,

    Happy new year Doug. To get you prepared for the year ahead, I have an incredibly sexy question which eagerly awaits your touch:

    Basically I have one function and within it is a nested function:

    function func1()
    ….
    function func2()
    ….
    end
    ….
    end

    now, within func1 I use another function (non-nested) which I wish to use to mathematically optimise func2. Now, this gives me error messages, since, it would seem this optimisation function is not able to access func2 since it is a nested function.

    Now, one solution would be for me to make func2 non-nested and write it out in its own separate m-file and then call the optimisation routine from within func1 to optimise the now-non-nested func2. However, this seems like a non-memory efficient kinda way to do this as, I am gonna have to pass so many arguments to func2 (which in its previously nested state were accessible directly without the need to pass).

    Can you help?

    BTW, can we also use pointers in MATLAB? Or do we always need to get functions to return everything we want? (Arguments passing by reference etc..)

  13. Jack replied on :

    managed to get it done long time ago using function handles..don’t bother replying…:-)

  14. dhull replied on :

    @Altino,

    This makes me very happy! I am glad to see you are able to adopt the ideas and improve your code immediately.

    Thanks for watching,

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.