MATLAB: Legend command creates too many entries

alldatadata1labelslegendMATLABorderpartialsubset

How do I get the legend to only have a few of the plots and not all of them? I try plotting two plots, creating a legend for the two plots, and then I add another plot and it automatically goes in the legend as 'data1'. How do I not have the last plot in my legend?
Reproduction steps:
>> plot(1:10)
>> hold on
>> plot(1:3:12)
>> legend('plot1', 'plot2')
>> plot(1:2:16)

Best Answer

Putting the legend at the end of all the plots resolves this issue.
>> plot(1:10)
>> hold on
>> plot(1:3:12)
>> plot(1:2:16)
>> legend('plot1', 'plot2')
The legend automatically will only include the number of plots that are included in the legend definition. If plots are added after the legend is created, MATLAB assumes you want those included in your legend and will automatically put them in labeled as 'data1', 'data2', ...
If you would like MATLAB to include a subset of your plots that are not the first consecutive plots, these can be specified as described in the following link: