annotate toolboxes/FullBNT-1.0.7/docs/graph_to_dot.m @ 0:cc4b1211e677 tip

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