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

조회 수: 1(최근 30일)
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 matlab.graphics.GraphicsPlaceholder?
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)
delete(hLigne);
end

답변(1개)

Voss
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);
get(get(SampledColor,'Parent'))
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().

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by