Main Content

addnode

Add new node to graph

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: [3x1 table]
    Nodes: [4x0 table]

G = addnode(G,2)
G = 
  graph with properties:

    Edges: [3x1 table]
    Nodes: [6x0 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: [3x1 table]
    Nodes: [6x1 table]

G = addnode(G,5);
G.Nodes
ans=11×1 table
       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: [3x1 table]
    Nodes: [4x1 table]

G = addnode(G,{'E' 'F'})
G = 
  digraph with properties:

    Edges: [3x1 table]
    Nodes: [6x1 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: [3x1 table]
    Nodes: [4x0 table]

H = addnode(H,{'E','F'});
H.Nodes
ans=6×1 table
      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: [2x1 table]
    Nodes: [3x1 table]

Add a node attribute to indicate whether each airport has free Wi-Fi®.

G.Nodes.WIFI = [false true true]';
G.Nodes
ans=3×2 table
     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 Wi-Fi 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=5×2 table
     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 graph to create an undirected graph or digraph to create a directed graph.

Example: G = graph(1,2)

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

Node names, specified as one or more node names in one of these forms:

  • Single Node — Character vector 'A' or string scalar "A".

  • Multiple Nodes — Cell array of character vectors {'A' 'B' 'C'} or string array ["A" "B" "C"].

Example: H = addnode(G,'A')

Example: H = addnode(G,["A" "B" "C"])

Data Types: char | cell | string

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

Node attributes, specified as a table. NodeProps can contain any number of variables to describe attributes of the graph nodes, but must be able to be concatenated to G.Nodes, so that the result is H.Nodes = [G.Nodes; NodeProps]. For node names, use the variable Name, since this variable name is used by some graph functions. If there is a variable Name, then it must be a cell array of character vectors or string array specifying a unique name in each row. See table for more information on constructing a table.

Data Types: table

Output Arguments

collapse all

Output graph, returned as a graph or digraph object.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2015b