wolffd@0: function graph_to_dot(adj, varargin) wolffd@0: %GRAPH_TO_DOT Makes a GraphViz (AT&T) file representing an adjacency matrix wolffd@0: % graph_to_dot(adj, ...) writes to the specified filename. wolffd@0: % wolffd@0: % Optional arguments can be passed as name/value pairs: [default] wolffd@0: % wolffd@0: % 'filename' - if omitted, writes to 'tmp.dot' wolffd@0: % 'arc_label' - arc_label{i,j} is a string attached to the i-j arc [""] wolffd@0: % 'node_label' - node_label{i} is a string attached to the node i ["i"] wolffd@0: % 'width' - width in inches [10] wolffd@0: % 'height' - height in inches [10] wolffd@0: % 'leftright' - 1 means layout left-to-right, 0 means top-to-bottom [0] wolffd@0: % 'directed' - 1 means use directed arcs, 0 means undirected [1] wolffd@0: % wolffd@0: % For details on graphviz, See http://www.research.att.com/sw/tools/graphviz wolffd@0: % wolffd@0: % See also dot_to_graph and draw_dot. wolffd@0: wolffd@0: % First version written by Kevin Murphy 2002. wolffd@0: % Modified by Leon Peshkin, Jan 2004. wolffd@0: % Bugfix by Tom Minka, Mar 2004. wolffd@0: wolffd@0: node_label = []; arc_label = []; % set default args wolffd@0: width = 10; height = 10; wolffd@0: leftright = 0; directed = 1; filename = 'tmp.dot'; wolffd@0: wolffd@0: for i = 1:2:nargin-1 % get optional args wolffd@0: switch varargin{i} wolffd@0: case 'filename', filename = varargin{i+1}; wolffd@0: case 'node_label', node_label = varargin{i+1}; wolffd@0: case 'arc_label', arc_label = varargin{i+1}; wolffd@0: case 'width', width = varargin{i+1}; wolffd@0: case 'height', height = varargin{i+1}; wolffd@0: case 'leftright', leftright = varargin{i+1}; wolffd@0: case 'directed', directed = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: % minka wolffd@0: if ~directed wolffd@0: adj = triu(adj | adj'); wolffd@0: end wolffd@0: wolffd@0: fid = fopen(filename, 'w'); wolffd@0: if directed wolffd@0: fprintf(fid, 'digraph G {\n'); wolffd@0: arctxt = '->'; wolffd@0: if isempty(arc_label) wolffd@0: labeltxt = ''; wolffd@0: else wolffd@0: labeltxt = '[label="%s"]'; wolffd@0: end wolffd@0: else wolffd@0: fprintf(fid, 'graph G {\n'); wolffd@0: arctxt = '--'; wolffd@0: if isempty(arc_label) wolffd@0: labeltxt = '[dir=none]'; wolffd@0: else wolffd@0: labeltext = '[label="%s",dir=none]'; wolffd@0: end wolffd@0: end wolffd@0: edgeformat = strcat(['%d ',arctxt,' %d ',labeltxt,';\n']); wolffd@0: fprintf(fid, 'center = 1;\n'); wolffd@0: fprintf(fid, 'size=\"%d,%d\";\n', width, height); wolffd@0: if leftright wolffd@0: fprintf(fid, 'rankdir=LR;\n'); wolffd@0: end wolffd@0: Nnds = length(adj); wolffd@0: for node = 1:Nnds % process nodes wolffd@0: if isempty(node_label) wolffd@0: fprintf(fid, '%d;\n', node); wolffd@0: else wolffd@0: fprintf(fid, '%d [ label = "%s" ];\n', node, node_label{node}); wolffd@0: end wolffd@0: end wolffd@0: for node1 = 1:Nnds % process edges wolffd@0: arcs = find(adj(node1,:)); % children(adj, node); wolffd@0: for node2 = arcs wolffd@0: if ~isempty(arc_label) wolffd@0: fprintf(fid, edgeformat,node1,node2,arc_label{node1,node2}); wolffd@0: else wolffd@0: fprintf(fid, edgeformat, node1, node2); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: fprintf(fid, '}'); wolffd@0: fclose(fid);