From: ItsLouieD on
Consider a m-file called add.m, which looks like this on the inside:

%===== start add.m
function m = add
....
end

function n = plusit
....
end

function p = minusit
....
end
%===== end add.m

Is there a way for me to call "minusit" from outside add.m? I've
looked around and it doesn't look like it's possible but I'd
appreciate any input on this.

LD
From: dpb on
ItsLouieD wrote:
> Consider a m-file called add.m, which looks like this on the inside:
>
> %===== start add.m
> function m = add
> ...
> end
>
> function n = plusit
> ...
> end
>
> function p = minusit
> ...
> end
> %===== end add.m
>
> Is there a way for me to call "minusit" from outside add.m? I've
> looked around and it doesn't look like it's possible but I'd
> appreciate any input on this.

See the section on using function handles w/ nested functions in the
user guide programming chapter

--
From: Matt Fig on
ItsLouieD <ldubegm(a)gmail.com> wrote in message <eb9626a9-8959-49b3-8373-63bd5b1f5ae9(a)e29g2000prn.googlegroups.com>...
> Consider a m-file called add.m, which looks like this on the inside:
>
> %===== start add.m
> function m = add
> ...
> end
>
> function n = plusit
> ...
> end
>
> function p = minusit
> ...
> end
> %===== end add.m
>
> Is there a way for me to call "minusit" from outside add.m? I've
> looked around and it doesn't look like it's possible but I'd
> appreciate any input on this.
>
> LD

As written those are subfunctions, not nested functions.
From: ItsLouieD on
On Jun 29, 12:00 pm, "Matt Fig" <spama...(a)yahoo.com> wrote:
> ItsLouieD <ldub...(a)gmail.com> wrote in message <eb9626a9-8959-49b3-8373-63bd5b1f5...(a)e29g2000prn.googlegroups.com>...
> > Consider a m-file called add.m, which looks like this on the inside:
>
> > %===== start add.m
> > function m = add
> > ...
> > end
>
> > function n = plusit
> > ...
> > end
>
> > function p = minusit
> > ...
> > end
> > %===== end add.m
>
> > Is there a way for me to call "minusit" from outside add.m? I've
> > looked around and it doesn't look like it's possible but I'd
> > appreciate any input on this.
>
> > LD
>
> As written those are subfunctions, not nested functions.- Hide quoted text -
>
> - Show quoted text -

Yes I realize that I made a mistake. The example in the first post are
subfunctions. While on the subject, can THOSE be accessed from outside
of the m-file they're residing?

A proper example for nested functions:

%===== start add1.m
function m = add1
....

function n = plusit1
...
end

function p = minusit1
...
end

end
%===== end add1.m

In the documentation, "Using Function Handles with Nested Functions",
the following example is provided:

function h = getCubeHandle
h = @findCube; % Function handle constructor

function cube = findCube(X) % Nested function
cube = X .^ 3;
end
end

I would like to get away from specifying the handle inside the main
function (h = @findCube). I'm assuming I know the name of the nested
function I'm going after from the get-go (in this particular case this
assumption is okay, usually I wouldn't recommend it).

According to the documentation, str2func CANNOT access nested
functions. Can it access subfunctions?

gt = str2func('add/minusit');
gt()

Thanks for the help!
From: per isakson on
ItsLouieD <ldubegm(a)gmail.com> wrote in message <291d4a99-88b1-4dd2-b835-4d96913db8f3(a)w31g2000yqb.googlegroups.com>...
> On Jun 29, 12:00 pm, "Matt Fig" <spama...(a)yahoo.com> wrote:
> > ItsLouieD <ldub...(a)gmail.com> wrote in message <eb9626a9-8959-49b3-8373-63bd5b1f5...(a)e29g2000prn.googlegroups.com>...
> > > Consider a m-file called add.m, which looks like this on the inside:
> >
> > > %===== start add.m
> > > function m = add
> > > ...
> > > end
> >
> > > function n = plusit
> > > ...
> > > end
> >
> > > function p = minusit
> > > ...
> > > end
> > > %===== end add.m
> >
> > > Is there a way for me to call "minusit" from outside add.m? I've
> > > looked around and it doesn't look like it's possible but I'd
> > > appreciate any input on this.
> >
> > > LD
> >
> > As written those are subfunctions, not nested functions.- Hide quoted text -
> >
> > - Show quoted text -
>
> Yes I realize that I made a mistake. The example in the first post are
> subfunctions. While on the subject, can THOSE be accessed from outside
> of the m-file they're residing?
>
> A proper example for nested functions:
>
> %===== start add1.m
> function m = add1
> ...
>
> function n = plusit1
> ...
> end
>
> function p = minusit1
> ...
> end
>
> end
> %===== end add1.m
>
> In the documentation, "Using Function Handles with Nested Functions",
> the following example is provided:
>
> function h = getCubeHandle
> h = @findCube; % Function handle constructor
>
> function cube = findCube(X) % Nested function
> cube = X .^ 3;
> end
> end
>
> I would like to get away from specifying the handle inside the main
> function (h = @findCube). I'm assuming I know the name of the nested
> function I'm going after from the get-go (in this particular case this
> assumption is okay, usually I wouldn't recommend it).
>
> According to the documentation, str2func CANNOT access nested
> functions. Can it access subfunctions?
>
> gt = str2func('add/minusit');
> gt()
>
> Thanks for the help!

Have alook at Lorens blog ( http://blogs.mathworks.com/loren/ )

Some Ways to Create Function Handles (2006-11-17)

and the comments to that blog.

The function must be in scope when the function_handle is created. Thus, it must be done from "inside".

/ per