Stubborn annotation? Cannot be found by a search? Even though it exists...
2 次查看(过去 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
0 个评论
回答(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);
get(get(SampledColor,'Parent'))
hSampledColor = findobj('Tag','LineRB')
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')
Note that findall() requires a handle (or array of handles), e.g., a figure, as its first argument, whereas this is optional in findobj().
0 个评论
另请参阅
类别
在 Help Center 和 File 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!