MATLAB Answers

checking a matlab function for nested functions

4 views (last 30 days)
Alain Barraud
Alain Barraud on 5 Oct 2019
Commented: Image Analyst on 12 Oct 2019
how to find programatically if a matlab function m file contains nested function.
fileData = mlintmex('-calls',which(fileName));
fileData = regexp(fileData,'[NS](\d+) (\d+) \d+ (\w+)\n','tokens');
gives the list of function within fileName. My question is how to distinguish local functions from nested functions?
Best regards

  7 Comments

Show 4 older comments
Alain Barraud
Alain Barraud on 8 Oct 2019
It is probably not a good practice, however during project I frequently write functions m file containing other local functons. From time to time such local functions become usefull elsewhere. I have written a tools which automatically split such function file in order to have a one to one relation between function name and function file.
Personnaly i don't use nested function, however this must be taken into account.
Further more when I buid the dependence tree of a project main function all used functions appears including the "local" ones which are not initally visible!!
Alain
Alain Barraud
Alain Barraud on 10 Oct 2019
I have found the answer to my question. The undocumented function mtree contains all the necessary infos. The only difficulty is to find where to search. The wrapper getcallinfo, another undocumented function gives a good approach to retrieve what I am asking for, once all the nested struct fields have been visited.
Alain
Image Analyst
Image Analyst on 12 Oct 2019
You could easily find all function definitions by opening the m-file as a text file, using fgetl() to get a line, then using contains() or startsWith() to see if the line contains a function definition
fid = fopen(mFileName, 'rt');
textLine = fgetl(fid);
while ischar(textLine)
if startsWith(strtrim(textLine), 'function ')
% It's a function definition...
end
textLine = fgetl(fid); % Read next line
end
fclose(fid);

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 Oct 2019
See the attached function to list all the dependent m-files.

  1 Comment

Alain Barraud
Alain Barraud on 9 Oct 2019
I use directly matlab.codetools.requiredFilesAndProducts(). However the local functions don't appear, only the main function whose name is attached to file name.

Sign in to comment.