Refresh annotate on figure in loop

Hello.
I have read several other solutions that use various methods to replace an annotation with each figure refresh, but none of them seem to work for me. The annotations just keep stacking to the side of the first.
I've tried the solutions found here (to no avail):
figure;
a1 = [];
a2 = [];
for k = 1:K
%compute indices for the current frame
n = (1:N)+(N*(k-1));
%signal 1
x = s1(n);
E_m1(k) = (x'*x)/N; %Normalized frame energy
subplot(211);
plot(n,s1(n),'b',n,e(n),'g:');
%annotation stuff
str1 = sprintf('NFE %d', E_m1);
dim1 = [0.662 0.622 .3 .3];
delete(a1);
a1 = annotation('textbox',dim1,'string',str1,'FitBoxToText','on');
%pause between frames, waiting for keypress
pause(1)
end
Thanks for your time.

답변 (1개)

Rik
Rik 2020년 10월 8일

0 개 추천

Create the annotation object once, and update the String property inside your loop.
I would suggest drawnow to trigger the update of the graphics and to process any queued callbacks (e.g. those triggered by keypresses).

댓글 수: 4

Tharon
Tharon 2020년 10월 8일
편집: Tharon 2020년 10월 8일
I don't think this is what I'm trying to do. I need two annotations (a1, a2) because I am concurrently annotating two subplots with different annotations. The second subplot is in the same loop. Both annotations receive the updated values to the right of the original annotation instead of in replacement of. The code above is just an example of what one of the subplots annotation attempt looks like.
Is exactly what I want to do, but it does not work for me. Could it be because my x-axis values change with each loop iteration?
I don't see the problem of having two annotations, you just have to keep track of two handles:
figure;
a1 = [];
a2 = [];
for k = 1:K
%compute indices for the current frame
n = (1:N)+(N*(k-1));
%signal 1
x = s1(n);
E_m1(k) = (x'*x)/N; %Normalized frame energy
%signal 2
x = s2(n);
E_m2(k) = (x'*x)/N; %Normalized frame energy
ax1=subplot(2,1,1);
plot(n,s1(n),'b',n,e(n),'g:','Parent',ax1);
% ^^^^^^^^^^^^
% use explicit handles here as well
%annotation stuff
str1 = sprintf('NFE %d', E_m1);
str2 = sprintf('NFE %d', E_m2);
dim1 = [0.662 0.622 .3 .3];
dim2 = ___;
if k==1
a1 = annotation('textbox',dim1,'string',str1,'FitBoxToText','on');
a2 = annotation('textbox',dim2,'string',str2,'FitBoxToText','on');
else
set(a1,'Position',dim1,'String',str1);
set(a2,'Position',dim2,'String',str2);
drawnow;%trigger graphics update
end
%pause between frames, waiting for keypress
pause(1)
end
Tharon
Tharon 2020년 10월 8일
Thank you, I will try this soon.
Tharon
Tharon 2020년 10월 9일
Thanks for all of your help so far Rik, but I am still having trouble. I implemented the changes you suggested. Now it seems that instead of stacking the new annotations to the right, it stacks them under the previous:

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

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

질문:

2020년 10월 7일

댓글:

2020년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by