How to find all nested functions in a program?
조회 수: 8 (최근 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!