gpt4 book ai didi

matlab - matlab 脚本中是否使用了某些 matlab 例程?

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:01 26 4
gpt4 key购买 nike

我正在运行一个不是我自己编写的大 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

关于matlab - matlab 脚本中是否使用了某些 matlab 例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46358432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com