How do I find the handle to the colorbar for a specific axes
75 views (last 30 days)
I would like to know how to programatically find the handle to the colorbar for a specific axis in a figure which contains several subplots which each of which might or might not have an associated colorbar. Since colorbars are children of the figure it is not obvious to me how I get the handle of the correct colorbar. I can easily get a list of all of the colorbar handles, but I do not see any property value which indicates which bar goes with which axis.
Morteza on 19 Aug 2015
use the specific term for each color bar before making them like:
t = colorbar('peer',gca,'Tag','colorbar1');
Matt J on 17 Mar 2022
Edited: Matt J on 18 Mar 2022
Here is an excruciatingly roundabout method.
%Return colorbar handles for axes with colorbars, and optionally a logical
%indicator array telling which axes have colorbars.
% hAxes: an array of axes handles
% Hcb: an array of colorbar handles corresponding to the elements of
% hAxes. If hAxes(i) has no colorbar, then Hcb(i) will be
% an empty GraphicsPlaceHolder.
% tf: a logical array indicating which elements of hAxes have
if after<before %colorbar was present