How do I get the properties of annotation objects in the current figure?

9 views (last 30 days)
I created some annotation objects such as text boxes and arrows in a figure. I would like to obtain the strings entered in the text boxes.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
To obtain the properties and values of annotation objects in the current figure, search the figure using the FINDALL function to locate hggroup objects that are children of the annotation object parent axes. The following code provides an example:
close all
clear all
% Create some annotations
annotation('textbox',[0.5 0.5 0.1 0.1],'string','hello world');
annotation('textbox',[0.7 0.9 0.1 0.1],'string','hello world1');
annotation('textbox',[0.2 0.2 0.1 0.1],'string','hello world2');
annotation('line',[0.2 0.2],[0.8 0.3]);
fig = gcf;
% Return the axes that contains the annotation objects
ax = findall(fig, 'Type', 'axes', 'Tag', 'scribeOverlay');
% Find the Children with type hggroup
annotation_handles = findall(get(ax,'Children'),'Type','hggroup');
ss = size(annotation_handles);
j = max(ss);
% In a loop, determine if the annotations are of textbox type
for i = 1:j
hh = get(annotation_handles(i));
% Check if the object has a 'String' property
if(isfield(hh,'String'))
text_string{i} = get(annotation_handles(i),'string');
else i = i+1;
end
end
% Display the strings in the annotation textboxes
text_string
It is important to note that all annotation objects are of type 'hggroup' and hence based on the type you cannot determine whether it is a textbox or a line. Hence, you will need to find if the 'String' property exists in the handle structure of the annotation object.

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!