Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
programmatically find nested vs. subfunctions

Subject: programmatically find nested vs. subfunctions

From: Ed Murphy

Date: 4 Apr, 2007 18:55:51

Message: 1 of 9

Does anyone know how to write matlab code that can parse a function
m-file and determine whether the functions defined within it are
nested functions or subfunctions?

Specifically, it would seem that one of the most difficult cases to
distinguish would be when there is a mix of subfunctions and nested
functions.

If you had two functions defined within the primary function and none
of those functions were terminated with an 'end' keyword, you would
know that they must both be subfunctions. But if all functions within
the primary are terminated with 'end', I don't see how one could tell
the difference, yet it must be possible.

Any Thoughts?

Subject: programmatically find nested vs. subfunctions

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Apr, 2007 23:18:35

Message: 2 of 9

In article <ef53653.-1@webcrossing.raydaftYaTP>,
Ed Murphy <murphy294@hotmail.com> wrote:
>Does anyone know how to write matlab code that can parse a function
>m-file and determine whether the functions defined within it are
>nested functions or subfunctions?

>Specifically, it would seem that one of the most difficult cases to
>distinguish would be when there is a mix of subfunctions and nested
>functions.

>If you had two functions defined within the primary function and none
>of those functions were terminated with an 'end' keyword, you would
>know that they must both be subfunctions. But if all functions within
>the primary are terminated with 'end', I don't see how one could tell
>the difference, yet it must be possible.

>Any Thoughts?

You would have to parse the matlab code well enough to match
up all the 'end' to their respective control structures.
If you hit a 'function' statement while you are at a non-zero
nesting depth, then you have a nested function.

When you are doing the parsing, you would need to take
into account that you can place multiple statements on one line
by using semicolon or comma as a seperator. The comma case gets
fun because you have to check the bracket nesting level for
the three different kinds of brackets. And as usual in parsing,
you have to watch out for quoted strings, which is more fun
than usual in matlab because of the transpose operator.

There must be code around that can do it -- e.g., you could
look at the octave code.

Did I remember to mention that you have to figure out
whether you are in an anonymous function?

--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: programmatically find nested vs. subfunctions

From: per isakson

Date: 4 Apr, 2007 21:16:38

Message: 3 of 9

Ed Murphy wrote:
>
>
> Does anyone know how to write matlab code that can parse a function
> m-file and determine whether the functions defined within it are
> nested functions or subfunctions?
>
> Specifically, it would seem that one of the most difficult cases to
> distinguish would be when there is a mix of subfunctions and nested
> functions.
>
> If you had two functions defined within the primary function and
> none
> of those functions were terminated with an 'end' keyword, you would
> know that they must both be subfunctions. But if all functions
> within
> the primary are terminated with 'end', I don't see how one could
> tell
> the difference, yet it must be possible.
>
> Any Thoughts?
  
Here is a couple of lines from my code that does the trick.

saStack = dbstack( '-completenames' );
buff = evalc( 'mlint( ''-calls'', saStack(2).file )' ); % us' tip

inspect the string 'buff'. Thisa is an undocumented option.

/ per

Subject: programmatically find nested vs. subfunctions

From: Steven Lord

Date: 4 Apr, 2007 22:54:35

Message: 4 of 9


"Ed Murphy" <murphy294@hotmail.com> wrote in message
news:ef53653.-1@webcrossing.raydaftYaTP...
> Does anyone know how to write matlab code that can parse a function
> m-file and determine whether the functions defined within it are
> nested functions or subfunctions?

I'm curious ... what application are you implementing where you need or want
this information?

--
Steve Lord
slord@mathworks.com

Subject: programmatically find nested vs. subfunctions

From: per isakson

Date: 4 Apr, 2007 23:22:19

Message: 5 of 9

Steven Lord wrote:
>
>
>
> "Ed Murphy" <murphy294@hotmail.com> wrote in message
> news:ef53653.-1@webcrossing.raydaftYaTP...
>> Does anyone know how to write matlab code that can parse a
> function
>> m-file and determine whether the functions defined within it
are
>> nested functions or subfunctions?
>
> I'm curious ... what application are you implementing where you
> need or want
> this information?
>
> --
> Steve Lord
> slord@mathworks.com
>

I use it in a tool for unit testing / per

Subject: programmatically find nested vs. subfunctions

From: Yair Altman

Date: 5 Apr, 2007 03:35:43

Message: 6 of 9

Ed Murphy wrote:
> Does anyone know how to write matlab code that can parse a function
> m-file and determine whether the functions defined within it are
> nested functions or subfunctions?

Here's how to do it:

