matlab 脚本中是否使用了某些 matlab 例程?
Is certain matlab-routine used in matlab script?
我是 运行 一个很大的 m 文件,不是我自己写的,它依赖于某些子功能。我想知道是否在所有嵌套函数的任何地方都使用了特定函数(在我的例子中是函数 eig.m (计算特征值))。
有快速的方法吗?
亲切的问候,
科恩
您可以使用半文档化函数getcallinfo
(有关它的更多信息,请参阅Yair Altman's blog):
getcallinfo
Returns called functions and their first and last lines
This function is unsupported and might change or be removed without
notice in a future version.
一般使用getcallinfo
让我们创建一个 示例脚本 ,其中包含 子函数 (这适用于 in Matlab R2016b 或更新版本)并将其保存为 'filename.m
'。如果存在 嵌套函数 ,或者如果主文件是 函数 而不是脚本,该过程也适用。
x = input('');
y = find(x);
z = f(norm(x));
disp(z)
function u = f(v)
u = -log2(v) + log2(pi);
end
然后:
>> 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
的其他字段提供更多详细信息,例如姓名
>> g(1).name
ans =
filename
>> g(2).name
ans =
f
或输入
>> g(1).type
ans =
Script with no properties.
>> g(2).type
ans =
subfunction
如何确定给定函数是否在文件中的任何位置使用
按照上面的解释获取g
,然后在g
的所有calls.fcnCalls.names
字段中寻找想要的函数名:
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
手动检查 的一种选择是使用 profiler。
我使用的是 2014 Matlab,我不能 运行 前面的例子。然而,据我在 上看到的,它显示类似的信息,但采用网页样式。
基本上,我还建议您使用探查器,它会列出所有调用的函数。
但给你一个替代方案,如果你只寻找一个函数(请参阅免责声明),你可以使用当前文件夹中的函数优先。因此,假设您想检查是否使用了一个名为 jacobianest
的函数,那么您可以在名为 jacobianest.m
的文件夹中创建一个新函数,例如
function jacobianest(args)
error('jacobianest is being used')
end
那么如果程序因该错误而终止,则您正在使用 jacobianest
。
免责声明 1:这仅适用于非内置功能。从某种意义上说,如果您键入 edit *name of function*
并且只出现评论,则您不能使用此策略。如果代码本身出现,您可以。
我是 运行 一个很大的 m 文件,不是我自己写的,它依赖于某些子功能。我想知道是否在所有嵌套函数的任何地方都使用了特定函数(在我的例子中是函数 eig.m (计算特征值))。 有快速的方法吗?
亲切的问候, 科恩
您可以使用半文档化函数getcallinfo
(有关它的更多信息,请参阅Yair Altman's blog):
getcallinfo
Returns called functions and their first and last lines
This function is unsupported and might change or be removed without notice in a future version.
一般使用getcallinfo
让我们创建一个 示例脚本 ,其中包含 子函数 (这适用于 in Matlab R2016b 或更新版本)并将其保存为 'filename.m
'。如果存在 嵌套函数 ,或者如果主文件是 函数 而不是脚本,该过程也适用。
x = input('');
y = find(x);
z = f(norm(x));
disp(z)
function u = f(v)
u = -log2(v) + log2(pi);
end
然后:
>> 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
的其他字段提供更多详细信息,例如姓名
>> g(1).name
ans =
filename
>> g(2).name
ans =
f
或输入
>> g(1).type
ans =
Script with no properties.
>> g(2).type
ans =
subfunction
如何确定给定函数是否在文件中的任何位置使用
按照上面的解释获取g
,然后在g
的所有calls.fcnCalls.names
字段中寻找想要的函数名:
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
手动检查 的一种选择是使用 profiler。
我使用的是 2014 Matlab,我不能 运行 前面的例子。然而,据我在
基本上,我还建议您使用探查器,它会列出所有调用的函数。
但给你一个替代方案,如果你只寻找一个函数(请参阅免责声明),你可以使用当前文件夹中的函数优先。因此,假设您想检查是否使用了一个名为 jacobianest
的函数,那么您可以在名为 jacobianest.m
的文件夹中创建一个新函数,例如
function jacobianest(args)
error('jacobianest is being used')
end
那么如果程序因该错误而终止,则您正在使用 jacobianest
。
免责声明 1:这仅适用于非内置功能。从某种意义上说,如果您键入 edit *name of function*
并且只出现评论,则您不能使用此策略。如果代码本身出现,您可以。