Refresh annotate on figure in loop

19 views (last 30 days)
Tharon
Tharon on 7 Oct 2020
Commented: Tharon on 9 Oct 2020
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.

Answers (1)

Rik
Rik on 8 Oct 2020
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 Comments
Tharon
Tharon on 8 Oct 2020
Thank you, I will try this soon.
Tharon
Tharon on 9 Oct 2020
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:

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!