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

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

问题描述

任何人都知道可以使用MATLAB自动构建函数调用的图表的工具?



例如。对于给定的函数,该工具将递归地执行函数调用并构建一个2D图,其中节点将表示函数,并且有向边将连接调用函数和调用函数。



理想情况下,该工具可以允许用户打开和关闭过滤器,只包括用户定义的功能,限制递归深度等。



我相信 Doxygen 为更传统的OOP 语言,但我想知道如果这样的事情已经存在于MATLAB中。



谢谢!

解决方案

您可以使用gnovice注释中引用的其他答案中的技术来获取函数依赖关系列表(A ,B)对,A呼叫B.然后安装 GraphViz ,并使用它来生成图。您可以使用Matlab创建.dot文件。

  function createFunctionDependencyDotFile(call)
%CREATEFUNCTIONDEPENDENCYDOTFILE创建函数调用列表中的GraphViz DOT图文件

%调用(cellstr)是格式为{caller,callee; ...}的n×2单元格数组。

%示例:
%calls = {'foo','X'; 酒吧, Y; 富, Z; 富,酒吧; 巴,杆};
%createFunctionDependencyDotFile(call)

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

%呈现图像
imageFile = [baseName'.png'];
%假设GraphViz bin目录在路径上;如果没有,请使用完整路径到dot.exe
cmd = sprintf('dot -Tpng -Gsize =2,2%s-o%s',dotFile,imageFile);
system(cmd);
fprintf('写成%s\\\
',imageFile);



GraphViz适用于许多其他树和图形应用程序,如类继承和依赖关系树,数据流等。


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.

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

Thanks!

解决方案

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 {\n');
for i = 1:size(calls,1)
    [parent,child] = calls{i,:};
    fprintf(fid, '   "%s" -> "%s"\n', parent, child);
end
fprintf(fid, '}\n');
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\n', imageFile);

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天全站免登陆