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