问题描述
我正在运行一个我自己没有写的大m文件,这取决于某些子功能.我想知道是否所有嵌套函数中的任何地方都使用了特定函数(在我的情况下,该函数eig.m(用于计算特征值)).有没有一种快速的方法可以做到这一点?
致以诚挚的问候,科恩您可以使用半文档化函数 getcallinfo
(请参见 Yair Altman的博客以获取更多信息)
getcallinfo
的常规用法让我们创建一个包含子功能的示例脚本(此方法或更高版本中为rel =" nofollow noreferrer>),并将其另存为'filename.m
'.如果有嵌套函数,或者主文件是 function 而不是脚本,则该过程也适用.
x = input('');y =查找(x);z = f(范数(x));显示(z)函数u = f(v)u = -log2(v)+ log2(pi);结尾
然后:
>>g = getcallinfo('filename.m');
为您提供一个嵌套结构数组,其中包含有趣的信息,包括函数调用.第一个条目 g(1)
指向主文件.子功能或嵌套功能可能还有其他条目.在这种情况下, g(2)
引用子功能 f
.
>>g(1).calls.fcnCalls回答=带字段的结构:名称:{'input''find''norm''disp''log2''log2''pi'}行:[1 2 3 4 6 6 6]>>g(1).calls.innerCalls回答=带字段的结构:名称:{'f'}行数:3>>g(2).calls.fcnCalls回答=带字段的结构:名称:{'log2''log2''pi'}行:[6 6 6]>>g(2).calls.innerCalls回答=结构与字段:名称:{1×0格}线:[1×0双]
g
的其他字段提供了更多详细信息,例如名称
>>g(1).名称回答=文件名>>g(2).名称回答=F
或键入
>>g(1).类型回答=没有属性的脚本.>>g(2).类型回答=子功能
如何确定文件中任何位置是否使用了给定功能
如上所述获取 g
,然后在 g
的所有 calls.fcnCalls.names
字段中查找所需的函数名称:
g = getcallinfo('filename.m');wanted_function ='log2';%或'eig't = arrayfun(@(x)x.calls.fcnCalls.names,g,'UniformOutput',false);%收集所有被调用函数的名称.给出一个单元格数组字符串百分比(字符向量)t = [t {:}];%嵌套:连接到字符串的单元格数组中结果=任何(strcmp(t,seek_function));与所需功能名称比较的百分比
I am running a big m-file that I didn't write myself and that depends on certain subfunctions. I want to know if anywhere in all nested functions a particular function (in my case the function eig.m (to calculate eigenvalues) ) is used.Is there a quick way to do this?
kind regards,Koen
You can use the semi-documented function getcallinfo
(see Yair Altman's blog for more information about it):
General use of getcallinfo
Let's create an example script which contains subfunctions (this works in Matlab R2016b or newer) and save it as 'filename.m
'. The procedure also works if there are nested functions, or if the main file is a function instead of a script.
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');
gives you a nested struct array with interesting information, including function calls. The first entry, g(1)
, refers to the main file. There may be further entries for subfunctions or nested functions. In this case, g(2)
refers to subfunction 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]
Other fields of g
give further details, such as name
>> 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
How to determine if a given function is used anywhere in the file
Obtain g
as explained above, and then look for the desired function name in all calls.fcnCalls.names
fields of 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
这篇关于matlab脚本中是否使用了某些matlab例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!