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