Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

Find functions used from the particular matlab toolbox

I am migrating my code and need to reduce the amount of used toolboxes as much as possible. For instance, I have a large script file that uses several toolboxes. I can find these using

[fList,pList] = matlab.codetools.requiredFilesAndProducts('myscript.m'); 
display({pList.Name}');

I get the following result

'Image Processing Toolbox'
'Instrument Control Toolbox'
'MATLAB'
'Model-Based Calibration Toolbox'
'Signal Processing Toolbox'
'Statistics and Machine Learning Toolbox'
'Parallel Computing Toolbox'

Is there an easy way to know which functions are used from that particular toolbox in my script file? For example, how can I know which function of the 'Model-Based Calibration Toolbox' is used in my code? Or which line of code that toolbox is used at? This way I can try to implement the function myself and avoid using the toolbox.

Note: I need this to include toolbox dependencies in all local and nested functions, as well as function used inside those functions (full dependency tree). For instance a gui file has many local callback functions.

Upvotes: 4

Views: 421

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112769

You can get the names of functions called by a file using the semi-documented function getcallinfo:

g = getcallinfo('filename.m');
f = g(1).calls.fcnCalls.names;

In general, the file may have subfunctions, and g is a non-scalar struct array. g(1) refers to the main function in the file, and f is a cell array with the names of the functions it calls. f has an entry for each call (the lines where those calls occurr are g(1).calls.fcnCalls.lines). You can then locate those functions using which:

cellfun(@(x) which(x), unique(f))

where unique is used to remove duplicate function names. Note, however, that the functions that which sees may not be the same as seen by your file, depending on search paths.


As an example, the built-in file perms.m gives:

>> g = getcallinfo('perms.m')

>> g(1)
ans = 
  struct with fields:

              type: [1×1 internal.matlab.codetools.reports.matlabType.Function]
              name: 'perms'
          fullname: 'perms'
    functionPrefix: 'perms>'
             calls: [1×1 struct]
         firstline: 1
          lastline: 37
          linemask: [61×1 logical]

>> g(2)
ans = 
  struct with fields:

              type: 'subfunction'
              name: 'permsr'
          fullname: 'perms>permsr'
    functionPrefix: 'perms>permsr'
             calls: [1×1 struct]
         firstline: 40
          lastline: 61
          linemask: [61×1 logical]

>> f = g(1).calls.fcnCalls.names
f =
  1×8 cell array
    'cast'    'computer'    'error'    'factorial'    'isequal'    'length'    'message'    'numel'

>> cellfun(@(x) which(x), unique(f))
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\datatypes\cast)
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\general\computer)
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\lang\error)
C:\Program Files\MATLAB\R2016b\toolbox\matlab\specfun\factorial.m
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\isequal)
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\length)
message is a built-in method  % message constructor
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elmat\numel)

Upvotes: 4

Related Questions