MATLAB: Does COPYOBJ reverse the order of objects in the output vector when copying mutliple objects in MATLAB 7.0.1 (R14SP1)

copyobjflipudhandleMATLABorderoutputreversevector

The following code demonstrates the problem:
hFigure = figure;
hButton = zeros(2,1);
for k = 1:2
hButton(k) = uicontrol('parent',hFigure,'position',[10 100-40*k 60 40],'string',int2str(k));
end
fprintf('Strings of text controls "hButton":\n');
get(hButton, 'String')
The output of the above code is as follows:
Strings of text controls "hButton":
ans =
'1'
'2'
Now, using COPYOBJ as follows:
hFigure2 = figure;
hButton_Copied = copyobj(hButton,hFigure2);
set([hButton(1), hButton_Copied(1)],'selected','on');
fprintf('Strings of text controls "hButton":\n');
get(hButton_Copied, 'String')
gives a reversed output:
Strings of text controls "hButton":
ans =
'2'
'1'

Best Answer

This bug has been fixed in Release 14 Service Pack 2 (R14SP2). For previous releases, please read below for any possible workarounds:
This has been verified as a bug in MATLAB 7.0.1 (R14SP1) in the way that the COPYOBJ command flips the order of the objects in the output vector.
Currently, to workaround this issue, you can use the FLIPUD command to reverse the order of the objects in the output vector of handles.
Thus, instead of:
new_handle = copyobj(handles_vector,parent)
you can use:
new_handle = flipud(copyobj(handles_vector,parent))