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