Add Graph Node Names, Edge Weights, and Other Attributes
This example shows how to add attributes to the nodes and edges in graphs created usinggraph
anddigraph
. You can specify node names or edge weights when you originally callgraph
ordigraph
to create a graph. However, this example shows how to add attributes to a graph after it has been created.
Create Graph
Create a directed graph. The corresponding elements ins
andt
define the source and target nodes of each edge in the graph.
s = [1 1 2 2 3]; t = [2 4 3 4 4]; G = digraph(s,t)
G = digraph with properties: Edges: [5x1 table] Nodes: [4x0 table]
Add Node Names
Add node names to the graph by adding the variable,Name
, to theG.Nodes
table. TheName
variable must be specified as anN
-by-1
cell array of character vectors or string array, whereN = numnodes(G)
. It is important to use theName
variable when adding node names, as this variable name is treated specially by some graph functions.
G.Nodes.Name = {'First''Second''Third''Fourth'}';
View the newNodes
table.
G.Nodes
ans=4×1 tableName __________ {'First' } {'Second'} {'Third' } {'Fourth'}
Use table indexing to view the names of nodes 1 and 4.
G.Nodes.Name([1 4])
ans =2x1 cell{'First' } {'Fourth'}
Add Edge Weights
Add edge weights to the graph by adding the variable,Weight
, to theG.Edges
table. TheWeight
variable must be anM
-by-1 numeric vector, whereM = numedges(G)
. It is important to use theWeight
variable when adding edge weights, as this variable name is treated specially by some graph functions.
G.Edges.Weight = [10 20 30 40 50]';
View the newEdges
table.
G.Edges
ans=5×2 tableEndNodes Weight ________________________ ______ {'First' } {'Second'} 10 {'First' } {'Fourth'} 20 {'Second'} {'Third' } 30 {'Second'} {'Fourth'} 40 {'Third' } {'Fourth'} 50
Use table indexing to view the first and third rows ofG.Edges
.
G.Edges([1 3],:)
ans=2×2 tableEndNodes Weight ________________________ ______ {'First' } {'Second'} 10 {'Second'} {'Third' } 30
Add Custom Attributes
In principle you can add any variable toG.Nodes
andG.Edges
that defines an attribute of the graph nodes or edges. Adding custom attributes can be useful, since functions likesubgraph
andreordernodes
preserve the graph attributes.
For example, add a variable namedPower
toG.Edges
to indicate whether each edge is'on'
or'off'
.
G.Edges.Power = {'on''on''on''off''off'}'; G.Edges
ans=5×3 tableEndNodes Weight Power ________________________ ______ _______ {'First' } {'Second'} 10 {'on' } {'First' } {'Fourth'} 20 {'on' } {'Second'} {'Third' } 30 {'on' } {'Second'} {'Fourth'} 40 {'off'} {'Third' } {'Fourth'} 50 {'off'}
Add a variable named大小
toG.Nodes
to indicate the physical size of each node.
G.Nodes.Size = [10 20 10 30]'; G.Nodes
ans=4×2 tableName Size __________ ____ {'First' } 10 {'Second'} 20 {'Third' } 10 {'Fourth'} 30
Modify Tables with Variables Editor
SinceNodes
andEdges
are both tables, you can use the Variables editor to interactively view or edit the tables. For more information, seeCreate and Edit Variables.
Label Nodes and Edges of Graph Plot
When you plot a graph, you can use the variables inG.Nodes
andG.Edges
to label the graph nodes and edges. This practice is convenient, since these variables are already guaranteed to have the correct number of elements.
Plot the graph and label the edges using thePower
variable inG.Edges
. Label the nodes using the大小
variable inG.Nodes
.
p = plot(G,'EdgeLabel',G.Edges.Power,'NodeLabel',G.Nodes.Size)
p = GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: {'10' '20' '10' '30'} EdgeLabel: {'on' 'on' 'on' 'off' 'off'} XData: [2 1.5000 1 2] YData: [4 3 2 1] ZData: [0 0 0 0] Show all properties