Why are graphic positions not observable properties?

3 views (last 30 days)
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.

Answers (1)

prabhat kumar sharma
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:
  1. 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.
  2. 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.
  3. 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.
fig = uifigure('Visible', 'off');
grid = uigridlayout(fig, [1, 1]);
grid.SizeChangedFcn = @(src, event) set(fig, 'Visible', 'on');
I hope it helps!
  1 Comment
Dan
Dan on 7 Jan 2025
I have found no evidence that drawnow affects uigridlayout sizing. For example:
>> g=uigridlayout('ColumnWidth',{'fit'},'RowHeight',{'fit'});h=uilabel(g,'Text',repmat('M',[1 20]));disp(h.Position);drawnow();disp(h.Position);pause(1);disp(h.Position);
reveals that the label size has not changed after drawnow but at some later time.
I would love to precalculate text sizes, but there is no simple way of doing so. In fact, things have gotten a little weird in that legacy features having an Extent property (uicontrol, axes text, etc.) are rendered differently than newer graphics (uilabel, uibutton, etc.), even at the same font size. In principle one can figure out vertical spacing needed based on font size (in points) given the screen resoultion; width is a bit tricker as it character aspect ratio varies between fonts. Unfortunately, physical sizing has been broken in MATLAB for almost nine years now, and although recent changes (e.g., Mac DPI set to 96 instead of 72) address readability issues, it is still an utter mess.
The code you suggest does not work because layout grids do not have a SizeChangedFcn property; neither do components like uilabel. That feature is limited to uifigures.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!