Daniel@0: function adj2pajek2(adj,filename, varargin) Daniel@0: % ADJ2PAJEK2 Converts an adjacency matrix representation to a Pajek .net read format Daniel@0: % adj2pajek2(adj, filename-stem, 'argname1', argval1, ...) Daniel@0: % Daniel@0: % Set A(i,j)=-1 to get a dotted line Daniel@0: % Daniel@0: % Optional arguments Daniel@0: % Daniel@0: % nodeNames - cell array, defaults to {'v1','v2,...} Daniel@0: % shapes - cell array, defaults to {'ellipse','ellipse',...} Daniel@0: % Choices are 'ellipse', 'box', 'diamond', 'triangle', 'cross', 'empty' Daniel@0: % partition - vector of integers, defaults to [1 1 ... 1] Daniel@0: % This will automatically color-code the vertices by their partition Daniel@0: % Daniel@0: % Run pajek (available from http://vlado.fmf.uni-lj.si/pub/networks/pajek/) Daniel@0: % Choose File->Network->Read from the menu Daniel@0: % Then press ctrl-G (Draw->Draw) Daniel@0: % Optional: additionally load the partition file then press ctrl-P (Draw->partition) Daniel@0: % Daniel@0: % Examples Daniel@0: % A=zeros(5,5);A(1,2)=-1;A(2,1)=-1;A(1,[3 4])=1;A(2,5)=1; Daniel@0: % adj2pajek2(A,'foo') % makes foo.net Daniel@0: % Daniel@0: % adj2pajek2(A,'foo','partition',[1 1 2 2 2]) % makes foo.net and foo.clu Daniel@0: % Daniel@0: % adj2pajek2(A,'foo',... Daniel@0: % 'nodeNames',{'TF1','TF2','G1','G2','G3'},... Daniel@0: % 'shapes',{'box','box','ellipse','ellipse','ellipse'}); Daniel@0: % Daniel@0: % Daniel@0: % The file format is documented on p68 of the pajek manual Daniel@0: % and good examples are on p58, p72 Daniel@0: % Daniel@0: % Written by Kevin Murphy, 30 May 2007 Daniel@0: % Based on adj2pajek by Gergana Bounova Daniel@0: % http://stuff.mit.edu/people/gerganaa/www/matlab/routines.html Daniel@0: % Fixes a small bug (opens files as 'wt' instead of 'w' so it works in windows) Daniel@0: % Also, simplified her code and added some features. Daniel@0: Daniel@0: N = length(adj); Daniel@0: for i=1:N Daniel@0: nodeNames{i} = strcat('"v',num2str(i),'"'); Daniel@0: shapes{i} = 'ellipse'; Daniel@0: end Daniel@0: Daniel@0: [nodeNames, shapes, partition] = process_options(varargin, ... Daniel@0: 'nodeNames', nodeNames, 'shapes', shapes, 'partition', []); Daniel@0: Daniel@0: if ~isempty(partition) Daniel@0: fid = fopen(sprintf('%s.clu', filename),'wt','native'); Daniel@0: fprintf(fid,'*Vertices %6i\n',N); Daniel@0: for i=1:N Daniel@0: fprintf(fid, '%d\n', partition(i)); Daniel@0: end Daniel@0: fclose(fid); Daniel@0: end Daniel@0: Daniel@0: fid = fopen(sprintf('%s.net', filename),'wt','native'); Daniel@0: Daniel@0: fprintf(fid,'*Vertices %6i\n',N); Daniel@0: for i=1:N Daniel@0: fprintf(fid,'%3i %s %s\n', i, nodeNames{i}, shapes{i}); Daniel@0: end Daniel@0: Daniel@0: %fprintf(fid,'*Edges\n'); Daniel@0: fprintf(fid,'*Arcs\n'); % directed Daniel@0: for i=1:N Daniel@0: for j=1:N Daniel@0: if adj(i,j) ~= 0 Daniel@0: fprintf(fid,' %4i %4i %2i\n',i,j,adj(i,j)); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: fclose(fid) Daniel@0: Daniel@0: Daniel@0: if 0 Daniel@0: adj2pajek2(A,'foo',... Daniel@0: 'nodeNames',{'TF1','TF2','G1','G2','G3'},... Daniel@0: 'shapes',{'box','box','ellipse','ellipse','ellipse'}); Daniel@0: Daniel@0: N = 100; part = ones(1,N); part(intersect(reg.tfidxTest,1:N))=2; Daniel@0: G = reg.Atest(1:N, 1:N)'; Daniel@0: adj2pajek2(G, 'Ecoli100', 'partition', part) Daniel@0: end Daniel@0: end