MATLAB: Is the order of child handles reversed when an object is queried for the ‘Children’ property

childrenhandleinvertedkidsMATLAB

When multiple children are added to a graphics object such as a figure or an axes, the order of the child handles is reversed in the 'Children' property as compared to the order in which those children were added.
For example, if I create an axes and make two line plots on it, I expect the handle of the first line plot to appear before the handle of the second line plot when I query for the 'Children' property using the GET function. Instead this order is reversed.
>> figure
>> h = axes;
>> h1 = plot(1:10, 1:10, 'b')
h1 =
158.0044
>> hold on
>> h2 = plot(1:10, 2:2:20, 'r')
h2 =
159.0039
>> get(gca, 'children')
ans =
159.0039
158.0044
As can be seen above, the order of child handles is the opposite of the order in which they were added to the axes.

Best Answer

The behavior that is observed here is not a bug but instead a design decision which causes the most recently added child object's handle to appear at the top of the list of handles returned by the 'Children' property. This behavior can be observed in a variety of cases presently in MATLAB as follows:
1. Adding multiple line objects to an axes
2. Adding multiple uicontrols to a figure (uicontrols are children of the figure object)
3. Adding multiple axes to a figure (axes are children of the figure object)