以编程方式 return 所有函数的列表

Programmatically return a list of all functions

我想以编程方式获取当前 MATLAB 命名空间中可用函数的列表,以及包中的可用函数。如何做到这一点?

为此我们可以使用 package metadata:

pkgs = meta.package.getAllPackages();
% Or if the specific package name is known:
mp = meta.package.fromName('matlab')

第 1st 个案例 pkgs 返回的元胞数组包含如下对象:

  package with properties:

                   Name: 'Sldv'
            Description: ''
    DetailedDescription: ''
              ClassList: [29×1 meta.class]
           FunctionList: [8×1 meta.method]
            PackageList: [9×1 meta.package]
      ContainingPackage: [0×0 meta.package]

所以剩下要做的就是遍历包和子包并收集它们的 FunctionList 条目。

我不确定如何获取属于 "default" 命名空间的函数,除了解析 function list doc page, for example using the Python API and BeautifulSoup:

fl = arrayfun(@(x)string(x{1}.string.char), py.bs4.BeautifulSoup( ...
       fileread(fullfile(docroot,'matlab','functionlist-alpha.html')), ...
       'html.parser').find_all("code")).';

进一步 , parsing the function list documentation web page 非常容易,因为 Web 开发人员(当前)使用有用的 "function" class 来标记每个函数名称! HTML:

中的每个函数看起来像这样
<code class="function">accumarray</code>

所以我们可以用urlread抓取源码,用正则表达式去掉每个"function"class项的内部文字:

str = urlread('https://mathworks.com/help/matlab/functionlist-alpha.html');
funcs = regexp( str, '(?<="function">)[0-9A-Za-z.]+', 'match' );

注意:URL中的"alpha"是为了"alphabetical"而不是表示提前测试!

funcs 是一个包含该页面上所有函数名称的元胞数组。

上面使用的页面适用于最新的 MATLAB 版本。对于特定版本,使用结构如下的历史文档页面:

https://mathworks.com/help/releases/R2017b/matlab/functionlist.html