Branch removal cause error in plotting graph with coordinates

4 views (last 30 days)
Mohsen Mazidi
Mohsen Mazidi on 29 Jul 2021
Answered: Devyani Maladkar on 25 Aug 2021
Hi
I have a graph of 124 edges and 125 vertices, then I remove some branches randomly:
b=branch;
idx=randperm(length(branch),40);
b(idx,:)=[];
then when I want to plot the graph:
p=position;
node=p(:,1);
x=p(:,2);
y=p(:,3);
G=graph(b(:,1)',b(:,2)',b(:,3)');
plot(G,'XData',x,'YData',y,'EdgeLabel',G.Edges.Weight,'NodeLabel',node)
I got the following error:
Expected XData to be an array with number of elements equal to 124.
I know its because graph function reduces number of nodes, but I don't want to, my node coordinates are fixed. Please would anyone give me some help?

Answers (1)

Devyani Maladkar
Devyani Maladkar on 25 Aug 2021
Hello,
I understand from your question that you are plotting a graph and want to remove some branches randomly. Since you have a fixed number of nodes and node coordinates you don't want to remove the nodes, which results in the error.
Since you have fixed node coordinates, you can specify beforehand the total number of nodes. This can be done by specifying when graph object is created as below:
G=graph(b(:,1)',b(:,2)',b(:,3)',total_nodes);
The extra nodes are disconnected from the primary connected component.
For more information on creating the graph object using the above argument you can refer to the documentation under the section Edge List Construction with Extra Nodes. The documentation link refers to R2021a release but will work for R2015b release as well.
If you would like to refer to archived documentation, you can access it from the following link: https://www.mathworks.com/help/doc-archives.html .
For access to earlier releases, please contact support.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!