Clear Filters
Clear Filters

Error in plotting graph

2 views (last 30 days)
Hi community, please I'm try to plot a graph from a gml file containing data. I'm getting an error when i try to create the graph. The error reads "Error using matlab.internal.graph.MLGraph. Target must be a dense double array of positive integer node indices."
This is my code.
%Extracting edges from gml file graph
fileName = 'power.gml';
inputfile = fopen(fileName);
Edges=[];
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;
Edges(k,2)=str2num(nums{1});
k=k+1;
continue;
end
Edges(k,1)=str2num(nums{1});
l=1;
else
l=0;
continue;
end
if Edges(:,2)== 0
Edges (:,2) = 1;
end
end
Edges = Edges+1;
G = graph(Edges(:,1), Edges(:,2)); % create a graph from A
figure % visualize the graph
plot(G);
  3 Comments
Isaac Osei Agyemang
Isaac Osei Agyemang on 11 Oct 2018
data file can be downloaded from http://www-personal.umich.edu/~mejn/netdata/
file name is Power grid:
Isaac Osei Agyemang
Isaac Osei Agyemang on 11 Oct 2018
Edited: dpb on 11 Oct 2018
@ madhan ravi, I've realized that if the is 0 in either the source or target nodes it troughs that error. I just tried this basic example
s = [1 1 1 2 2 3 3 4 5 5 6 0];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
G = graph(s,t)
plot(G)
If you change the 0 to any number it plots fine but with the 0, it throughs [throws -dpb] the error.
Any idea of how to resolve the problem.

Sign in to comment.

Accepted Answer

Isaac Osei Agyemang
Isaac Osei Agyemang on 12 Oct 2018
Got it working now, did some editing..
%Extracting edges from gml file graph
fileName = 'power.gml';
inputfile = fopen(fileName);
Edges=[];
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=3; % the 0 here was the problem...
Edges(k,2)=str2num(nums{1});
k=k+1;
continue;
end
Edges(k,1)=str2num(nums{1});
l=1;
else
l=0;
continue;
end
end
Edges = Edges+1;
G = graph(Edges(:,1), Edges(:,2)); % create a graph from A
figure % visualize the graph
plot(G);

More Answers (1)

dpb
dpb on 11 Oct 2018
Edited: dpb on 11 Oct 2018
graph(s,t)
...
s,t — Node pairs
...
Node pairs, ... graph creates edges between the corresponding nodes in s and t, ...
If s and t are numeric, then they correspond to indices of graph nodes. Numeric node
indices must be positive integers greater than or equal to 1.
...
IOW, there is no such thing as a "Node zero"; the node numbers are like indices into an array or matrix; they are one-based index values to which node, 1 thru N of the nodes in the list.
I don't know what your idea is of what the zero is supposed to represent?
  1 Comment
Isaac Osei Agyemang
Isaac Osei Agyemang on 12 Oct 2018
Edited: Isaac Osei Agyemang on 12 Oct 2018
I understand you perfectly, but the previous answer you edited isn't working and the idea of zero isn't from me, I'me working with a real dataset i downloaded. I get zero tobe part of the data after extraction. Dataset (Power grid) is available here http://www-personal.umich.edu/~mejn/netdata/.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!