【问题标题】:Call graph generation from matlab src code从 matlab src 代码调用图生成
【发布时间】:2011-09-14 18:49:59
【问题描述】:

我正在尝试为大约 500 个 matlab src 文件创建一个函数调用图。我找不到任何可以帮助我对多个 src 文件执行相同操作的工具。

有人熟悉任何工具或插件吗?

如果没有任何此类工具可用,关于阅读 6000 行 matlab 代码的任何建议 欢迎没有文档。

【问题讨论】:

    标签: matlab dependencies code-analysis call-graph


    【解决方案1】:

    让我推荐M2HTML,这是一个自动生成 MATLAB m 文件的 HTML 文档的工具。在其功能列表中:

    • 查找函数之间的依赖关系并生成依赖关系图(使用 GraphViz 的点工具)
    • 自动交叉引用函数和子函数及其在源代码中的定义

    查看此demo 页面以查看此工具的输出示例。

    【讨论】:

    • mdot 的帮助给出了这个例子: mdot('m2html.mat','m2html.dot'); !dot -Tps m2html.dot -o m2html.ps !neato -Tps m2html.dot -o m2html.ps
    【解决方案2】:

    我建议研究使用depfun 函数来构建调用图。请参阅http://www.mathworks.com/help/techdoc/ref/depfun.html 了解更多信息。

    特别是,我发现使用'-toponly' 参数调用depfun,然后迭代结果,是手动构建调用图的好方法。不幸的是,我不再能够访问我使用它编写的任何代码。

    【讨论】:

      【解决方案3】:

      我认为您的意思是您想查看您的代码是如何运行的——哪些函数调用了哪些子函数,它们运行的​​时间和时间是多少?

      看看MATLAB Code Profiler。按如下方式执行您的代码:

      >> profile on -history; MyCode; profile viewer
      >> p = profile('info');
      

      p 包含函数历史记录,来自我上面链接的同一个帮助页面:

      历史数据描述了函数在执行过程中进入和退出的顺序。 profile 命令在它返回的结构的FunctionHistory 字段中返回历史数据。历史数据是一个 2×n 数组。第一行包含布尔值,其中0 表示进入函数,1 表示退出函数。第二行通过FunctionTable 字段中的索引标识正在进入或退出的函数。此示例 [下] 读取历史数据并将其显示在 MATLAB 命令行窗口中。

      profile on -history
      plot(magic(4));
      p = profile('info');
      
      for n = 1:size(p.FunctionHistory,2)
       if p.FunctionHistory(1,n)==0
              str = 'entering function: ';
       else
              str = 'exiting function: ';
       end
       disp([str p.FunctionTable(p.FunctionHistory(2,n)).FunctionName])
      end
      

      你不一定需要像上面的例子那样显示入口和出口调用;只需查看 p.FunctionTablep.FunctionHistory 就足以显示代码何时进入和退出函数。

      【讨论】:

        【解决方案4】:

        我同意 m2html 的回答,我只想说以下 m2html/mdot 文档中的示例很好:

        mdot('m2html.mat','m2html.dot');
        !dot -Tps m2html.dot -o m2html.ps
        !neato -Tps m2html.dot -o m2html.ps
        

        但我最好能导出为 pdf:

        mdot('m2html.mat','m2html.dot');
        !dot -Tpdf m2html.dot -o m2html.pdf
        

        此外,在尝试上述命令之前,您必须发出类似以下内容:

        m2html('mfiles','..\some\dir\with\code\','htmldir','doc_dir','graph','on')
        

        【讨论】:

          【解决方案5】:

          我发现 m2html 非常有用(与 Graphviz 软件结合使用)。但是,就我而言,我想创建包含在文件夹中的程序的文档,但忽略了一些子文件夹和 .m 文件。我发现,通过在 m2html 调用中添加“ignoreddir”标志,可以使程序忽略一些子文件夹。但是,我没有找到用于忽略 .m 文件的模拟标志(“ignoreddir”标志也没有完成这项工作)。作为一种解决方法,在 m2html.m 文件的第 1306 行之后添加以下行允许使用“ignoreddir”标志来忽略 .m 文件:

          d = {d{~ismember(d,{ignoredDir{:}})}};
          

          因此,例如,要生成文件夹“program_folder”中包含的程序的 html 文档,但忽略“subfolder_1”子文件夹和“test.m”文件,应该执行以下操作:

          m2html( 'mfiles', 'program_folder', ...  % set program folder
                  'save', 'on', ...  % provide the m2html.mat
                  'htmldir', './doc', ...  % set doc folder
                  'graph', 'on', ...  % produce the graph.dot file to be used for the visualization, for example, as a flux/block diagram
                  'recursive', 'on', ...  % consider also all the subfolders inside the program folders
                  'global', 'on', ...  % link also calls between functions in different folders, i.e., do not link only the calls for the functions which are in the same folder
                  'ignoreddir', { 'subfolder_1' 'test.m' } );  % ignore the following folders/files
          

          请注意,“program_folder”中所有名为“subfolder_1”的子文件夹和所有名为“test.m”的文件都将被忽略。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-11
            • 2014-10-07
            • 2011-09-17
            • 2018-10-25
            • 2017-07-06
            • 1970-01-01
            • 2015-09-09
            • 2016-08-30
            相关资源
            最近更新 更多