I would like to determine if an axes already has a colorbar. I have code which plots many objects on the same axes, but I only want to create and define the colorbar once to make things more efficient. I've tried looking for a ColorBar object as a child of the figure handle, but I have multiple subplots in the same figure, and each may or may not need their own colorbar. Is there a way to determine if a particular axes has a colorbar associated with it?
How to determine if an axes already as a colorbar?
30 views Asked by David K At
2
There are 2 answers
1
On
you can use findobj for that, for example
figure
subplot(2,2,1); plot(rand(4));
subplot(2,2,2); imagesc(rand(10)); colorbar
subplot(2,2,3); plot(rand(3)); legend('a','b','c')
subplot(2,2,4); imagesc(rand(32)); colorbar
h = get(gcf,'children');
axes_ind = findobj(h,'type','Axes');
legend_ind = findobj(h,'type','Legend');
colorbar_ind = findobj(h,'type','Colorbar');
you can check h and also see that colorbar_ind now has access to your colorbars...
note that once you create a colorbar, you create an independent object at some position in the figure, it is not a child of an axes object.
Credit to Voss at MATLAB Answers
Returns
trueReturns
false