Can I change my UIFigure size while maintaining location of elements in figure?
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amanda Beatty
el 15 de Sept. de 2022
Comentada: J. Alex Lee
el 22 de Sept. de 2022
I have a UIFigure that I want to be interactive. There is a dropdown menu, and depending on the number the user selects, new edit field boxes will appear to match that number. I achieve this by adding length to the figure, making the extra editfield boxes visible (they start off as hidden), then re-positioning everything on the bottom half to be closer to the bottom.
The problem is that when I make the figure taller, the length is added to the bottom (this is fine) but when I make the figure shorter, the length is removed from the top. So if the user selects 5, then goes back to select 1, a lot of my items are now out of frame, because I only adjust the position of the items on the bottom half of the figure.
I could just re-position everything within the figure every time I chane the figures size, but there are 51 elements total between all the hidden/visibile labels and buttons and boxes and such, and that is a lot of stuff to reposition.
Is there a workaround I'm not thinking of where I can repeatedly change the size of my figure without messing up the position of everything? Is there a better way to go about this? I'm on 2019b if that matters.
6 comentarios
J. Alex Lee
el 15 de Sept. de 2022
Must the figure itself change size when you change your selection? If not, can you make the maximum number of rows of edit boxes in fixed positions and toggle their visibilities depending on your selection?
Respuesta aceptada
J. Alex Lee
el 16 de Sept. de 2022
I think you may be able to achieve what you want using the uigridlayout, see if this example works on 2019b (I'm on 2022a).
The idea is to use uigridlayout to keep your static stuff positioned how you want, and you would still preallocate the max number of rows for your edit fields, but you can make the inactive grid cell heights zero and then resize the figure
% elements are typically 22 pixels tall
% default spacing/padding in uigridlayout is 10
h = 22;
s = 10;
% lay out a drop down, n edit fields, and a button at the bottom
n = 5;
figHeight = (2+n)*(h+s) + s;
itemsdata = 1:n;
% draw
fig = uifigure();
fig.Position(3) = 200;
fig.Position(4) = figHeight;
uig = uigridlayout(fig,[2+n,1],"Padding",s,"RowSpacing",s,...
"RowHeight","fit"+strings(2+n,1));
uib = uibutton(uig,"Layout",matlab.ui.layout.GridLayoutOptions('Row',2+n,'Column',1),"Text","A button");
for i = n:-1:1
uie(i) = uieditfield(uig,"Layout",matlab.ui.layout.GridLayoutOptions('Row',1+i,'Column',1),"Value","edit "+i);
end
uid = uidropdown(uig,"Layout",matlab.ui.layout.GridLayoutOptions('Row',1,'Column',1),...
"ItemsData",itemsdata,"Items",itemsdata+" edits","Value",n,...
"ValueChangedFcn",@(o,e)ddchangefcn(o,e,uig,fig,h,s) ...
);
function ddchangefcn(src,evnt,uig,fig,h,s)
n = numel(src.Items);
m = src.Value;
RH = cell(1,n);
RH(m+1:n) = {0};
RH(1:m) = {'fit'};
uig.RowHeight = ['fit',RH,'fit'];
fig.Position(4) = (2+m)*(h+s) + s;
end
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!