This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Label graph nodes





labelnode(H,nodeIDs,Labels) labels the nodes specified by nodeIDs with the character vectors contained in Labels. The lengths of nodeIDs and Labels must be equal, or Labels can be scalar.


collapse all

Create and plot a graph.

s = [1 1 2 2 3 4 5 5];
t = [2 3 3 4 4 5 1 2];
G = digraph(s,t);
h = plot(G);

The plot function automatically labels the graph nodes with their node indices (or with their node names, if available). Use text labels for some of the graph nodes instead.

labelnode(h,[1 2],{'source' 'target'})

Label node 3 and node 4 as 'A'. Since the node labels do not change the underlying graph, G, the labels can contain duplicate names (the official node names in G must always be unique).

labelnode(h,[3 4],'A')

Input Arguments

collapse all

Input graph plot, specified as a GraphPlot object. Use the graph or digraph functions to create a graph, and then use plot with an output argument to return a GraphPlot object.

Example: H = plot(G)

Nodes to label, specified as a vector of node indices, a node name, or a cell array of character vectors containing node names.

Node labels, specified as a character vector or a cell array of character vectors.

  • If Labels is a character vector, then labelnode uses that label for each node specified by nodeIDs.

  • If Labels is a cell array, then it must contain a label for each node specified by nodeIDs.

Data Types: char | cell

Introduced in R2015b

Was this topic helpful?