Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track number of times a function is referenced within a folder/file in MATLAB?

I have a large project going with 40+ functions and it's just increasing every day. Often times I reference a function multiple times from different scripts. Every once in a while, I'll find that I need to edit a function for one script, and then I realize that it's possible that I want that function to stay the same for another script. Obviously this in itself is no problem; I can just write a new function. But sometimes I don't remember if I've referenced that function anywhere else in my larger folder containing all my scripts!

Is there a way in MATLAB to somehow find a count of how many times a function is used within a folder? If so, is there a way to track where it's being referenced from? Thanks in advance =).

like image 236
spaderdabomb Avatar asked Aug 23 '13 08:08

spaderdabomb


2 Answers

For this I typically use the find files funcionality (found in the menu on top of your screen) with the 'contains' option. Especially if your function name does not match common variable names this works very well.

Just search in the entire matlab path, or in the specific directory for something like myFun( and you will see all the places where it is called. In the worst case you will also find some places where it is not called.

like image 187
Dennis Jaheruddin Avatar answered Oct 17 '22 12:10

Dennis Jaheruddin


MATLAB provides support for dependency tracking using the depfun function. depfun tells you which other functions are required to run a given function.

What you're asking is the opposite problem: Which functions require a given function?

Using depfun, you can do a reverse lookup. Here's a quick example:

function result = invdepfun(allFunctions, lookFor)
% Return all functions that depend on a given function
%
% Example: invdepfun({'myfun1', 'myfun2', 'myfun3'}, 'myfun4') returns all of
% 'myfun1', 'myfun2', 'myfun3' that use 'myfun4'.

    filename = which(lookFor);

    result = {};
    for i = 1 : numel(allFunctions)
        deps = depfun(allFunctions{i}, '-quiet');
        if any(strcmpi(deps, filename))
            result{end + 1} = allFunctions{i};
        end
    end
end

You can use various other MATLAB functions (which, dir, etc.) to autmatically compile a list of all your functions to pass to invdepfun as the first argument.

See also this post on File Exchange.

like image 24
Florian Brucker Avatar answered Oct 17 '22 11:10

Florian Brucker