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