Turn off legend entires?

5 views (last 30 days)
John
John on 30 Jun 2011
Hello, I have an array of line handle objects, and each has a display name. My array is populated similar to this:
hline = [];
hline(1) = line(x1, y1);
set(hline(1), 'DisplayName', 'Line #1');
...
hlegend = legend(hline);
I have many of these line objects all plotted on the same axis, and what I have written is the ability to toggle any of the line objects visibility on or off. What I am trying to do is when I hide a line object, I also want to to remove it from the legend display. I have tried setting the IconDiplsayStyle property to "off" like so:
set(get(get(hline(i),'Annotation'),'LegendInformation',),...
'IconDisplayStyle','off');
legend
This does NOT work however... it simply adds all the objects in the array hline to the legend regardless of what their IconDisplayProperty value is set to. If I give legend an array of strings, then it will hide those objects with IconDisplayValue off, but this is not desireable because I want the correct name for each line object to be stored in its DisplayName field...
Can someone help me to get this working?

Answers (1)

Jan
Jan on 30 Jun 2011
You can create the legend dynamically and define the line handles to show explicitely:
Name = get(hline, 'DisplayName');
isVisible = strcmp(get(hline, 'Visible'), 'on');
legend(hline(isVisible), Name(isVisible));
You can call this from the callback, which toggles the visibility of the lines.

Community Treasure Hunt

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

Start Hunting!