Stubborn annotation? Cannot be found by a search? Even though it exists...

조회 수: 2 (최근 30일)
Roger Breton
Roger Breton 2022년 2월 17일
답변: Voss 2022년 2월 17일
I'm using a 'line' annotation to show a selected pixel color in my script. I programmatically create the annotation using this code :
SampledColor = annotation(FigureH, 'line', 'Units', 'Pixels','Tag','LineRB', 'Position', [70 50 Width 0], 'Color','k','LineWidth',10);
hSampledColor = findobj('Tag','LineRB');
Trouble is, the 'tag' property does not seem to be recognized or registered? The first line above executes fine but the following line, findobj('Tag', 'LineRB') only returns an empty 0x0
To work around the problem, I found I could first 'zap' the area where the line is drawn in the figure using this statement :
WhiteLine = annotation(FigureH, 'line', 'Units', 'Pixels','Tag','LigneRB', 'Position', [0 50 FigureWidth 0], 'Color','w','LineWidth',10);
It works but I'd love to be able to tell whether the annotation exists before creating it, so that I could delete it using these statements:
hLigne = findobj('Tag','LineRB');
if ~isempty(hLigne)

답변 (1개)

Voss 2022년 2월 17일
The reason findobj() doesn't work is because SampledColor's parent AnnotationPane has its 'HandleVisibility' set to 'off':
FigureH = gcf();
Width = 100;
SampledColor = annotation(FigureH, 'line', 'Units', 'Pixels','Tag','LineRB', 'Position', [70 50 Width 0], 'Color','k','LineWidth',10);
Children: [1×1 Line] HandleVisibility: 'off' Parent: [1×1 Figure] Tag: 'scribeOverlay' Type: 'annotationpane' UserData: [] Visible: on
hSampledColor = findobj('Tag','LineRB')
hSampledColor =
0×0 empty GraphicsPlaceholder array.
To find objects with 'HandleVisibility' set to 'off' (or their descendants in this case), you can use findall() rather than findobj():
hSampledColor = findall(FigureH,'Tag','LineRB')
hSampledColor =
Line (LineRB) with properties: Color: [0 0 0] LineStyle: '-' LineWidth: 10 Position: [70 50 100 0] Units: 'pixels' X: [70 170] Y: [50 50] Show all properties
Note that findall() requires a handle (or array of handles), e.g., a figure, as its first argument, whereas this is optional in findobj().


Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by