error graph with EdgeTable

I would like to plot a graph consistent with the connectivity properties written in 'edge' with a customized width of the edges.
node = [100 -50 -50 -86.6025 0 86.6025;0 86.6025 -86.6025 50 -100 50;0 0 0 200 200 200];
edge = [1 2;2 3;3 1;1 4;2 5;3 6];
type = [1; 1; 1; -1; -1; -1];
EdgeTable = table(edge, ...
'VariableNames','EndNodes');
structure=graph(EdgeTable);
p=plot(structure,'XData', node(1,:), 'YData', node(2,:),'ZData',node(3,:));
p.NodeColor='black';
diameter=20*ones(length(edge),1); % define diameter of struts by using the coefficient
diameter(type>0)=1; % define diameter of cables by using the coefficient
labeledge(p,1:numedges(structure),(1:length(edge)))
p.LineWidth = diameter;
However the result is:
As it can be noted, the grapf doesn.t match the properties of the connectivity as well as the labeledge. Maybe, it happens because the EdgeTable is automatically rewritten by reordering the nodes. Is it right?
How can I fix this problem?

 Risposta accettata

Steven Lord
Steven Lord il 27 Gen 2020
You are correct that the edges stored in the Edges table of the structure variable have been reordered, and so the values in type are not being applied to the edge you think they are. One solution is to add them as additional data to your Edges table.
>> EdgeTable = table(edge, type, 'VariableNames',{'EndNodes', 'type'});
>> structure=graph(EdgeTable)
Now if you look at structure.Edges the type variable has been reordered to match the ordering of the edges. Instead of using type later in your code, use
structure.Edges.type

3 Commenti

Gaetano Pavone
Gaetano Pavone il 27 Gen 2020
Modificato: Gaetano Pavone il 27 Gen 2020
Thanks a lot. Following your suggestion I have edited my code as follows:
node = [100 -50 -50 -86.6025 0 86.6025;0 86.6025 -86.6025 50 -100 50;0 0 0 200 200 200];
edge = [1 2;2 3;3 1;1 4;2 5;3 6];
type = [1; 1; 1; -1; -1; -1];
EdgeName={1:length(edge)};
EdgeTable = table(edge,type,EdgeName, ...
'VariableNames',{'EndNodes','type','EdgeName'});
structure=graph(EdgeTable);
p=plot(structure,'XData', node(1,:), 'YData', node(2,:),'ZData',node(3,:),'EdgeLabel', G.Edges.EdgeName);
p.NodeColor='black';
diameter=20*ones(length(edge),1);% define diameter of struts by using the coefficient
diameter(type>0)=1;% define diameter of cables by using the coefficient
% labeledge(p,1:numedges(structure),(1:length(edge)))
p.LineWidth = diameter;
but it doesn't work...
I know that Edgename should be as:
EdgeName={'1' '2' '3' '4' '5' '6'}';
bu I don't know a smart way to built it. How can I do this?
You can make EdgeName a string array instead of a cell array of characters, which is much easier to construct:
>> string(1:4)
ans =
1×4 string array
"1" "2" "3" "4"
If you're using a sufficiently recent release:
EdgeName = string(1:size(edge, 1)).'

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graph and Network Algorithms in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by