Assign Numerical Node Labels

12 views (last 30 days)
Kamal Premaratne on 2 Jul 2020
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 Comment

Christine Tobler on 2 Jul 2020
Can you tell us the error message you're getting?

Christine Tobler on 2 Jul 2020
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 Comment

Kamal Premaratne on 2 Jul 2020
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.

Kamal Premaratne on 2 Jul 2020
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 Comment

Kamal Premaratne on 5 Jul 2020
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.

Translated by