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

    %% Display One Legend Entry for Group of Objects
% This example shows how to group sets of lines together and add a legend to 
% the graph that contains one entry for each group.
% 
% Create two groups, |g1| and |g2|. Plot four sine waves and four cosine 
% waves. Group all the sine plot lines together by setting their |Parent| property 
% to |g1|. Group all the cosine plot lines together by setting their |Parent| 
% property to |g2|.

% Copyright 2015 The MathWorks, Inc.


g1 = hggroup;
g2 = hggroup;

t = linspace(0,2*pi,100);
plot(t,sin(t),'b','Parent',g1)
hold on
plot(t,sin(t+1/7),'b','Parent',g1)
plot(t,sin(t+2/7),'b','Parent',g1)
plot(t,sin(t+3/7),'b','Parent',g1)

plot(t,cos(t),'g','Parent',g2)
plot(t,cos(t+1/7),'g','Parent',g2)
plot(t,cos(t+2/7),'g','Parent',g2)
plot(t,cos(t+3/7),'g','Parent',g2)
hold off % reset hold state to off
%% 
% Add a legend with one description for each group of lines.

legend([g1,g2],'sine','cosine')
%% 
% The legend contains two entries, one for each group of lines.