uigridlayout shows unexpected behavior; components are built differently when executing and debugging
5 次查看(过去 30 天)
显示 更早的评论
I am currently creating a GUI based on a uifigure. To structure my components I use a uitabgroup inside a uigridlayout. To arrange my components I iterate over all Children of the current tab and place them one above another depending on their size. As their width I assign them the width of the tab minus a constant SPC. Now I came to the strange situation where my code behaves differently when debugging or executing everything in bulk. When I debug the code, i.e. go through the program command by command, everything works flawlessly and as expected.
If I now execute the entire script, the width of the tabgroup in line 15 returns a wrong value (the default one and not the one specified in the consturctor in line 1; see below) and all components are wrongly located and sized.
Do you have an idea what might be the root for this issue (=what I'm doing wrong) or is this actually a bug in one of the uifigure/uigridlayout/uitabgroup classes.
I tried to include drawnow at different points in the code, which also made no difference. When including uiwait statements in lines 11 and 17, the components appear at the right place but still not with the correct width, if I leave one of them out, it doesnt work anymore at all.
Thanks in advance! Code and screenshots of the output are below!
window = uifigure('Position', [100 100 700 500], 'Name', 'Window');
grid = uigridlayout(window);
grid.ColumnWidth = {'2x', '1x'};
grid.RowHeight = {'10x', '1x'};
tabgroup = uitabgroup(grid);
tabgroup.Layout.Column = 1;
tabgroup.Layout.Row = [1 2];
tab = uitab(tabgroup, 'Title', 'Tab1');
% uiwait(window, 1);
label = uilabel(tab);
label.Text = 'Any component inside here behaves the same';
TG_W = tab.Parent.Position(3)
SPC = 20;
% uiwait(window, 1);
components = tab.Children;
for i = length(components) : -1 : 1
if i == length(components)
% place first component at top end of tab and make as wide as tab
components(i).Position(3) = TG_W-2*SPC;
components(i).Position(1:2) = [SPC tab.Position(4)-1.5*SPC-components(i).Position(4)];
else
components(i).Position(3) = TG_W-2*SPC;
components(i).Position(1:2) = components(i+1).Position(1:2) - [0 components(i).Position(4)];
end
end
the appearance of the window, when I execute the entire script with f5/run-button:
the appearance of the window, when regularly executed but with uiwait-statements in ll.11,17
the appearance of the window, when debugging the code line by line
The output when debugging (left) and when executing everything at once (right):
0 个评论
回答(1 个)
Shivam
2023-10-18
Hi Eric,
From the information provided, I understand that you are getting different results while executing the script using the "Run" button and debugging using the "Step" button.
You can add a pause of 2 seconds before accessing the "Position" property of "tab.Parent". It allows MATLAB to finish rendering the UI components before accessing the "Position" property.
You can refer to the following example:
% ..
% ...
pause(2);
TG_W = tab.Parent.Position(3);
Also, the exact amount of time you need to pause might vary depending on a number of factors, including the complexity of your UI and the performance of your computer. You might need to adjust the pause duration to find what works best for your specific situation.
Hope it helps.
Thanks,
Shivam
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!