从特定的matlab工具箱中查找使用的函数

从特定的matlab工具箱中查找使用的函数,matlab,Matlab,我正在迁移代码,需要尽可能减少使用的工具箱数量。例如,我有一个使用多个工具箱的大型脚本文件。我可以用电脑找到这些 [fList,pList] = matlab.codetools.requiredFilesAndProducts('myscript.m'); display({pList.Name}'); 我得到以下结果 'Image Processing Toolbox' 'Instrument Control Toolbox' 'MATLAB' 'Model-Based Calibrat

我正在迁移代码,需要尽可能减少使用的工具箱数量。例如,我有一个使用多个工具箱的大型脚本文件。我可以用电脑找到这些

[fList,pList] = matlab.codetools.requiredFilesAndProducts('myscript.m'); 
display({pList.Name}');
我得到以下结果

'Image Processing Toolbox'
'Instrument Control Toolbox'
'MATLAB'
'Model-Based Calibration Toolbox'
'Signal Processing Toolbox'
'Statistics and Machine Learning Toolbox'
'Parallel Computing Toolbox'
有没有一种简单的方法可以从脚本文件中的特定工具箱中知道使用了哪些函数?例如,我如何知道我的代码中使用了
“基于模型的校准工具箱”
的哪个功能?或者工具箱用于哪一行代码?通过这种方式,我可以尝试自己实现该函数,并避免使用工具箱


注意:我需要在所有本地和嵌套函数中包含工具箱依赖项,以及这些函数中使用的函数(完整依赖项树)。例如,gui文件有许多本地回调函数。

您可以使用半文档化函数获取文件调用的函数名称:

通常,文件可能有子函数,
g
是非标量结构数组
g(1)
表示文件中的主函数,
f
是一个单元格数组,其中包含它调用的函数的名称
f
对每个调用都有一个条目(发生这些调用的行是
g(1).calls.fcnCalls.lines
)。然后,您可以使用以下方法查找这些函数:

其中用于删除重复的函数名。但是,请注意,看到的
函数可能与文件看到的函数不同,具体取决于搜索路径


例如,内置文件
perms.m
给出:

>> 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)

尝试运行,这将告诉您哪些函数导致使用特定工具箱。应该有一个超链接,指向调用这些函数的代码的特定行。谢谢Luis,这太棒了。我假设如果我通过
g
循环,我可以得到所有本地函数(子函数)。然而,这是否会包括更深层次的调用?如果不是的话,唯一的方法是通过一个接一个的依赖并做同样的事情来实现吗?@VaheTshitoyan不,它只包括第一级。在我的示例中,
factorial
例如调用
cumprod
,这里不包括它。您需要手动执行递归,但我不确定这是否可行感谢您的澄清,这看起来像是一个文件交换项目
cellfun(@(x) which(x), unique(f))
>> 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)