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

    %% Highlight Node Neighbors
% Create a graph representing a square grid with a side of 8 nodes. Plot
% the graph and return a handle to the |GraphPlot| object, |p|.

% Copyright 2015 The MathWorks, Inc.

n = 8;
A = delsq(numgrid('S',n+2));
G = graph(A,'OmitSelfLoops');
p = plot(G);

%%
% Find the neighbors of node 36. 
n36 = neighbors(G,36)

%%
% Use |highlight| to change the color of node 36 to green, and the color of
% its neighbors and their connecting edges to red.
highlight(p,36,'NodeColor',[0 0.75 0])
highlight(p,n36,'NodeColor','red')
highlight(p,36,n36,'EdgeColor','red')