Height of a figure spontaneously changes.

조회 수: 2 (최근 30일)
Matt J
Matt J 2022년 1월 16일
편집: DGM 2022년 1월 18일
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