给定子图的句柄,如何获取其所有关联的颜色条句柄?

发布于 2024-12-27 05:07:21 字数 351 浏览 2 评论 0原文

以下面的代码为例:

Hsp=subplot(1,2,1);

image(rand(5,5));

Hc=colorbar;

subplot(1,2,2);

image(rand(5,6));

colorbar;

我的问题是,仅给出Hsp,如何获取Hc

众所周知,颜色条的类型是axes。所以我尝试搜索子情节的所有子情节。

Hs=findall(Hsp,'type','axes');

但是,Hs 中没有与 Hc 匹配的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2025-01-03 05:07:21

使用以下脚本可以找到作为轴子级的所有颜色条的句柄。这里Ha1是带有图像的轴的句柄(例如子图),Hc1s是轴的对等颜色条的句柄。

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

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
时光倒影 2025-01-03 05:07:21

您的颜色条是图形的子项,而不是子图轴的子项(颜色条本身就是轴)。尝试

hc = get(hf, 'children')

获取图窗的所有子级的列表,其中 hf 是图窗句柄。我不确定您如何看待 hc 的哪个元素等于您的 Hc,即哪个是 first 颜色条。

编辑

如果稍后需要使用对象的句柄,最好在创建对象时将其分配给变量,并在整个过程中使用该变量。

但是,如果你不想这样做(尽管我强烈建议你这样做)我可以想到你可以做的两件事。它们并不是特别优雅,并且肯定比仅将对象句柄分配给变量要更多的工作。

如果您知道轴的创建顺序,那么您很幸运:在子列表中,创建的第一个子元素是列表中的 last 元素,最后创建的子元素是 >首先。例如,

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

由于您想要第一个颜色条(它是创建的第二个子项),因此您可以使用

hc(end-2)

或者,在创建将来要引用的颜色条时,设置它的 tag 属性。在上面的示例中,将该行替换

hc1 = colorbar;

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

您可以稍后使用以下命令获取该对象的句柄

findobj(hf, 'type', 'axes', 'tag', 'myID')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文