How to find all nested functions in a program?
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
Guillaume
2017-2-1
info = checkcode(somefile, '-id')
for inf = info(strcmp(info.id, 'DEFNU'))'
fprintf('%s\n', inf.message);
end
0 个评论
Steven Lord
2017-2-1
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!
0 个评论
Jan
2017-2-1
编辑:Jan
2017-2-1
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!