Convert GML file to adjacency matrix in matlab

4.5k Views Asked by At

I have a GML file of a directed graph (Political blogs). I want to use this graph in Matlab as an adjacency matrix. How can I convert it? Thanks.

2

There are 2 best solutions below

3
On BEST ANSWER

There is a sample code here for this purpose:

%Extracting edges from gml file graph
fileName = 'dolphins.gml';
inputfile = fopen(fileName);
A=[];
l=0;
k=1;
while 1
      % Get a line from the input file
      tline = fgetl(inputfile);
      % Quit if end of file
      if ~ischar(tline)
          break
      end
      nums = regexp(tline,'\d+','match');
      if length(nums)
          if l==1
              l=0;
              A(k,2)=str2num(nums{1});  
              k=k+1;
              continue;
          end
          A(k,1)=str2num(nums{1});
          l=1;
      else
          l=0;
          continue;
      end
end

A[], an [m x 2] matrix, contains the links between nodes.

0
On

With R you can use:

library("multiplex") ## >v1.5 
read.gml(file, as="matrix")