GML files in MATLAB

Hi All,
I'm looking to start playing around with graphs in MATLAB - and I keep finding data sets in .gml format.
I can read it fine in a text editor - but we're talking thousands of nodes/edges - does anyone know of a program I can use to convert this into a MATLAB friendly format? I just want an adjacency matrix, and I don't know how I would convert it myself.
Here is a link to a small example of the kind of file I'm talking about: http://www-personal.umich.edu/~mejn/netdata/dolphins.zip
thanks.
Neil.

回答 (4 件)

Walter Roberson
Walter Roberson 2020 年 3 月 18 日

1 投票

S = fileread('dolphins.gml');
nodes = regexp(S, 'node.*?id (?<id>\d+).*?label\s*"(?<label>[^"]*)"', 'names');
edges = regexp(S, 'edge.*?source\s*(?<source>\d+).*?target\s*(?<target>\d+)', 'names');
all_ids = {nodes.id};
all_names = {nodes.label};
all_sources = {edges.source};
all_targets = {edges.target};
[source_found, s] = ismember(all_sources, all_ids);
nfidx = find(~source_found);
if ~isempty(nfidx)
error('Source ids not found in node list, starting from "%s"', edges(nfidx(1).source));
end
[target_found, t] = ismember(all_targets, all_ids);
nfidx = find(~target_found);
if ~isempty(nfidx)
error('Target ids not found in node list, starting from "%s"', edges(nfidx(1).target));
end
EdgeTable = table([s.', t.'], ones(length(s),1), 'VariableNames', {'EndNodes' 'Weight'});
NodeTable = table(all_names.', 'VariableNames',{'Name'});
G = graph(EdgeTable,NodeTable);
plot(G);

4 件のコメント

M Abedi
M Abedi 2021 年 1 月 11 日
thanx alot. this cod work for me. but my graph has longitud and latitude for each node. how i can access them in matlab?
Walter Roberson
Walter Roberson 2021 年 1 月 11 日
At the time you do the plot(), pass in 'XData' and 'YData' name/value pairs. You could have previously stored the data into the NodeData property; https://www.mathworks.com/help/matlab/ref/graph.html#bun70n7-NodeTable
See also
M Abedi
M Abedi 2021 年 1 月 14 日
but my problem is about reading Xdata and Ydata frome gml file.
a part of my gml file is like this:
node [
id 1
label "Brisbane2"
Country "Australia"
Longitude 153.02809
Internal 1
Latitude -27.46794
]
node [
id 2
label "Canberra1"
Country "Australia"
Longitude 149.12807
Internal 1
Latitude -35.28346
]
with the above cod i could read nodes IDs and labels... but dont read nodes Longitudes and Latitudes yet.
Walter Roberson
Walter Roberson 2021 年 1 月 14 日
nodes = regexp(S, 'node.*?id (?<id>\d+).*?label\s*"(?<label>[^"]*)"', 'names');
can be extended to parse the lat and long as well using the same .*?KEYWORD\s*(<TAG>\S+) format.

サインインしてコメントする。

Mahmoud
Mahmoud 2012 年 4 月 28 日

0 投票

You can use the following naive code to extract the edges from the graph and put them in a matrix which can easily be changed to adjacency matrix.
%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[] will be the [m x 2] matrix containing all edges.
Best,
Mahmoud

5 件のコメント

Walter Roberson
Walter Roberson 2012 年 4 月 28 日
str2double() instead of str2num() would be better programming practice (more secure, faster)
mary
mary 2013 年 11 月 10 日
sorry I have used this solution but gives these 3 errors: error in fgets,fgetl and tline= fgetl(inputfile); what is the reason? thank you
Yasaman
Yasaman 2014 年 11 月 2 日
thanks a lot!
Karen Sachs
Karen Sachs 2020 年 3 月 17 日
This is great thank you! Do you know how to extract the names of the variables? This tells me the indicies of the variables but not the variables themselves..
Walter Roberson
Walter Roberson 2020 年 3 月 18 日
See my (new) answer, which graphs the node ids and labels and edge information, and builds a complete Graph object from it.

サインインしてコメントする。

Antonio
Antonio 2014 年 2 月 4 日
編集済み: Walter Roberson 2020 年 3 月 18 日

0 投票

function G = importgml(fileName)
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'); %get number from string
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
G=[];
for i=1:length(A)
G(A(i,1)+1,A(i,2)+1) = 1;
end

3 件のコメント

Antonio
Antonio 2014 年 2 月 4 日
A slight enhancement which returns the adjacency matrix.
hossein
hossein 2016 年 6 月 16 日
Adjacency matrix must be symmetric.
for i=1:length(A)
G(A(i,1)+1,A(i,2)+1) = 1;
G(A(i,2)+1,A(i,1)+1) = 1;
end
Alexander Kocian
Alexander Kocian 2021 年 10 月 6 日
編集済み: Alexander Kocian 2021 年 10 月 6 日
@hossein: this is only true for undirected graphs

サインインしてコメントする。

Mohammad Bhat
Mohammad Bhat 2018 年 2 月 2 日

0 投票

How can I convert an Adjacency Matrix into .gml file

カテゴリ

ヘルプ センター および File ExchangeGraph and Network Algorithms についてさらに検索

質問済み:

2011 年 9 月 7 日

編集済み:

2021 年 10 月 6 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by