comparison toolboxes/FullBNT-1.0.7/docs/graph_to_dot.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function graph_to_dot(adj, varargin)
2 %GRAPH_TO_DOT Makes a GraphViz (AT&T) file representing an adjacency matrix
3 % graph_to_dot(adj, ...) writes to the specified filename.
4 %
5 % Optional arguments can be passed as name/value pairs: [default]
6 %
7 % 'filename' - if omitted, writes to 'tmp.dot'
8 % 'arc_label' - arc_label{i,j} is a string attached to the i-j arc [""]
9 % 'node_label' - node_label{i} is a string attached to the node i ["i"]
10 % 'width' - width in inches [10]
11 % 'height' - height in inches [10]
12 % 'leftright' - 1 means layout left-to-right, 0 means top-to-bottom [0]
13 % 'directed' - 1 means use directed arcs, 0 means undirected [1]
14 %
15 % For details on graphviz, See http://www.research.att.com/sw/tools/graphviz
16 %
17 % See also dot_to_graph and draw_dot.
18
19 % First version written by Kevin Murphy 2002.
20 % Modified by Leon Peshkin, Jan 2004.
21 % Bugfix by Tom Minka, Mar 2004.
22
23 node_label = []; arc_label = []; % set default args
24 width = 10; height = 10;
25 leftright = 0; directed = 1; filename = 'tmp.dot';
26
27 for i = 1:2:nargin-1 % get optional args
28 switch varargin{i}
29 case 'filename', filename = varargin{i+1};
30 case 'node_label', node_label = varargin{i+1};
31 case 'arc_label', arc_label = varargin{i+1};
32 case 'width', width = varargin{i+1};
33 case 'height', height = varargin{i+1};
34 case 'leftright', leftright = varargin{i+1};
35 case 'directed', directed = varargin{i+1};
36 end
37 end
38 % minka
39 if ~directed
40 adj = triu(adj | adj');
41 end
42
43 fid = fopen(filename, 'w');
44 if directed
45 fprintf(fid, 'digraph G {\n');
46 arctxt = '->';
47 if isempty(arc_label)
48 labeltxt = '';
49 else
50 labeltxt = '[label="%s"]';
51 end
52 else
53 fprintf(fid, 'graph G {\n');
54 arctxt = '--';
55 if isempty(arc_label)
56 labeltxt = '[dir=none]';
57 else
58 labeltext = '[label="%s",dir=none]';
59 end
60 end
61 edgeformat = strcat(['%d ',arctxt,' %d ',labeltxt,';\n']);
62 fprintf(fid, 'center = 1;\n');
63 fprintf(fid, 'size=\"%d,%d\";\n', width, height);
64 if leftright
65 fprintf(fid, 'rankdir=LR;\n');
66 end
67 Nnds = length(adj);
68 for node = 1:Nnds % process nodes
69 if isempty(node_label)
70 fprintf(fid, '%d;\n', node);
71 else
72 fprintf(fid, '%d [ label = "%s" ];\n', node, node_label{node});
73 end
74 end
75 for node1 = 1:Nnds % process edges
76 arcs = find(adj(node1,:)); % children(adj, node);
77 for node2 = arcs
78 if ~isempty(arc_label)
79 fprintf(fid, edgeformat,node1,node2,arc_label{node1,node2});
80 else
81 fprintf(fid, edgeformat, node1, node2);
82 end
83 end
84 end
85 fprintf(fid, '}');
86 fclose(fid);