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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by