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 Commenti

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.
"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.
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
I'm curious what you're planning to do with this information. Why do you need to perform this type of analysis?
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
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);

Accedi per commentare.

Risposte (1)

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

1 Commento

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

Accedi per commentare.

Categorie

Scopri di più su Scripts in Centro assistenza e File Exchange

Richiesto:

il 5 Ott 2019

Commentato:

il 12 Ott 2019

Community Treasure Hunt

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

Start Hunting!

Translated by