Assigning values to nodes of a graph

22 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 1 月 20 日
コメント済み: Deepa Maheshvare 2020 年 1 月 21 日
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.

採用された回答

Guillaume
Guillaume 2020 年 1 月 20 日
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 件のコメント
Guillaume
Guillaume 2020 年 1 月 20 日
編集済み: Guillaume 2020 年 1 月 20 日
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 2020 年 1 月 21 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by