www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CreateAndModifyDigraphObjectExample.m

    %% Create and Modify Digraph Object
% Create a |digraph| object with three nodes and three edges. One edge is
% from node 1 to node 2, another is from node 1 to node 3, and the third is
% from node 2 to node 1. 

% Copyright 2015 The MathWorks, Inc.

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

%%
% View the edge table of the graph. For directed graphs, the first column
% indicates the source nodes of each edge, and the second column indicates
% the target nodes.
G.Edges

%%
% Add node names to the graph, then view the new node and edge tables. The
% source and target nodes of each edge are now expressed using their node
% names.
G.Nodes.Name = {'A' 'B' 'C'}';
G.Nodes

%%
G.Edges

%%
% You can add or modify extra variables in the |Nodes| and |Edges| tables
% to describe attributes of the graph nodes or edges. However, you cannot
% directly change the number of nodes or edges in the graph by modifying
% these tables. Instead, use the |addedge|, |rmedge|, |addnode|, or
% |rmnode| functions to modify the number of nodes or edges in a graph.
%
% For example, add an edge to the graph between nodes 2 and 3 and view the
% new edge list.
G = addedge(G,2,3)

%%
G.Edges