File Exchange

image thumbnail

plot_subfun

version 1.19.0.0 (21.6 KB) by Christopher Pedersen
plots the subroutines in a function, and their dependencies on each other

3 Downloads

Updated 07 Dec 2016

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

plot_subfun('foo.m') plots a node map of the subroutines in the file foo.m, and their dependencies.
The screenshot is a sample output for the function plot_subfun.m itself.
There is a demo of examples which can be shown with web(publish('demo_subfun'))

Cite As

Christopher Pedersen (2020). plot_subfun (https://www.mathworks.com/matlabcentral/fileexchange/46070-plot_subfun), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (35)

Very nice tool!
I see one space for improvement. With recent MATLAB versions is it possible to call a function from the package and this tool is not sensitive for it.
Calling: out = package1.package2.example_fun();

It would be very helpful to add this feature.

ytoyo

This function is very cool.
Perfectly worked with latest MATLAB releases.

In my case, current version of plot_subfun does not seem to detect annonymized functions.
For example,
@(x)getresidual(x,data)
calls getresidual function through the annonymous function, but I cannot find the getreisudal function in figure made by plot_subfun.

ytoyo

Thanks for catching those, Amro. Code has been fixed.

Amro

Two fixes I made:
- in plot_graph, fix typo s/leyboard/keyboard/
- in plot_subfun>sub_show_unhide, replace "opt.edge.index" with "opt.edge.nodeindex"

Erik

Recommended!
Good Settings: '-extsub','-nomain'

David Sanz

Really great :-) Thanks for sharing!

Hi all,
Apparently Sean has been having issues with this under windows 7, that it only generates the figure if you ask for an output:
plot_subfun('plot_subfun') does nothing.
resu = plot_subfun('plot_subfun'); does generate the figure.
If anyone else is having the same issue, please get in touch.

seth sun

Thanks Christopher!
I have a question. When I use your scripts for GUI, it only plot input and output of the GUI. Is it possible to show the functions list in a GUI?

Sean

Run plot_subfun('plot_subfun','-unhide')
got nothing outputed. Should I expect a tree graph output?

Muhammad

GREAT!!!

Chris FUNG

Thanks Christopher !
Works perfectly now :-)

It would be even more perfect if you could add Matlab Classes ;-)

Bruce, Carl - the problem is that I was a total idiot, and uploaded the wrong file.

I'm absolutely mortified.

This looks like a very nice function, but I get errors:

"Undefined function 'plot_graph' for input arguments of type 'cell'." on line 17 of depfun and line 120 of sub_plot.

Also in the zip file there are only 2 files (plot_depfun and plot_graph)

I'm confused. This function (ID #46070) is supposed to be plot_subfun, right? I downloaded the Zip and got plot_depfun.m and plot_graph.m. Is that what was intended?

Bruce, Paul : I have added an option -extshow that will include external function calls in the figure. It can't "dig in" to those external functions to examine their subroutines... Still pondering how to do this.

Paul

Thanks for the nice, useable submission. I would just like to support the request of Bruce --- All my subfunctions are usually stored in external files, and these subfunctions contain their own subfunctions, also stored in external files. So I would really be able to start using plot_subfun if the calls to external functions are added to the figure.

@bruce. It's possible to add calls to external functions to the figure. I've added your suggestion to the to-do list.

@alexander Thanks for the feedback. I uploaded a new version yesterday that should work with windows32 and folders with space in them. Just for clarification of your last problem : do the figures appear on the screen when you run publish, but they don't make it into the html, or do they not appear at all ?

@Maria. Hi - it looks like you might be trying to call plot_graph directly, without using plot_subfun, is that correct ?
Can you email me directly with the details of what you're trying to do, and I'll see if I can help ?

Bruce

Playing with this a bit more (but not having spent a lot of time looking at the code), how difficult would it be to extend it's reach? In particular, I'm thinking about the case where routine "main_program.m" calls a function called "sub_function" which is stored in "sub_function.m". Is there a way to get plot_subfun to go looking for sub_function.m somewhere on the path and include it's calls in the represented tree?

Hello Christopher,

this is really nice. Unfortunately I experienced some (minor) issues here:

a) In sub_mwdot_call(opt) you get the location of the 'mwdot.exe'. In my case it is located in ...\bin\win32 . The "32" is not automatically included currently and thus an error results. I had to add it by hand:

opt.mwdot.file = fullfile(matlabroot,'bin',[fol, '32'],foo)

b) It seems that the syntax in line 205 does not like folders with spaces in its name (like "New Folder").
opt.mwdot.call = ['"' opt.mwdot.file '" -Tplain ' opt.mwdot.infile];

Could you confirm?

c) For some reason, my R2014b does not display any diagram for the demo. In R2011b this works. There is no error or message at all. Any idea?

