Track the number of function links in a folder / file in MATLAB?

I have a large project with 40 functions, and it just grows every day. Often I refer to a function several times from different scripts. From time to time, I find that I need to edit a function for one script, and then I understand that it is possible that this function will remain the same for another script. Obviously, this is not a problem in itself; I can just write a new function. But sometimes I don’t remember if I referenced this function anywhere else in my larger folder containing all my scripts!

Is there a way in MATLAB to somehow find the number of attempts to use a function inside a folder? If so, is there a way to track what it refers to? Thanks in advance =).

+6
source share
3 answers

For this, I usually use funcionality find find (located on the menu above the screen) with the option 'contains'. Especially if your function name does not match common variable names, this works very well.

Just search all matlab path or in a 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.

+6
source

MATLAB provides support for dependency tracking using the depfun function. depfun indicates what other functions are needed to run this function.

What you are setting is the opposite task: what functions require a given function?

Using depfun , you can do a reverse lookup. Here is 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 automatically compile a list of all your functions in order to go to invdepfun as the first argument.

See also this post in File hosting .

+5
source

I don't know any Matlab built-in functionality that does this, so you probably have to write some function to do this for you.

You can use the DIRWALK function from Matlab FileExchange to scan your project folder and view all Matlab files (use what command) to find the name of your function.

+2
source

Source: https://habr.com/ru/post/952342/


All Articles