Take the follow code for example:
Hsp=subplot(1,2,1);
image(rand(5,5));
Hc=colorbar;
subplot(1,2,2);
image(rand(5,6));
colorbar;
My question is how to obtain Hc, given only Hsp.
As is known, the type of a colorbar is axes. So I tried to search all the children of the subplot.
Hs=findall(Hsp,'type','axes');
But, there is no value in Hs which matches Hc.
Your colorbars are children of the figure, not of your subplot axes (colorbars are themselves axes). Try
to get a list of all children of the figure, where
hfis the figure handle. I’m not sure how you would which element ofhcis equal to yourHc, i.e. which is thefirstcolorbar.Edit:
If you need to use an object’s handle later on, it is best to assign it to a variable when it is created and to use that variable throughout.
However, if you don’t want to do this (although I strongly recommend that you do) I can think of two things you can do. They are not particularly elegant and are definitely more work that just assigning your object handle to a variable.
If you know the order in which the axes were created then you are in luck: in the list if children, the first child created is the last element in the list and the last child created is the first. For example,
Since you want the first colorbar, which was the second child created, you can then use
Alternatively, when creating the colorbar which you want to refer to in the future, set it’s
tagproperty. In the above example, replace the linewith
You can then get the handle to this object later with