如何在 MATLAB 中生成函数依赖项列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/95760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:38:59  来源:igfitidea点击:

How can I generate a list of function dependencies in MATLAB?

functionmatlabdependenciescode-analysis

提问by rmukhopadhyay

In order to distribute a function I've written that depends on other functions I've written that have their own dependencies and so on without distributing every m-file I have ever written, I need to figure out what the full list of dependencies is for a given m-file. Is there a built-in/freely downloadable way to do this?

为了分发我编写的函数,该函数依赖于我编写的其他函数,这些函数有自己的依赖项等等,而不分发我编写的每个 m 文件,我需要弄清楚依赖项的完整列表是什么对于给定的 m 文件。是否有内置/可免费下载的方法来执行此操作?

Specifically I am interested in solutions for MATLAB 7.4.0 (R2007a), but if there is a different way to do it in older versions, by all means please add them here.

具体来说,我对 MATLAB 7.4.0 (R2007a) 的解决方案感兴趣,但如果在旧版本中有不同的方法,请务必在此处添加它们。

采纳答案by Azim

For newer releases of Matlab (eg 2007 or 2008) you could use the built in functions:

对于较新版本的 Matlab(例如 2007 或 2008),您可以使用内置函数:

  1. mlint
  2. dependency report and
  3. coverage report
  1. 米林特
  2. 依赖性报告和
  3. 覆盖率报告

Another option is to use Matlab's profiler. The command is profile, it can also be used to track dependencies. To use profile, you could do

另一种选择是使用 Matlab 的分析器。该命令是 profile,它还可用于跟踪依赖项。要使用配置文件,你可以这样做

>> profile on   % turn profiling on
>> foo;         % entry point to your matlab function or script
>> profile off  % turn profiling off
>> profview     % view the report

If profiler is not available, then perhaps the following two functions are (for pre-MATLAB 2015a):

如果探查器不可用,那么可能有以下两个函数(对于 MATLAB 2015a 之前的版本):

  1. depfun
  2. depdir
  1. 玩得开心
  2. 目录

For example,

例如,

>> deps = depfun('foo');

gives a structure, deps, that contains all the dependencies of foo.m.

给出一个结构 deps,它包含 foo.m 的所有依赖项。

From answers 2, and 3, newer versions of MATLAB (post 2015a) use matlab.codetools.requiredFilesAndProductsinstead.

在答案23 中,使用较新版本的 MATLAB(2015a 之后)matlab.codetools.requiredFilesAndProducts

See answers

查看答案

EDIT:

编辑:

Caveats thanks to @Mike Katz comments

感谢@Mike Katz 评论的警告

  • Remember that the Profiler will only show you files that were actually used in those runs, so if you don't go through every branch, you may have additional dependencies. The dependency report is a good tool, but only resolves static dependencies on the path and just for the files in a single directory.

  • Depfun is more reliable but gives you every possible thing it can think of, and still misses LOAD's and EVAL's.

  • 请记住,Profiler 只会向您显示在这些运行中实际使用的文件,因此如果您没有遍历每个分支,则可能会有其他依赖项。依赖报告是一个很好的工具,但只能解析路径上的静态依赖,并且只针对单个目录中的文件。

  • Depfun 更可靠,但会为您提供它所能想到的所有可能的东西,但仍然会错过 LOAD 和 EVAL。

回答by Jonas Stein

For MATLAB 2015aand later you should preferably look at matlab.codetools.requiredFilesAndProducts

对于MATLAB 2015a后来你最好看看 matlab.codetools.requiredFilesAndProducts

or doc matlab.codetools.requiredFilesAndProducts

或者 doc matlab.codetools.requiredFilesAndProducts

because depfunis marked to be removed in a future release.

因为depfun被标记为在未来的版本中被删除。