- Use drawnow: After creating UI components and setting their properties, you can force MATLAB to update the graphics and process any pending callbacks using drawnow. This might help reduce the perceived lag by forcing the layout to update immediately.
- Pre-calculate Sizes: If possible, pre-calculate the sizes of your components based on the text they will contain. This can be done using functions like textwidth and textheight (if available) or by estimating based on font size and length of the text. This approach requires a bit of manual calculation but can help set initial sizes more accurately.
- Use Callbacks for Visibility: While you cannot directly observe the Position property, you can use callbacks like SizeChangedFcn on the parent figure or grid layout. This callback is triggered when the size of the figure changes, which might indirectly indicate that the layout has been adjusted. You can use this to control the visibility of the figure.
Why are graphic positions not observable properties?
3 views (last 30 days)
Show older comments
Back story: I am trying to port dialog boxes based on uicontrols to their uifigure equivalents: uilabel, uibutton, etc. The old system allowed you to size items based on the text contained within using the Extent property. For example, one could add a button with the String 'Apply', adjust the position to accomodate that text, and then size the figure as needed. Newer components lack an Extent property, and the closest I can come to that feature is stacking things in a uigridlayout using 'fit' for the row height and column width. However, there is noticeable lag, around a second or so, between when the component is created and grid sizing. The attached function illustrates this with a separate plot showing how long it takes for a uilable's width/height to change from its default value.
I thought one would be able to add a listener to the grid or component contained within to see when sizing is complete. However, the Position property is not an observable property for the grid layout or a uilable inside. Is there some reason for that? The lag is bad enough, but at least I could keep the figure hidden until all resizing is complete. Right now I have to guess as to how long this might take, put in a manual delay, and then hope all layout grids are done changing before sizing the figure based on what is inside.
FWIW I am using the latest update of R2024b with the desktop beta on a Mac.
0 Comments
Answers (1)
prabhat kumar sharma
on 7 Jan 2025
Hi Dan,
I understand you are facing the issue with the lag using uigridlayout.
Here are a few suggestions to help mitigate the lag and manage the layout more effectively:
fig = uifigure('Visible', 'off');
grid = uigridlayout(fig, [1, 1]);
grid.SizeChangedFcn = @(src, event) set(fig, 'Visible', 'on');
I hope it helps!
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!