comparison toolboxes/graph_visualisation/graphViz4Matlab/util/adj2pajek2.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 adj2pajek2(adj,filename, varargin)
2 % ADJ2PAJEK2 Converts an adjacency matrix representation to a Pajek .net read format
3 % adj2pajek2(adj, filename-stem, 'argname1', argval1, ...)
4 %
5 % Set A(i,j)=-1 to get a dotted line
6 %
7 % Optional arguments
8 %
9 % nodeNames - cell array, defaults to {'v1','v2,...}
10 % shapes - cell array, defaults to {'ellipse','ellipse',...}
11 % Choices are 'ellipse', 'box', 'diamond', 'triangle', 'cross', 'empty'
12 % partition - vector of integers, defaults to [1 1 ... 1]
13 % This will automatically color-code the vertices by their partition
14 %
15 % Run pajek (available from http://vlado.fmf.uni-lj.si/pub/networks/pajek/)
16 % Choose File->Network->Read from the menu
17 % Then press ctrl-G (Draw->Draw)
18 % Optional: additionally load the partition file then press ctrl-P (Draw->partition)
19 %
20 % Examples
21 % A=zeros(5,5);A(1,2)=-1;A(2,1)=-1;A(1,[3 4])=1;A(2,5)=1;
22 % adj2pajek2(A,'foo') % makes foo.net
23 %
24 % adj2pajek2(A,'foo','partition',[1 1 2 2 2]) % makes foo.net and foo.clu
25 %
26 % adj2pajek2(A,'foo',...
27 % 'nodeNames',{'TF1','TF2','G1','G2','G3'},...
28 % 'shapes',{'box','box','ellipse','ellipse','ellipse'});
29 %
30 %
31 % The file format is documented on p68 of the pajek manual
32 % and good examples are on p58, p72
33 %
34 % Written by Kevin Murphy, 30 May 2007
35 % Based on adj2pajek by Gergana Bounova
36 % http://stuff.mit.edu/people/gerganaa/www/matlab/routines.html
37 % Fixes a small bug (opens files as 'wt' instead of 'w' so it works in windows)
38 % Also, simplified her code and added some features.
39
40 N = length(adj);
41 for i=1:N
42 nodeNames{i} = strcat('"v',num2str(i),'"');
43 shapes{i} = 'ellipse';
44 end
45
46 [nodeNames, shapes, partition] = process_options(varargin, ...
47 'nodeNames', nodeNames, 'shapes', shapes, 'partition', []);
48
49 if ~isempty(partition)
50 fid = fopen(sprintf('%s.clu', filename),'wt','native');
51 fprintf(fid,'*Vertices %6i\n',N);
52 for i=1:N
53 fprintf(fid, '%d\n', partition(i));
54 end
55 fclose(fid);
56 end
57
58 fid = fopen(sprintf('%s.net', filename),'wt','native');
59
60 fprintf(fid,'*Vertices %6i\n',N);
61 for i=1:N
62 fprintf(fid,'%3i %s %s\n', i, nodeNames{i}, shapes{i});
63 end
64
65 %fprintf(fid,'*Edges\n');
66 fprintf(fid,'*Arcs\n'); % directed
67 for i=1:N
68 for j=1:N
69 if adj(i,j) ~= 0
70 fprintf(fid,' %4i %4i %2i\n',i,j,adj(i,j));
71 end
72 end
73 end
74 fclose(fid)
75
76
77 if 0
78 adj2pajek2(A,'foo',...
79 'nodeNames',{'TF1','TF2','G1','G2','G3'},...
80 'shapes',{'box','box','ellipse','ellipse','ellipse'});
81
82 N = 100; part = ones(1,N); part(intersect(reg.tfidxTest,1:N))=2;
83 G = reg.Atest(1:N, 1:N)';
84 adj2pajek2(G, 'Ecoli100', 'partition', part)
85 end
86 end