Chris
Chris

Reputation: 13

How do I retrieve the names of functions used in an .m file in MATLAB?

Say I have the following MATLAB function 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

I would like to know how to return an array of all the functions used in a given .m file. For example,

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

So that func_names is ['diag', 'rand', 'size']

Upvotes: 1

Views: 173

Answers (1)

alexforrence
alexforrence

Reputation: 2744

You can get this sort of information from depfun().

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

BUILTINS is a cell array of all built-in functions needed by your function, and returns something like

BUILTINS = 

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

Note that this also returns functions used by diag(), rand(), etc.

In R2014a (and up?), MATLAB warns Warning: DEPFUN will be removed in a future release. Use matlab.codetools.requiredFilesAndProducts instead. However, the suggested function does not return built-in functions, only user-defined ones.

Upvotes: 2

Related Questions