Thanks for your submission.

Alexander
Win 7 - R2014b

@Christopher

I am new to MATLAB. I am getting the following error when trying to use your function.
Cell contents reference from a non-cell array object.

Error in plot_graph>sub_mdot_write (line 169)
str{1} = sprintf('digraph %s_calls{',opt.names{1});

Error in plot_graph (line 48)
opt = sub_mdot_write(opt);

Moreover is there some way I can add signs to the arrows? liked signed digraphs, + and -?

Bruce - thanks for the feedback. I have updated the submission with your suggested change. I have confirmed this worked under windows7/2012B.

If anyone else is having problems getting this to run, please get in touch. I'm especially eager to hear if it works under unix.

Bruce

On Windows, line 202 (in sub_mwdot_call) needs fixing. It needs a '"' after the open square bracket and a " after the ' before -Tplain, otherwise it complains bitterly.

Other than that, so far I'm very impressed.

@Richard Ruff.
Thanks for your comment. I have added a bugfix that correctly handles built-in functions. Hope this fixes the issues you are having.

the function actually uses mlintmex, so it must have an m-file to examine. It will use the "which" command to determine which file it should examine.

For example:
plot_subfun('plot_subfun.m')
will give you a plot of the 22 subroutines in the file plot_subfun.m.

plot_subfun('plot')
will tell you there are no subroutines, because the file plot.m is just a script. My function isn't clever enough to realise that plot.m is also a wrapper for a built-in function.

Christopher - Do you have examples to run? Every file I have tried has failed. I haven't investigated in detail, but appears this requires the functions be executed, so the user must replace foo in your comments with a full function call syntax. Is that correct? A few examples will help the user understand the requirements to run this.

Johannes - latest version I believe adresses your comments by adding more options for hiding subroutines.

I change my rating to 5 stars, as my proposed improvement is nearly handled with the options, which i had overlooked at first sight.

Nevertheless, my suggestion would be a nice minor improvement.

This submission is pretty cool.

Just one star missing to encourage the author to improve the submission in the following context:

For large projects, the map may become confusing.
How about declaring levels of detail for substructuring in general and maybe specific to functionnames? It would be great if you could declare an additional list of functionnames, telling plot_subroutines, that "under"/"beneath" these functions, no subfunctions shall be plotted. So the plot could be purified from all those nasty "small" functions.

Regards,
Johannes

Marta - thanks for the feedback. I think the problem you are having is that 'ScriptAtlas.m' is a script, rather than a function, so it doesn't have any subroutines !

I have updated plot_subroutines to handle this situation - it now tells you that there are no subroutines, and returns an empty result.

Marta

This id a great idea. Shame there are several bugs in the code that make it unusable.

E.g. running:
plot_subroutines('ScriptAtlas.m');

gives:
Reference to non-existent field 'name'.
Error in plot_subroutines>sub_calls (line 108)
ext = ~ismember(data.call.name,data.fun.name);
Error in plot_subroutines (line 22)
data = sub_calls(data);

If you fix this error, another one about other non-existing fields appears...

Looking forward to using the code once you have fixed these.

Thanks.

Updates

1.19.0.0

Fixed bug i unhide code (Thanks Amro).

1.18.0.0

Fixed bug in plot_graph where node comments appeared in the wrong node. With thanks to Zi-Xiang WANG for spotting the bug.

1.17.0.0

Added graceful handling of files-not-found, class definitions and scripts when using the -extsub option.

1.16.0.0

added catch code to handle calls to built-in functions

1.15.0.0

added -extsub option to recursively find all subroutines in external functions too.

1.14.0.0

Corrected error : wrong file uploaded

1.13.0.0

added option to show external function calls in the figure

1.12.0.0

bugfix to work under 32-bit windows
bugfix to work in folders with spaces

1.11.0.0

bugfix in plot_graph line 202 : apparently old syntax didn't work on all windows versions.
also fixed line 207 so it gives a meaninful error message !

1.10.0.0

added -unhide option to show hidden function in grey
bugfix to catch an error in the -ends option
added a demo.
changed name to plot_subfun

1.9.0.0

bugfix that handles built-in functions, and functions with no subroutines.

1.8.0.0

added extra options for selectively hiding subroutines.

1.7.0.0

Added handling of scripts and function without subroutines.
figures now have a 3/4" margin to ensure they fit on the useable area of the screen.

1.6.0.0

Changed to matlab toolbox format

1.5.0.0

bugfix : plot_graph was not working on older versions of matlab under windows.

1.4.0.0

bugfix to correctly handle multiple subroutines with the same name.

1.3.0.0

small update to plot_graph

1.2.0.0

added acknowledgements

1.1.0.0

Added handling of files without subroutines

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux