How to find all nested functions in a program?

7 vues (au cours des 30 derniers jours)
Alon Rozen
Alon Rozen le 1 Fév 2017
Modifié(e) : Jan le 1 Fév 2017
Hi all,
I have a complicated and nested functions rich program. After some time of development and evolution the directory which store all the functions (each in its own file) also stores 'dead' functions which the program no longer use. In order to clean the directory from unused functions I need a list of all the necessary ones. So, my question is:
Is there a simple way to extract all the nested functions that the main program and its nested functions call?
I know I can use the Profiler and see what functions were used. But this opens the way to troubles in cases when not all options were chosen while running the program.
Any help will be appreciated,
Alon

Réponses (3)

Guillaume
Guillaume le 1 Fév 2017
You can find nested (and local) functions that matlab thinks are unused with checkcode.
info = checkcode(somefile, '-id')
for inf = info(strcmp(info.id, 'DEFNU'))'
fprintf('%s\n', inf.message);
end

Steven Lord
Steven Lord le 1 Fév 2017
The checkcode-based approach Guillaume suggested is a good idea, but there may be some false positives that checkcode thinks are unused but are used inside eval or as callback functions or something similar. I would combine this with a Profiler based approach using the tests that you've written for your code.
Don't worry if you haven't written tests before. If you know how to write a script file and how to use the assert function you can write script-based tests. If you know how to use the runtests function (which at its most basic you call with no inputs to run all the tests in the directory) you can run script-based tests. Once you start thinking about how to test your code, you may spend more time brainstorming test cases than you will actually implementing the test!

Jan
Jan le 1 Fév 2017
Modifié(e) : Jan le 1 Fév 2017
In addition to the profiler, Matlab's inmem shows a list of used M, Mex and class files:
clear all
... run your code
... prefer exhaustive integration and unit testing
[M, Mex, C] = inmem('-completenames')
% Remove Matlab's own functions:
mroot = matlabroot;
M(strncmpi(M, mroot, length(mroot)) = [];
Mex(strncmpi(M, mroot, length(mroot)) = [];
C(strncmpi(M, mroot, length(mroot)) = [];
This fails, if the brute clear all appears anywhere in the code. This is another reasons, why I recommend to avoid it carefully.
Note that you can use this to identify the used toolboxes also - before the "mroot = ..." section.

Catégories

En savoir plus sur Dialog Boxes dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by