Cannot get XLabel by Child accessing of gca

6 次查看(过去 30 天)
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.

回答(1 个)

dpb
dpb 2014-6-26
编辑:dpb 2014-6-26
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 个评论
Junhong YE
Junhong YE 2014-6-27
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 2014-6-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by