>> str = mlintmex('-calls',which(myFunc));
>> [a,b,c,d,e] = regexp(str,'([NS])\d* (\d+) (\d+) (\w+)\n');

forget about a-d: you're only interested in e, which is a cell array
of 1x4 cells - each 1x4 subarray is for a different sub/nested
function and has 4 elements:
1) type: 'N' for nested, 'S' for sub' (you could also modify the
regexp above to get 'U' for called functions)
2) line #
3) column #
4) function name

enjoy!
Yair Altman

Subject: programmatically find nested vs. subfunctions

From: Yair Altman

Date: 5 Apr, 2007 03:46:06

Message: 7 of 9

Yair Altman wrote:
>
>
> Ed Murphy wrote:
>> Does anyone know how to write matlab code that can parse a
> function
>> m-file and determine whether the functions defined within it
are
>> nested functions or subfunctions?
>
> Here's how to do it:
>
>>> str = mlintmex('-calls',which(myFunc));
>>> [a,b,c,d,e] = regexp(str,'([NS])\d* (\d+) (\d+) (\w+)\n');
>
> forget about a-d: you're only interested in e, which is a cell
> array
> of 1x4 cells - each 1x4 subarray is for a different sub/nested
> function and has 4 elements:
> 1) type: 'N' for nested, 'S' for sub' (you could also modify the
> regexp above to get 'U' for called functions)
> 2) line #
> 3) column #
> 4) function name
>
> enjoy!
> Yair Altman

Actually, you might also be interested to know the level of nesting,
if you have multiple levels in your m-files. You can get the level as
follows:

>> str = mlintmex('-calls',which(myFunc));
>> [a,b,c,d,e] = regexp(str,'([NS])(\d+) (\d+) (\d+) (\w+)\n');

Now e consists of 1x5 cells: type,level,line#,col#,name. Note that
mlintmex is undocumented.

Yair Altman

Subject: programmatically find nested vs. subfunctions

From: Ed Murphy

Date: 5 Apr, 2007 16:40:52

Message: 8 of 9

Steven Lord wrote:
>
>
>
> "Ed Murphy" <murphy294@hotmail.com> wrote in message
> news:ef53653.-1@webcrossing.raydaftYaTP...
>> Does anyone know how to write matlab code that can parse a
> function
>> m-file and determine whether the functions defined within it
are
>> nested functions or subfunctions?
>
> I'm curious ... what application are you implementing where you
> need or want
> this information?
>
> --
> Steve Lord
> slord@mathworks.com
>
>
>

I have written a function that will parse a project directory to
extract information for project reports. Currently, it will provide
quatitative information, like the number of code and comment lines as
well as subfunction lists. It also can create text files containing
code only or comments only as well as combinations and dependencies.
Finally, it will also create project summary reports as well.

One of the features that I am working on is the dependency reports. A
function called callingtree can be used to create dependency graphs,
but it doesn't like function files that contain nested functions, so
I would use the information I am seeking to skip those files.

Of course, this information is also useful when documenting your
codes structure.

Many of the features I am trying to add are already available in
applications like m2html, which is a great tool, and Universal
Report, which is a commerical product. These documentation tools are
very useful, but in cases like mine, where I created an application
for my Master's project, where the documentation is ALMOST as
important as the code itself, I need more.

As you know the publish function in Matlab would be a huge step in
creating these reports, but it has the current limitations regarding
function m-files that makes it practically useless to me. The current
version allows you to now publish functions without evaluating, but
what is the point? How is that different than what you could print or
copy and paste?

Thanks for your question and for all the suggestions. I will try them
out.

Thanks again.

Ed

Subject: programmatically find nested vs. subfunctions

From: us

Date: 23 Jun, 2010 13:50:05

Message: 9 of 9

"Ed Murphy" <murphy294@hotmail.com> wrote in message <ef53653.-1@webcrossing.raydaftYaTP>...
> Does anyone know how to write matlab code that can parse a function
> m-file and determine whether the functions defined within it are
> nested functions or subfunctions?
>
> Specifically, it would seem that one of the most difficult cases to
> distinguish would be when there is a mix of subfunctions and nested
> functions.
>
> If you had two functions defined within the primary function and none
> of those functions were terminated with an 'end' keyword, you would
> know that they must both be subfunctions. But if all functions within
> the primary are terminated with 'end', I don't see how one could tell
> the difference, yet it must be possible.
>
> Any Thoughts?

a hint:
- this FEX submission does what you want

http://www.mathworks.com/matlabcentral/fileexchange/15924-farg-a-pedestrian-m-file-parser-showing-all-used-functions-syntax

% this does even a bit more
http://www.mathworks.com/matlabcentral/fileexchange/17291-fdep-a-pedestrian-function-dependencies-finder

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us