Assigning values to nodes of a graph

20 Ansichten (letzte 30 Tage)
Deepa Maheshvare
Deepa Maheshvare am 20 Jan. 2020
Kommentiert: Deepa Maheshvare am 21 Jan. 2020
I'm assigning values to nodes of a graph
tail = 1:9;
head = 2:10;
G = graph(tail,head);
data = 1:10;
G.Nodes.Value = data';
p = plot(G)
p.Marker = 's';
p.MarkerSize = 7;
p.NodeCData = G.Nodes.Value;
colorbar
The above works fine.
Let's say there are no values for nodes 1 and 10 (head and tail). I'm not sure how to assign values in such a case. I could simply do rmnode(G,[1,10]), but this renumbers all the nodes. I actually plot the graph,G, and visualize the values at each node, so I don't want the nodes to be renumbered.
Any suggestions on how to proceed will be highly appreciated.

Akzeptierte Antwort

Guillaume
Guillaume am 20 Jan. 2020
It sounds like you want to associate a unique ID to each node, one that doesn't change if you alter the graph. You can't use the node number for that as indeed this changes when you add/remove edges or get a subset of the graph.
The best thing is to give a name to the nodes. The node names will then be used automatically for plot as node labels and the names won't change when the graph is edited. Unfortunately, matlab will not let you use numbers as node names, it has to be a string or char vector.
tail = 1:9;
head = 2:10;
G = graph(tail,head);
G.Nodes.Name = string(1:height(G.Nodes))'; %for example
%or G.Nodes.Name = num2cell(char('A' + (0:height(G.Nodes)-1)))'; %for letters
figure; subplot(1, 2, 1);
p1 = plot(G);
G = rmnode(G, [1, 10]);
subplot(1, 2, 2);
p2 = plot(G); %notice that the labels have been preserved
  3 Kommentare
Guillaume
Guillaume am 20 Jan. 2020
Bearbeitet: Guillaume am 20 Jan. 2020
I'm not sure what you mean by the graph breaks.
"is there a way to assign values only to certain nodes"
The Value is a variable of the Nodes table of your own addition. You can set it to whatever you want, matlab will not care. It also won't use it for anything (unlike the Name variable which matlab uses as node labels). If the Value variable is numeric as per your example, it can't be empty but it can be set to NaN.
G.Nodes.Value([1, 5]) = NaN; %this assumes that the variable Value already exist in the Nodes table
Deepa Maheshvare
Deepa Maheshvare am 21 Jan. 2020
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by