Why an annotation is plotted in a different position when the script is run as a whole than when running parts of it separately?

조회 수: 4 (최근 30일)
When I run a more detailed script as a whole, with the same structure as:
%%%% BLOCK 1
figure();
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
for j=1:3
subplot(4,3,c(j))
for cv = 1:11
plot()
end
subplot(4,3,c(j)+3)
for cv = 1:11
plot()
end
subplot(4,3,c(j)+3)
for cv = 1:11
plot()
end
subplot(4,3,c(j)+6)
for cv = 1:11
plot()
end
subplot(4,3,c(j)+9)
for cv = 1:11
plot()
end
end
subplot(4,3,1); ylabel(
subplot(4,3,2); title(
subplot(4,3,3); title(
subplot(4,3,4); yyaxis left; ylabel(
legend([p(1),p(23),p(12)],{
subplot(4,3,7); ylabel(
subplot(4,3,10); ylabel(
subplot(4,3,11); xlabel(
subplot(4,3,12); xlabel(
%Additional Annotations and text
subplot(4,3,7); sp=get(gca,'Position');
text(
annotation ('arrow',
annotation('line',
subplot(4,3,8);
text(
annotation ('arrow',
annotation('line',
subplot(4,3,9);
text(
annotation ('arrow',
annotation('line',
subplot(4,3,10); sp=get(gca,'Position');
text(
annotation ('arrow',
subplot(4,3,11);
text(
annotation ('arrow',
subplot(4,3,12);
text(
annotation ('arrow', .. ); annotation('line', .. );
%%% BLOCK 2
ax = subplot(4,3,2);
h=findobj(ax,'Type','text','String','ON'); set(h(2), 'Color', [0 1 0])
sp = get(ax, 'position'); %[Ax_xpos, Ax_ypos, Ax_width, Ax_height]
xscale = get(ax, 'xlim' ); %[xmin, xmax]
yscale = get(ax, 'ylim'); %[ymin, ymax]
X_norm = (h(2).Position(1)-xscale(1))/(xscale(2)-xscale(1))*sp(3)+sp(1);
Y_norm = (h(2).Position(2)-yscale(1))/(yscale(2)-yscale(1))*sp(4)+sp(2);
annotation('rectangle',[X_norm sp(2)+sp(4) .1347 .0125],'FaceColor','black','FaceAlpha',1);
%%%
It produces different X_norm and Y_norm and places the rectangle annotation in a different position than if I run BLOCK 1 first and then BLOCK 2 (either by commenting out the block not needed or by copying each block in the command window). Running the script as a whole, places the rectangle in the wrong position. The code should plot the start of the rectangle at the green ON. See attachments to compare differences.
What do I need to modify so I can plot the rectangle in the correct position when running the code as a whole?
  댓글 수: 2
ErikaZ
ErikaZ 2020년 12월 11일
I found a work around using
pause(0.01)
%OR
drawnow
Right before
ax = subplot(4,3,2);
And I can run the whole script and display the rectangle annotation correctly.
However, I would still like to know why is this happening and why
sp = get(ax, 'position');
has different values if the script is run as a whole or by section.
Raunak Gupta
Raunak Gupta 2020년 12월 19일
편집: Raunak Gupta 2020년 12월 19일
Hi,
The code given in the question looks to have lot of missing parts. It will be helpful for reproducing the issue, if you can add the code which atleast plot the subplots and corresponding axes even if the data is not plotted. Also please mention which MATLAB Release you are using.

댓글을 달려면 로그인하십시오.

채택된 답변

ErikaZ
ErikaZ 2021년 4월 23일
drawnow
or
pause(0.01)
Solves the display issue

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by