Assign Numerical Node Labels

5 次查看(过去 30 天)
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.

采纳的回答

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