在 MATLAB 中自动生成函数调用图 [英] Automatically generating a diagram of function calls in MATLAB

查看:25
本文介绍了在 MATLAB 中自动生成函数调用图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道可用于在 MATLAB 中自动构建图表的函数调用的工具吗?

Anybody knows of a tool that can be used to automatically build diagrams of function calls in MATLAB?

例如对于给定的函数,该工具将递归地执行函数调用并构建一个二维图,其中节点代表函数,有向边将调用函数与被调用函数连接起来.

E.g. For a given function, the tool would recursively go through function calls and build a 2D graph where nodes would represent functions and directed edges would connect calling functions with called functions.

理想情况下,该工具可以允许用户打开和关闭过滤器以仅包含用户定义的函数、限制递归深度等.

Ideally the tool could allow the user to turn on and off filters to only include user-defined functions, limit the depth of recursion, etc.

我相信 Doxygen 为更传统的 OOP 语言,但我想知道 MATLAB 是否已经存在类似的东西.

I believe Doxygen provides some similar functionality for more traditional OOP languages, but I was wondering if something like this exists already for MATLAB.

谢谢!

推荐答案

您可以使用 gnovice 评论中引用的其他答案中的技术来获取作为 (A,B) 对的函数依赖项列表,其中 A 调用 B.然后安装 GraphViz 并使用它来生成图表.您可以使用类似这样的方法从 Matlab 创建 .dot 文件.

You can use the techniques from those other answers referenced in gnovice's comment to get a list of function dependencies as (A,B) pairs, where A calls B. Then install GraphViz and use it to generate the diagrams. You can create the .dot files from Matlab with something like this.

function createFunctionDependencyDotFile(calls)
%CREATEFUNCTIONDEPENDENCYDOTFILE Create a GraphViz DOT diagram file from function call list
%
% Calls (cellstr) is an n-by-2 cell array in format {caller,callee;...}.
%
% Example:
% calls = { 'foo','X'; 'bar','Y'; 'foo','Z'; 'foo','bar'; 'bar','bar'};
% createFunctionDependencyDotFile(calls)

baseName = 'functionCalls';
dotFile = [baseName '.dot'];
fid = fopen(dotFile, 'w');
fprintf(fid, 'digraph G {
');
for i = 1:size(calls,1)
    [parent,child] = calls{i,:};
    fprintf(fid, '   "%s" -> "%s"
', parent, child);
end
fprintf(fid, '}
');
fclose(fid);

% Render to image
imageFile = [baseName '.png'];
% Assumes the GraphViz bin dir is on the path; if not, use full path to dot.exe
cmd = sprintf('dot -Tpng -Gsize="2,2" "%s" -o"%s"', dotFile, imageFile);
system(cmd);
fprintf('Wrote to %s
', imageFile);

GraphViz 非常适用于许多其他树和图形应用程序,例如类继承和依赖树、数据流等.

GraphViz works great for lots of other tree and graph applications, like class inheritance and dependency trees, data flow, and so on.

这篇关于在 MATLAB 中自动生成函数调用图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