%% Specify Legend Labels During Plotting Commands
% Plot two lines. Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. Then, add a legend.

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

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

legend

% Copyright 2012 The MathWorks, Inc.