Height of a figure spontaneously changes.

1 view (last 30 days)
Matt J
Matt J on 16 Jan 2022
Edited: DGM on 18 Jan 2022
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
Ankit
Ankit on 17 Jan 2022
This is weird I am also getting the similar results. But it works with Position Property
for i=1:2
set(H(i),'Position',[OP(1)+dpos(i,1) OP(2) OP(3) OP(4)])
end

Sign in to comment.

Accepted Answer

DGM
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
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Jan 2022
Does calling drawnow help?
  1 Comment
Matt J
Matt J on 17 Jan 2022
Yes, similar to DGM's answer drawnow works as well.
for i=1:2; figure(i);plot(rand(1,5)); end;
H=flip( findobj('Type','figure') );
dpos = [0,0;
1,0];
drawnow
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 =
1.0e+03 *
0.6720 0.5500 0.5760 0.5130
1.2538 0.5500 0.5760 0.5130

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!