Help with legend children handles

So I needed to change the style/color/symbol of the legend entries. I found out that (in normal conditions) for each legend entry there are 3 children handles, the first is the marker, second is the line, and third is the text. So I get these 3 handles this way:
Hchilds = get(hleg,'Children'); % Where hleg is the legend handle.
So when everything is working normal I can access the legend graphic objects and edit as desired in order to control the legend as I please.
I do this inside a group of plotting functions that I call and so on, but, SOMETIMES (don't know exactly what triggers this behavior), while I am calling this SAME set of functions repeatedly (trying out different plot settings for instance), suddenly there are not 3 children handles for each entry in the legend, but 8. My very vague guess is that, somewhere along the many functions I call, something triggers an option or something that changes the legend (or axes) behavior.
Inside the axes properties I have (for a plot with just 1 line) 8 children instead of the normal 1 I should have. I can't trace the problem easily since I think it can happen calling the same set of functions X number of times. A number of calls will go alright and then suddenly this changes and my function that changes legend looks crashes because its treating a text handle as a line handle (since the order in the handles is not right anymore).
A solution I found was to get the legend children handles through:
Hchilds = legprop.UserData.LabelHandles; % (here I do have 3 handles per legend entry even when the thing is behaving strangely)
Where legprop = get(hleg); % Where hleg is the legend handle.
But I thought the first approach was more appropiate or direct, and, although the solution I found seems to work, I would like (out of curiosity and not to lose faith) what is going on.
I know I should post code but the thing is the whole code is a lot more complicated than the actual problem, and what is curious is that the problem triggers while I am still calling the same routine over and over. Once this weird behavior is triggered, the complete Matlab session will behave this way (until I restart the whole program) even if I do "clear all", "close all", clc, everything. Once triggered, I get 8 children when running this:
plot(1:10); hleg = legend('L1'); hch = get(hleg,'Children');
And then, length(hch) is 8 instead of 3.
What I hope is that maybe someone that knows a lot about these things maybe says "hey, these are hidden handles that appear when you put option X in manual" or something like it.
Thanks in advance and sorry if the post is confusing.
JP

5 Comments

Juan P. Viera
Juan P. Viera on 29 Jan 2014
Edited: Juan P. Viera on 29 Jan 2014
I want to point out that I know another way to find the handles for the lines in the legend:
linesinleg = findobj(hleg,'type','line');
However, this saves the situation just if I want to change the marker or line color, but if I want to edit something in the text entry, when I get those 8 handles, 6 of them are 'type' 'text' so to identify the actual one that I need is a bit more complicated.
Well I found that there are in fact hidden handles, and if I run:
set(0,'ShowHiddenHandles','on');
I trigger the behavior I was describing above. So that is what may have been happening. However, I still don't get where may this line have been run because I checked the code thoroughly and, at least in my functions, there is no such thing. Maybe there is a built in function or something that could trigger this?
A legend is an axes object. If you are repeatedly adding objects to it, then you are adding handles as well. Maybe I do not really understand the problem. A minimum working example would help us help you.
I know that when you add entries to the legend, you get more handles. I said above that in normal conditions, for each entry in the legend you get 3 handles. Now, the problem is that something is triggering the 'ShowHiddenHandles' option because suddenly I get 8 handles PER legend entry. And maybe it is some built in function because in none of the lines I am running this occurs. What is even more weird is that, if it is some built in function, it is doing it randomly because the behavior gets triggered sometimes and sometimes not, with me running the same programs.
It would be easier to help you with a minimum working example.

Sign in to comment.

Answers (0)

Asked:

on 28 Jan 2014

Commented:

on 30 Jan 2014

Community Treasure Hunt

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

Start Hunting!