%% Add Legend to Current Axes
% Plot two lines and add a legend to the current axes. Specify the legend labels as input arguments to the legend function.

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on 
y2 = cos(2*x);
plot(x,y2)

legend('cos(x)','cos(2x)')

%
% If you add or delete a data series from the axes, the legend updates accordingly. Control the label for the new data series by setting the DisplayName property as a name-value pair during creation. If you do not specify a label, then the legend uses a label of the form 'dataN'.
% Note: If you do not want the legend to automatically update when data series are added to or removed from the axes, then set the AutoUpdate property of the legend to 'off'.

y3 = cos(3*x);
plot(x,y3,'DisplayName','cos(3x)')
hold off

%
% Delete the legend.

legend('off')

% Copyright 2012 The MathWorks, Inc.