MATLAB Answers


How do I find the handle to the colorbar for a specific axes

Asked by jvc
on 19 Aug 2015
Latest activity Commented on by Michael G. Baker on 27 Nov 2018
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.


Sign in to comment.

1 Answer

Answer by Morteza
on 19 Aug 2015

use the specific term for each color bar before making them like:
t = colorbar('peer',gca,'Tag','colorbar1');


But this also creates a colorbar if non existed previously. How can I test if there is a colorbar?
C = findall(gcf,'type','ColorBar');
if isempty(C)
display('No ColorBar.');
ColorBars are apparently children of the Figure object, rather than the Axes. I'm not sure how Matlab keeps the colorbars associated with the relevant axes, though. If I need to modify positions and settings later on, I set the object Tags to the same value and use the findall command.

Sign in to comment.