Reputation: 3488
I have a gui that includes a plot. In this plot I add an annotation. When the plot data is changed using the gui, he old annotation remains and the new one is plotted over the old on.
So I need to remove he old annotation. I tried the following code, but that has no effect:
set(0,'showhiddenhandles','on')
% look for all axes in the figure of choice:
h_all_axes = findall(gcf,'type','axes');
% get the 'annotation layer' axes handle:
h_anno_axes = double(find(handle(h_all_axes),'-class','graph2d.annotationlayer'));
delete(h_anno_axes);
set(0,'showhiddenhandles','off');
annotationPos = [0.55 0.58 0.6 0.3];
htxtbox = annotation('textbox',annotationPos, ...
'String' ,strtextbox, ...
'FontSize' ,FontSize+1, ...
'FitBoxToText', 'on', ...
'EdgeColor', 'none', ...
'FontName' , 'Courier New');
Upvotes: 3
Views: 12277
Reputation: 11
I also found that findobj
doesn't work here. The Parent of an annotation seems to be an AnnotationPane, of which there is just one per Figure as far as I can see, which is like a transparent sheet overlaid on the Figure, that one writes upon.
- If you do clf
, it gets cleared.
- If you do cla
, it doesn't.
I found this out because in my application I have 3D axes that the user can rotate, and I wanted the user's preferred viewpoint, and size of figure-window on the screen, to stay from one run to the next, which needed cla
not clf
. But I found that the annotations (which changed from run to run) just accumulated on the Figure. However this worked for me:
% This eccentric method removes any existing annotations, since they
% lie around on the Figure; cla didn't remove them. htemp.Parent is
% 'the' AnnotationPane.
htemp = annotation('textbox','Position',[0 0 0 0]);
delete(htemp.Parent.Children);
Explanation: The AnnotationPane doesn't seem to be part of the usual set of graphics objects, so I "got at" it by creating a dummy annotation and accessing its Parent. The existing annotations are stored in the array htemp.Parent.Children
(you can use this to list them), so delete
works on this.
MatheWorks, you can do better than this!
Upvotes: 1
Reputation: 74940
The easiest solution is to add a specific tag to the annotation.
%# create the annotation
annotationPos = [0.55 0.58 0.6 0.3];
htxtbox = annotation('textbox',annotationPos, ...
'String' ,strtextbox, ...
'FontSize' ,FontSize+1, ...
'FitBoxToText', 'on', ...
'EdgeColor', 'none', ...
'FontName' , 'Courier New', ...
'Tag' , 'somethingUnique');
%# delete the annotation
delete(findall(gcf,'Tag','somethingUnique'))
Upvotes: 9