MATLAB: Hide and change location of pushbutton in GUI

guiguidehidelocationMATLABpushbuttonvisible

I'm making a GUI using GUIDE, and I was wondering how I can change the location of push buttons after other have been made not visible. Haven't found anything online yet.

Best Answer

You can set the Position property of a uicontrol object to move it to any location you like inside the callback that sets the Visible property for the objects you're hiding.
Edit:
Here is a small example of how you could do something like this:
function example_GUI
f=figure;
for n=1:3
pos=[(n-1)/3 0 1/4 1];
buttons(n)=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',pos,...
'String',sprintf('Click me! (%d)',n),...
'Tag',num2str(n),...%use tag to discern the buttons in the callback
'Callback',@buttoncallback); %#ok<SAGROW>
end
%store to guidata struct
h=struct;
h.f=f;
h.buttons=buttons;
h.HasBeenClicked=false(size(h.buttons));
guidata(h.f,h)
end
function buttoncallback(hObject,eventdata) %#ok<INUSD>
h=guidata(hObject);
buttonindex=str2double(get(hObject,'Tag'));
if h.HasBeenClicked(buttonindex)
return%ignore extra click
else
h.HasBeenClicked(buttonindex)=true;
guidata(h.f,h)
end
v=1:3;v(buttonindex)=[];
%hide one button
set(h.buttons(v(1)),'Visible','off')
%'move' the other button
pos=get(h.buttons(v(2)),'Position');
pos=[pos(1:2)+pos(3:4)/3 pos(3:4)*2/3];%semi-random new position
set(h.buttons(v(2)),'Position',pos)
end