Why we can't directly delete Legend entries?

Hi,
As in question Why we can't directly delete Legend entries from Matlab figure?

Answers (3)

What if you didn't want to delete the curves/lines from the plot, but only wanted certain things deleted from the legend?

7 Comments

Set IconDisplayStyle of the item to off and then legend('show') to update.
There is no IconDisplayStyle property of a line. But I do see it buried under the Annotation.LegendInformation structure of a line object. Thus I can highlight the line and type:
h = gco;
h.Annotation.LegendInformation.IconDisplayStyle='off';
legend('show')
And this does work for a regular plot, but not the mapping toolbox plots.
Hmmmm, I will need to look at this further. I see that linem() does produce the same matlab.graphics.primitive.Line objects, but I agree it does not seem to update using this method.
Odd. I turned off an IconDisplayStyle with no effect on the legend. I linem()'d another line in place with a DisplayName property set, did a legend show, and the legend was updated in a way that replaced the old entry (which should have disappeared before) with the new entry.
Tossed on a couple more entries, turned off an IconDisplayStyle, legend show did not remove it. Deleted a line, legend updated to remove both the deleted entry and the one that had been turned off.
Hmmm, perhaps
legend toggle; legend toggle
?
Okay, got it:
After turning the IconDisplayStyle to off or on,
doMarkDirty( matlab.graphics.shape.internal.AxesLayoutManager.getManager( gca));
Replace the gca with a specific axes if you have one.
I'm not seeing doMarkDirty do anything, but the following works for removing curves from legend of a mapping toolbox figure:
Select curve on the mapping figure, and type this:
h = gco;
h.Annotation.LegendInformation.IconDisplayStyle = 'off';
legend off
legend
@Mike D. Thank you! I was touching up an old plot for a paper: it works!

Sign in to comment.

Image Analyst
Image Analyst on 12 May 2013
Edited: Image Analyst on 12 May 2013
I don't know. You have to do it indirectly, such as by reissuing the legend command with fewer entries, or by calling findobj(), finding the entry you wish to delete, and then calling delete(itemHandle). Calling legend() again is not too bad - you already have the strings, so it's not too much trouble just to leave one off - that's about the same work to do than if you did something like (unsupported) legend('Remove', stringToRemove);
This is an old question, but this works in MATLAB 2015a, delete the lines you want to remove from the legend, delete the legend, and then ctrl-z to bring the legend back, and the legend should come back with only the legend entries removed.

1 Comment

With recent versions of MATLAB, delete the lines you want to remove from the legend, and then command
legend show
to have it update the legend.

Sign in to comment.

Asked:

on 12 May 2013

Commented:

on 28 Jan 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!