addnode

Add new node to graph

Syntax

Description

example

H = addnode(G,nodeIDs) adds the nodes specified by nodeIDs to graph G. The node names in nodeIDs must not refer to nodes already present in G.

example

H = addnode(G,numNodes) adds a number of new nodes to G equal to numNodes. If G contains nodes with names, then the new nodes are assigned sequential names indicating their row placement in G.Nodes.Name. For example, 'Node5' is located at G.Nodes.Name(5).

example

H = addnode(G,NodeProps) adds new nodes to G with the node properties in NodeProps. One node is added for each row in NodeProps. The NodeProps table must be able to be concatenated to G.Nodes, so that the result is H.Nodes = [G.Nodes; NodeProps].

Examples

collapse all

Add two nodes to a graph that does not have node names.

G = graph([1 2 3],[2 3 4])
G = 

  graph with properties:

    Edges: [3×1 table]
    Nodes: [4×0 table]

G = addnode(G,2)
G = 

  graph with properties:

    Edges: [3×1 table]
    Nodes: [6×0 table]

Add node names to the graph, and then add five additional new nodes. The auto-generated names for the new nodes indicate their placement in G.Nodes.Name.

G.Nodes.Name = {'A' 'B' 'C' 'D' 'E' 'F'}'
G = 

  graph with properties:

    Edges: [3×1 table]
    Nodes: [6×1 table]

G = addnode(G,5);
G.Nodes
ans = 

      Name  
    ________

    'A'     
    'B'     
    'C'     
    'D'     
    'E'     
    'F'     
    'Node7' 
    'Node8' 
    'Node9' 
    'Node10'
    'Node11'

Create a directed graph with named nodes, and then add two named nodes to the graph.

G = digraph({'A' 'B' 'C'},{'D' 'C' 'D'})
G = 

  digraph with properties:

    Edges: [3×1 table]
    Nodes: [4×1 table]

G = addnode(G,{'E' 'F'})
G = 

  digraph with properties:

    Edges: [3×1 table]
    Nodes: [6×1 table]

If the graph does not already have node names, then adding named nodes to the graph automatically generates names for the other nodes.

Create a directed graph without node names, and then add two named nodes to the graph.

H = digraph([1 2 3],[4 3 4])
H = 

  digraph with properties:

    Edges: [3×1 table]
    Nodes: [4×0 table]

H = addnode(H,{'E','F'});
H.Nodes
ans = 

     Name  
    _______

    'Node1'
    'Node2'
    'Node3'
    'Node4'
    'E'    
    'F'    

Create a graph whose nodes represent airports.

G = graph({'JFK' 'LAX'}, {'LAX' 'DEN'})
G = 

  graph with properties:

    Edges: [2×1 table]
    Nodes: [3×1 table]

Add a node attribute to indicate whether each airport has free WIFI.

G.Nodes.WIFI = [false true true]';
G.Nodes
ans = 

    Name     WIFI 
    _____    _____

    'JFK'    false
    'LAX'    true 
    'DEN'    true 

Add two new nodes to the graph by creating a table, NodeProps, containing the node name and WIFI status of each new node. Use addnode to concatenate NodeProps to G.Nodes.

NodeProps = table({'ATL' 'ANC'}', [false true]', ...
    'VariableNames', {'Name' 'WIFI'});
G = addnode(G, NodeProps);

View the modified node table.

G.Nodes
ans = 

    Name     WIFI 
    _____    _____

    'JFK'    false
    'LAX'    true 
    'DEN'    true 
    'ATL'    false
    'ANC'    true 

Input Arguments

collapse all

Input graph, specified as either a graph or digraph object. Use the graph function to create an undirected graph or the digraph function to create a directed graph.

For more information, see graph or digraph.

Example: G = graph(1,2)

Example: G = digraph([1 2],[2 3])

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

Data Types: char | cell

Number of nodes to add, specified as a nonnegative numeric scalar.

Node attributes, specified as a table.

Data Types: table

Output Arguments

collapse all

Output graph, returned as a graph or digraph object.

For more information, see graph or digraph.

See Also

| | |

Introduced in R2015b

Was this topic helpful?