%% Add Plots to Axes After Creating Legend
% Plot a line and add a legend to the axes. Then plot another line. The legend automatically updates to include the new line in the legend. Control the label for the new line by setting the DisplayName property as a name-value pair when creating the line.

x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
legend('First Line')

%

hold on 
y2 = cos(x);
plot(x,y2,'DisplayName','Second Line')

%
% Note: If you do not want the legend to automatically update when plots are added to or removed from the axes, then set the AutoUpdate parameter of the legend to 'off', such as legend('First Line','AutoUpdate','off').
% Copyright 2012 The MathWorks, Inc.