MATLAB: How to know which axes is associated to a colorbar in MATLAB R2013b

MATLAB

I have a figure with some subplots, and these subplots have got associated a colorbar.
I would like to find programmatically which colorbar is associated with a given axes, but the 'Parent' property of the colorbar is the figure itself not the axes.

Best Answer

The colorbar are graphics objects of type 'axes' and they are of the same type as the axes where you plot the graphs.
As a consequence they are not automatically associated with a graph.
 
As a workaround you can use the 'Tag' property to identify each colorbar.
You could set such property when creating the colorbar and then use it as an identifier:
 
h_fig = figure('Name', 'Test');
h1 = subplot(121);
[C,h] = contourf(peaks(20),10);
h_bar1 = colorbar('Tag','axes1');
h2 = subplot(122);
[C,h] = contourf(peaks(20),10);
h_bar2 = colorbar('Tag','axes2');