checking a matlab function for nested functions

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 comentarios

Rik
Rik el 5 de Oct. de 2019
I don't know how to best integrate this in your work flow, but the option you could resort to is to parse the function headers yourself, and checking where a new function starts before the previous function is closed with an end keyword.
Stephen23
Stephen23 el 6 de Oct. de 2019
"but the option you could resort to is to parse the function headers yourself, and checking where a new function starts..."
Probably not too difficult.
"...before the previous function is closed with an end keyword"
Considering comments, strings, operators, etc., that will not be trivial at all.
Perhaps mtree can help. Note that such meta-programming will be inherently slow and complex.
Alain Barraud
Alain Barraud el 8 de Oct. de 2019
yes of course... it is quite possible. it must be taken into account that when no nested function exists the "end" is not mandatory.
Meantime I have found on the file exchange an old publication which use extensively mlintmex (farg by us). It does much more than asked but answers to the question. If it is no time consuming I'll keep this solution.
If I write a custom solution what is the faster way to find keyword within an m file.
I use the following to read the file.
%get FileName code source
out=splitlines(matlab.internal.getcode.mfile(FullFileName));
Best regards
Alain
Steven Lord
Steven Lord el 8 de Oct. de 2019
I'm curious what you're planning to do with this information. Why do you need to perform this type of analysis?
Alain Barraud
Alain Barraud el 8 de Oct. de 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 el 10 de Oct. de 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
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);

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 8 de Oct. de 2019

0 votos

See the attached function to list all the dependent m-files.

1 comentario

Alain Barraud
Alain Barraud el 9 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Oct. de 2019

Comentada:

el 12 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by