How to visualize a project structure in MATLAB? [closed]

I've come into ownership of several thousand lines of Matlab code, some as >900 line functions and a few directories full of function_name.m files. It's hard to figure out what everything is doing (or relating to) or figure out the dependencies. What would you suggest to visualize the functions structure, such as what functions are called from which, and in what sequence?


Solution 1:

Port to NumPy.

(Joke.)

Usually in Matlab you have some files written as functions, and some as scripts. Scripts do things like load the data you want to process, and feed it to the functions, and graph it.

To organize things I would start at the top level script and find out which functions do the loading, graphing, processing, etc. Keep the scripts in a top level directory and try to separate the functions out into subdirectories, according to the purpose of the function. Put dependencies of a function into the same subdirectory. Try to make it so that no code in a directory depends on anything in a parent directory (or cousin directory).

Whenever you figure out what a function does and what its arguments are, write a doc comment.

This assumes the person who wrote the code was reasonable. If not, Matlab makes it easy to plunk everything down into one directory and have everything depend on everything else in a rickety tower of code, so you may end up doing a lot of refactoring.

Solution 2:

I have had to deal with this problem many times in my various roles at The MathWorks. This is what I do for the big pieces of MATLAB code:

  1. Back it up, maybe twice!
  2. Select all, Ctrl-I to smart indent
  3. Select all, Ctrl-J to wrap comments

  4. If I am feeling paper-based- Print all the files out, and get a set of highlighters- follow manually, highlighting long term variables and important function calls.

~~~ AND / OR ~~~

5 If I am feeling lucky, start running the code in the debugger, stepping through one line at a time (stepping into subfunctions that were user written)

At this point, I can go through and follow a typical flow through the control structure. I may not have a great idea what everything does, but I have a decent idea of what is going on.

Normally, my goal is to find a bug, solve it and move on. Your goals might be completely different. This is the method that I have used to quickly comprehend hundereds of different pieces of MATLAB code that I have been sent over the years.

Solution 3:

Does your code come with decent help text? In that case, m2html is going to be a great help, since it allows you to create linked html help for easy browsing.

Furthermore, it allows you to make dependency graphs, which help you understand a bit more how you may want to organize the code.