Assign Numerical Node Labels

조회 수: 6 (최근 30일)
Kamal Premaratne
Kamal Premaratne 2020년 7월 2일
댓글: Kamal Premaratne 2020년 7월 5일
I am working with a digraph GG. I need to label the nodes in a different order than the order in which they appear in the adjacency matrix which was used to generate GG. I have these new integer-valued labels in a Nx1 vector called Name (N is the number of nodes). Why is it that I get an error when I use the following command?
>> GG.Nodes.Name = num2str(Name).
I also tried
>> labelnode(GG, 1:N, num2str(Name)),
again, to no avail.
Thank you very much for any advice you can offer.
  댓글 수: 1
Christine Tobler
Christine Tobler 2020년 7월 2일
Can you tell us the error message you're getting?

댓글을 달려면 로그인하십시오.

채택된 답변

Christine Tobler
Christine Tobler 2020년 7월 2일
For the first call, use
>> GG.Nodes.Name = num2str(Name)'
- GG.Nodes.Name has to be a column vector.
For the second call, labelnode only applies labels to a plot of a graph, not to the graph itself. For example
p = plot(GG);
labelnode(p, 1:26, num2str(Name));
But this is usually used to just relabel one or a few of the nodes. To set all of their labels in the plot (but not in the graph object), you would use
plot(GG, 'NodeLabel', num2str(Name));
  댓글 수: 1
Kamal Premaratne
Kamal Premaratne 2020년 7월 2일
Oops, now I realize the mistake I did. I used
num2str(Name')
which generates a row vector instead of
num2str(Name)'
which generates the correct column vector.
Also, good to know the difference between creating a Name column in the Node table and labelnode.
Thank you very much for all your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kamal Premaratne
Kamal Premaratne 2020년 7월 2일
Thanks for the response. Here are the messages:
>> GG.Nodes.Name = num2str(Name)
Error using digraph.validateNodeProperties (line 341)
Name variable in node table must have one column.
Error in digraph/set.Nodes (line 312)
G.NodeProperties = digraph.validateNodeProperties(T);
Error in digraph/subsasgn (line 52)
G = builtin('subsasgn', G, S, V);
>> labelnode(GG, 1:26, num2str(Name)) % N = 26
Check for missing argument or incorrect argument data type in call to function 'labelnode'.
  댓글 수: 1
Kamal Premaratne
Kamal Premaratne 2020년 7월 5일
Hi Christine:
If I may, I would like to follow-up on my question and your response. For this discussion, let me create a simple 4-vertex / 4-edge weighted digraph:
>> start_node = [1 2 3 4]; end_node = [2 3 4 2]; edge_weight = [1 2 3 4];
>> G = digraph(start_node, end_node, edge_weight);
Everything is fine so far. Suppose I want to relabel the default vertex labels [1 2 3 4] to [2 3 4 5]:
>> new_labels = [2 3 4 5]'; % Notice that this is a column vector.
Now I would like to enter this information into the node table as a new column:
>> G.Nodes.Name = num2str(new_labels)
MATLAB does not like this and it spits out the following error.
_____BEGIN_____
Error using digraph.validateName (line 352)
Node names must be a string vector or a cell array of character vectors.
Error in digraph.validateNodeProperties (line 343)
T.Name = digraph.validateName(name);
Error in digraph/set.Nodes (line 312)
G.NodeProperties = digraph.validateNodeProperties(T);
Error in digraph/subsasgn (line 52)
G = builtin('subsasgn', G, S, V);
_____END____
Do you know why? Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by