Hide and change location of pushbutton in GUI?

4 ビュー (過去 30 日間)
Peter Smith
Peter Smith 2019 年 9 月 28 日
編集済み: Rik 2019 年 10 月 7 日
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.

採用された回答

Rik
Rik 2019 年 9 月 29 日
編集済み: Rik 2019 年 10 月 7 日
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
  1 件のコメント
Peter Smith
Peter Smith 2019 年 10 月 7 日
An example would be a big help. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by