Height of a figure spontaneously changes.
2 views (last 30 days)
Show older comments
When I run the following code, I find that the OuterPosition properties of both figures report the same height and width, as expected.
close all ; for i=1:2; figure(i);plot(rand(1,5)); end;
H=flip( findobj('Type','figure') );
dpos =[0,0;
1,0];
dpos=dpos.*H(1).OuterPosition(3:4).*[1.01,1.0];
OP=H(1).OuterPosition;
for i=1:2
H(i).OuterPosition=OP;
H(i).OuterPosition(1:2)=OP(1:2)+dpos(i,:);
end
OuterPositions=vertcat(H.OuterPosition)
OuterPositions =
26.0000 542.0000 576.0000 459.0000
607.7600 542.0000 576.0000 459.0000
On the screen, however, the figure windows are of different sizes. Why does the height of the first figure grow spontaneously?
1 Comment
Accepted Answer
DGM
on 17 Jan 2022
It's seems like the figure properties aren't updated immediately. You're reading the properties before they're ready and then the math you perform on them ends up making one window larger. I don't know what causes this delay.
close all ;
for i=1:2; figure(i);plot(rand(1,5)); end;
H=flip( findobj('Type','figure') );
dpos = [0,0;
1,0];
H(1).OuterPosition
pause(1)
H(1).OuterPosition
dpos=dpos.*H(1).OuterPosition(3:4).*[1.01,1.0];
OP=H(1).OuterPosition;
for i=1:2
H(i).OuterPosition=OP;
H(i).OuterPosition(1:2)=OP(1:2)+dpos(i,:);
end
OuterPositions=vertcat(H.OuterPosition)
ans =
679 554 562 447
ans =
679 554 562 502
OuterPositions =
1.0e+03 *
0.6790 0.5540 0.5620 0.5020
1.2466 0.5540 0.5620 0.5020
2 Comments
DGM
on 18 Jan 2022
Edited: DGM
on 18 Jan 2022
I imagine there's something in the background that's depending on reading those transient properties that you're modifying. It's probably just a matter of timing that one gets influenced and the other doesn't.
This line
H(i).OuterPosition = OP;
sets offset as well as height,width. At this point, the height and width stored in OP are the transient (undersize) values. Sometime between setting this property for the first and second figures, something gets updated in the background, and those yet unrealized changes are immediately overwritten when setting the property on the second figure. So figure 1 undergoes the magical resizing, but figure 2 is forced to stay at the undersized temporary geometry.
I don't get it either, but I've grown accustomed to dealing with laggy window managers and stale properties in my ad-hoc bash automation scripts, so that's my bumbling interpretation based on little more than a similarity in the character of the frustration involved. I'm probably at least half wrong.
IA's suggestion of drawnow() is something that rarely crosses my mind, but is probably way smarter than a fixed delay. That's just my bash habits showing.
More Answers (1)
See Also
Categories
Find more on Graphics Performance 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!