user001
user001

Reputation: 1848

matlab : suppress legend entry without removing from Plot Browser

One can suppress a legend entry for a line object h by executing h.HandleVisibility='off' or h.Annotation.LegendInformation.IconDisplayStyle='off'. However, both actions also prevent the curve from appearing in Matlab's Plot Browser user interface, and thus display of the curve cannot be interactively toggled.

Is there any way to suppress a legend entry for a given curve without also removing the ability to toggle display of that curve in the Plot Browser user interface?

Upvotes: 3

Views: 9009

Answers (2)

a11
a11

Reputation: 3396

You can also turn off handle visibility. This is way easier than having to set every plot as h1 = ...

Example:

x1 = randperm(10);
y = randperm(10);
x2 = randperm(10);

plot(x1, y, '-', 'Color', 'black', 'HandleVisibility', 'off')
hold on
plot(x2, y, '-', 'Color', 'green', 'DisplayName', 'Put This In Legend')
lgd = legend;
set(lgd, 'Location', 'best')

Upvotes: 2

Cris Luengo
Cris Luengo

Reputation: 60444

MATLAB's legend function accepts an optional argument listing the handles to include in the legend:

figure, hold on
h1 = plot(1,1,'ro');
h2 = plot(1,2,'gx');
h3 = plot(2,1,'m*');
legend([h1,h3]);  % don't make a legend for h2.

Upvotes: 1

Related Questions