Cannot get XLabel by Child accessing of gca

2 views (last 30 days)
Junhong YE
Junhong YE on 26 Jun 2014
Commented: dpb on 27 Jun 2014
I wonder why can't I access/see the xlabel object by get(gca,'children') .
However, I do can access its handle by get(gca,'XLabel').
When I try to find the parent of xlabel by get(get(gca,'XLabel'),'parent'), the result is Axes , which is gca .
I wonder what is the reason.

Answers (1)

dpb
dpb on 26 Jun 2014
Edited: dpb on 26 Jun 2014
The children of the axes are the line(s) while xlabel is a property which holds the handle of the text label object. Until modified, it (the xlabel object) is empty (but does exist). If you'll look at
>> plot(rand(10,1)) % a plot; no visible x label yet
>> hL=get(gca,'xlabel'); % get the xlabel property
>> xlabel('x label')==hL % see if matches handle when write an x-label...
ans =
1
>>
Voila! the handle returned is the same as the property retrieved before there was anything in the object; iow, the axes created the xlabel but default is that its default value is the empty string so nothing shows up even though the object already is in existence.
Children, otoh, don't exist until placed on the axes...
Continuing on from the above--
>> close 1
>> gca
ans =
173.0067
>> get(gca,'children')
ans =
Empty matrix: 0-by-1
>> get(gca,'xlabel')
ans =
174.0067
>>
So, while the xlabel's parent holding object is the axes object, it is not one of the children properties which are the lines. This also demonstrates conclusively that the xlabel (and by extension y- and z- as well) exists with the axes object independent of xlabel the function.
It's a choice of organization when the handle-graphics objects were first constructed to segregate the fixed characteristic things associated with the axes such as the grid marks, x/y labels, etc., from the variable ones, namely the data line objects that could be from none to very many and give the fixed ones specific names for mnemonic purposes.
Also keeping them out the the children array means don't have to do extensive searching on the returned vector of children handles to find out "who's who in the zoo" when making changes to lines.
  2 Comments
Junhong YE
Junhong YE on 27 Jun 2014
Thanks for your detailed answer!!
So we can only access it by hL=get(gca,'xlabel');?
By the way, is there any other property that acts like xlabel?
dpb
dpb on 27 Jun 2014
a) Directly, yes. As demonstrated, [x|y|z]label will return the handle as well so there's an indirect manner.
b) On the axes object, best of my recollection is they're the only ones that are handles themselves. Some of the other higher-level compound objects will, I'm certain, but I've not done any exhaustive search for which. Any that has another object as its content will like a legend is just another (especially modified) axes.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!