MATLAB: “uicontrol” behaves differently and does not always display on the figure after upgrading to R2014b or later

MATLAB

I recently upgraded from 2013b to 2016b, and "uicontrol" does not work like it used to.
When I do this:
p=uipanel;
A = uibuttongroup('parent',p);
B = uibuttongroup('parent',p);
xx=uicontrol(A)
I don't see the control appear on the panel.  However if I then do the following, then the control does appear:
yy=uicontrol(B)
In R2013 both controls would appear.  I see that much has changed with the entire structure of controls.  Is there a reason why this no longer works?

Best Answer

This is due to the release of a new graphics system in MATLAB R2014b.
The new graphics system stacks components in the order in which they were created. So in the example, "B" is created after "A", causing "B" to be stacked on top of "A". Therefore, when you execute "xx = uicontrol(A)", the uicontrol is still being drawn; however, it is drawn below B, so it is not visible. Similarly, when you execute "yy = uicontrol(B)", you can see it displayed because B is currently the top layer. 
In order to properly display the uicontrol in both cases:
1. Specify the positions of "A" and "B" within the uipanel so that they no longer overlap:
% Create the uipanel and uibutton groups as you did before, but specify their positions
% Note that "uibuttongroup" by default fills the whole panel if no position information is provided
>> p = uipanel
>> A = uibuttongroup('Parent',p,'Position',[0 0 .2 1]);
>> B = uibuttongroup('Parent',p,'Position',[.5 0 .2 1]);
% You should see each uicontrol displayed on the specified uibuttongroup
>> xx = uicontrol(A);
>> yy = uicontrol(B);
2. Adjust the stack layer order using "uistack":
% Use "uistack" in order to shift "A" to the top level. Now "A" is above "B".
% You can use this command before or after you have added the "uicontrol".
>> uistack(A,'top');
Here is a relevant link about missing/obscured graphics components in the new graphics system: