How do I change just one text in a legend without also changing the properties of the other text in the legened in MATLAB 7.8 (R2009a)?

13 views (last 30 days)
The text objects in a legend are linked together. When you change the proprties of one text object, for instance, if you make one string bold, the other text objects will become bold as well.
% Make a legend
figure
plot(rand(100,3),'o-');
lh = legend('One','Two','Three','Location','EastOutside');
%%We grab handles 'ch' to the 'Children' of the Legend and use FINDOBJ to
%%find handles 'th' to all the text objects in the legend.
ch = get(lh,'Children');
th = findobj(ch,'Type','text');
%%We grab a handle, 'headerhand', to just the text object containing the
%%string 'Two'. Then we change the font to bold and font size to 14. When we do this, all the text in the legend becomes size 14 and bolded.
headerhand = findobj(th,'String','Two');
set(headerhand,'FontWeight','bold','FontSize',14)
I would like to unlink the text objects so that I can change the text properties of one without also changing the properties of the other text objects.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jan 2010
The text objects in a legend are linked together using Listeners, where every text object listens to every other text object and when the properties of one change, they all change.
To unlink these text objects, use the RMAPPDATA function to remove the Listeners on each text object. To follow the above example, insert the following code right before grabbing a handle to headerhand (right below headerhand = findobj(th,'String','Two');')
%%The text objects are linked to one another using Listeners. We disable
%%(actually, we remove) the listeners using RMAPPDATA so that when we
%%change one of the text objects to be BOLD, the others do not also become
%%BOLD.
for i = 1:length(th)
if (th(i) == headerhand)
rmappdata(th(i),'Listeners');
end
end
Please see the attached file (legend_names.m) for the same example.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!