Height of a figure spontaneously changes.

2 次查看(过去 30 天)
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 个评论
Ankit
Ankit 2022-1-17
编辑:Ankit 2022-1-17
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

请先登录,再进行评论。

采纳的回答

DGM
DGM 2022-1-17
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 个评论
Matt J
Matt J 2022-1-18
编辑:Matt J 2022-1-18
Thanks, that fixes it. But it is still a bit weird that a fix is needed at all. At no point in the code do I do anything that should change the height/width of any of the figures...
DGM
DGM 2022-1-18
编辑:DGM 2022-1-18
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.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-1-17
Does calling drawnow help?
  1 个评论
Matt J
Matt J 2022-1-17
编辑:Matt J 2022-1-17
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by