Parsing mfunctions with mtree
Given an Nx1 colum vector of strings containing lines of mfunction code, it is possible to use the undocumented function mtree() to parse it into its constituent functions. For example, given,
load Inputs
str1,
the function below will find the starting and ending lines of each of the three function blocks.
[firstLine,lastLine] = functionBlocks(str1)
This works for nested functions as well:
str2,
[firstLine,lastLine] = functionBlocks(str2)
However, I would really like to have the function be able to group the output separately into top-level function blocks and nested function blocks. I know it is possible to do this through a post-analysis of the outputs firstLine and lastLine, but I wonder if it is possible to get information about whether a function block is top-level or nested directly from mtree, i.e., by modifying,
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
Unfortunately, because mtree() is undocumented, it is difficult to fathom its full capabilities. Does anyone know if/how this can be done?
function [firstLine,lastLine] = functionBlocks(str)
T = mtree(strjoin(str,newline));
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
K=fnSet.count;
kset=fnSet.indices;
for k = K:-1:1
fn = fnSet.select(kset(k));
firstLine(k) = fn.lineno;
lastLine(k) = fn.lastone;
end
endGiven an Nx1 colum vector of strings containing lines of mfunction code, it is possible to use the undocumented function mtree() to parse it into its constituent functions. For example, given,
load Inputs
str1,
the function below will find the starting and ending lines of each of the three function blocks.
[firstLine,lastLine] = functionBlocks(str1)
This works for nested functions as well:
str2,
[firstLine,lastLine] = functionBlocks(str2)
However, I would really like to have the function be able to group the output separately into top-level function blocks and nested function blocks. I know it is possible to do this through a post-analysis of the outputs firstLine and lastLine, but I wonder if it is possible to get information about whether a function block is top-level or nested directly from mtree, i.e., by modifying,
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
Unfortunately, because mtree() is undocumented, it is difficult to fathom its full capabilities. Does anyone know if/how this can be done?
function [firstLine,lastLine] = functionBlocks(str)
T = mtree(strjoin(str,newline));
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
K=fnSet.count;
kset=fnSet.indices;
for k = K:-1:1
fn = fnSet.select(kset(k));
firstLine(k) = fn.lineno;
lastLine(k) = fn.lastone;
end
end Given an Nx1 colum vector of strings containing lines of mfunction code, it is possible to use the undocumented function mtree() to parse it into its constituent functions. For example, given,
load Inputs
str1,
the function below will find the starting and ending lines of each of the three function blocks.
[firstLine,lastLine] = functionBlocks(str1)
This works for nested functions as well:
str2,
[firstLine,lastLine] = functionBlocks(str2)
However, I would really like to have the function be able to group the output separately into top-level function blocks and nested function blocks. I know it is possible to do this through a post-analysis of the outputs firstLine and lastLine, but I wonder if it is possible to get information about whether a function block is top-level or nested directly from mtree, i.e., by modifying,
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
Unfortunately, because mtree() is undocumented, it is difficult to fathom its full capabilities. Does anyone know if/how this can be done?
function [firstLine,lastLine] = functionBlocks(str)
T = mtree(strjoin(str,newline));
fnSet = T.mtfind(‘Kind’,’FUNCTION’);
K=fnSet.count;
kset=fnSet.indices;
for k = K:-1:1
fn = fnSet.select(kset(k));
firstLine(k) = fn.lineno;
lastLine(k) = fn.lastone;
end
end mtree, undocumented, parsing, functions MATLAB Answers — New Questions









