Can I change my UIFigure size while maintaining location of elements in figure?

조회 수: 27 (최근 30일)
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
J. Alex Lee
J. Alex Lee 2022년 9월 15일
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?
Amanda Beatty
Amanda Beatty 2022년 9월 15일
@J. Alex Lee Yeah I suppose I could do that. It just makes the figure pretty tall with a lot of blank space. Changing the figure size is classier in my opinion, but if I can't get it to work I can always use that as a last resort.

댓글을 달려면 로그인하십시오.

채택된 답변

J. Alex Lee
J. Alex Lee 2022년 9월 16일
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
Amanda Beatty
Amanda Beatty 2022년 9월 19일
@J. Alex Lee Yes yes YES!!! uigridlayout() is exactly what I needed, but I had never heard of it until your answer. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by