如何在MATLAB中检索.m文件中使用的函数名称?

时间:2015-03-09 16:47:08

标签: matlab

说我有以下MATLAB函数do_this.m

function result = do_this(input1, input2)
% input1 and input2 must have the same size.
    A = input1 + diag(input2);
    result = rand(size(input1)) + A;
end

我想知道如何返回给定.m文件中使用的所有函数的数组。例如,

>> func_names = get_func_names('do_this.m')

func_names['diag', 'rand', 'size']

1 个答案:

答案 0 :(得分:2)

您可以从depfun()获取此类信息。

% See depfun helpfile
[TRACE_LIST, BUILTINS] = depfun('do_this.m');

BUILTINS是函数所需的所有内置函数的单元格数组,并返回类似

的内容
BUILTINS = 

'colon'
'datenummx'
'diag'
'evalin'
'horzcat'
'loadobj'
'numel'
'plus'
'rand'
'saveobj'
'size'
'subsindex'
'subsref'
'vertcat'

请注意,这也会返回diag()rand()等使用的函数

在R2014a中(以及?),MATLAB警告Warning: DEPFUN will be removed in a future release. Use matlab.codetools.requiredFilesAndProducts instead.但是,建议的函数不返回内置函数,只返回用户定义的函数。