Starting in R2021a, you can leave an item out of a legend by setting the corresponding label to an empty character vector. For example, plot three lines. Then call the legend function and specify the second legend label as an empty character vector. The corresponding line is omitted from the legend.
plot(rand(3)); legend('Line 1','','Line 3')
Note that this strategy works when you specify just the labels, and not when you specify a set of objects to include in the legend.
For previous releases, you can specify the objects that you want to include as the first input argument to the “legend” function.
For example, plot three lines and return the “Line” objects as array “p”. Include only the first and third lines in the legend by specifying “p(1)” and “p(3)” as the first input argument to “legend”.
p = plot(rand(3)); \n legend([p(1) p(3)],'plot 1','plot 3')
Alternatively, you can set the “IconDisplayStyle” to “off” for the object that you do not want to include in the legend. For example, exclude the second “Line” object, “p(2)”.
p = plot(rand(3)); \n set(get(get(p(2),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); \n legend('plot 1','plot 3')