2

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.

Chris
  • 44,602
  • 16
  • 137
  • 156
Xun
  • 79
  • 1
  • 7

2 Answers2

2

Using the following script can find the handle of all colorbars which are children of an axes. Here Ha1 is the handle of the axes with image (e.g. a subplot), Hc1s are the handles of the peer colorbars of the axes.

function Hc1s = find_peer_colorbars_of_an_axes(Ha1)
    Hf = get(Ha1,'parent');
    Haxs = findobj(Hf,'type','axes');
    IsC=false(1,length(Haxs));
    Hc1s=[];

    for i=1:length(Haxs)
        if isa(handle(Haxs(i)),'scribe.colorbar');
            H=handle(Haxs(i));
            if isequal(double(H.axes),Ha1)
                Hc1s=[Hc1s,Haxs(i)];
            end
        end
    end
Chris
  • 44,602
  • 16
  • 137
  • 156
Xun
  • 79
  • 1
  • 7
1

Your colorbars are children of the figure, not of your subplot axes (colorbars are themselves axes). Try

hc = get(hf, 'children')

to get a list of all children of the figure, where hf is the figure handle. I'm not sure how you would which element of hc is equal to your Hc, i.e. which is the first colorbar.

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,

hf = figure;

ha1 = subplot(1,2,1);
image(rand(5,5));
hc1 = colorbar;

ha2 = subplot(1,2,2);
image(rand(5,5));
hc2 = colorbar;

hcs = get(hf, 'children')

hcs =

  206.0016
  204.0011
  176.0016
  174.0011

[hc2, ha2, hc1, ha1]'

ans =

  206.0016
  204.0011
  176.0016
  174.0011

Since you want the first colorbar, which was the second child created, you can then use

hc(end-2)

Alternatively, when creating the colorbar which you want to refer to in the future, set it's tag property. In the above example, replace the line

hc1 = colorbar;

with

hc1 = colorbar('tag', 'myID');

You can then get the handle to this object later with

findobj(hf, 'type', 'axes', 'tag', 'myID')
Chris
  • 44,602
  • 16
  • 137
  • 156
  • Thank you. Yes, it is the children of the figure. But if there are more than one colorbar in the figure, how to determine which is the one for the axes? – Xun Jan 13 '12 at 12:52
  • @Xun I have edited my answer to offer some solutions. However, I reiterate that it is best to store the handle to an object when you create it if you are going to use it later on, rather than try to find the handle at a later time. – Chris Jan 13 '12 at 13:59
  • Thank you for your answer. It's helpful. But I found another solution, which can found the colorbar of an anonymous axes handle. – Xun Jan 13 '12 at 14:50
  • Inspired by the function of find_colorbar.m in Matlab's original toolbox, I wrote some script to find the colorbar handles of a peer axes. – Xun Jan 13 '12 at 14:56
  • @Xun No need to thank me, just make sure you up vote answers which are helpful. Also, if you found a solution yourself you should post it as an answer - it may be helpful to others. – Chris Jan 13 '12 at 14:57
  • I could not answer my own question at this time, because I just registered. I will do it one day later or so. – Xun Jan 13 '12 at 15:01