您可以使用半文档化的功能getcallinfo
(see 亚尔奥特曼的博客 http://undocumentedmatlab.com/blog/function-definition-meta-info有关它的更多信息):
getcallinfo
Returns 被调用函数以及他们的第一行和最后一行
此功能不受支持,可能会在未经许可的情况下更改或删除
在未来版本中通知。
一般用途getcallinfo
让我们创建一个示例脚本其中包含子功能(这有效在Matlab R2016b中 https://es.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html或更新版本)并将其另存为'filename.m
'。如果有,该过程也有效嵌套函数,或者如果主文件是function而不是脚本。
x = input('');
y = find(x);
z = f(norm(x));
disp(z)
function u = f(v)
u = -log2(v) + log2(pi);
end
Then:
>> g = getcallinfo('filename.m');
给你一个嵌套结构数组包含有趣的信息,包括函数调用。第一个条目,g(1)
, 指主文件。可能还有子函数或嵌套函数的更多条目。在这种情况下,g(2)
指的是子功能f
.
>> g(1).calls.fcnCalls
ans =
struct with fields:
names: {'input' 'find' 'norm' 'disp' 'log2' 'log2' 'pi'}
lines: [1 2 3 4 6 6 6]
>> g(1).calls.innerCalls
ans =
struct with fields:
names: {'f'}
lines: 3
>> g(2).calls.fcnCalls
ans =
struct with fields:
names: {'log2' 'log2' 'pi'}
lines: [6 6 6]
>> g(2).calls.innerCalls
ans =
struct with fields:
names: {1×0 cell}
lines: [1×0 double]
其他领域的g
give 更多细节,比如名字
>> g(1).name
ans =
filename
>> g(2).name
ans =
f
or type
>> g(1).type
ans =
Script with no properties.
>> g(2).type
ans =
subfunction
如何确定给定函数是否在文件中的任何位置使用
Obtain g
如上所述,然后在所有中查找所需的函数名称calls.fcnCalls.names
的领域g
:
g = getcallinfo('filename.m');
sought_function = 'log2'; % or 'eig' in your case
t = arrayfun(@(x) x.calls.fcnCalls.names, g, 'UniformOutput', false);
% collect all names of called functions. Gives a cell array of cell arrays
% of strings (character vectors)
t = [t{:}]; % de-nest: concatenate into cell array of strings
result = any(strcmp(t, sought_function)); % compare with sought function name