给定子图的句柄,如何获取其所有关联的颜色条句柄?
以下面的代码为例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下脚本可以找到作为轴子级的所有颜色条的句柄。这里
Ha1
是带有图像的轴的句柄(例如子图),Hc1s
是轴的对等颜色条的句柄。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.您的颜色条是图形的子项,而不是子图轴的子项(颜色条本身就是轴)。尝试
获取图窗的所有子级的列表,其中
hf
是图窗句柄。我不确定您如何看待hc
的哪个元素等于您的Hc
,即哪个是first
颜色条。编辑:
如果稍后需要使用对象的句柄,最好在创建对象时将其分配给变量,并在整个过程中使用该变量。
但是,如果你不想这样做(尽管我强烈建议你这样做)我可以想到你可以做的两件事。它们并不是特别优雅,并且肯定比仅将对象句柄分配给变量要更多的工作。
如果您知道轴的创建顺序,那么您很幸运:在子列表中,创建的第一个子元素是列表中的 last 元素,最后创建的子元素是 >首先。例如,
由于您想要第一个颜色条(它是创建的第二个子项),因此您可以使用
或者,在创建将来要引用的颜色条时,设置它的
tag
属性。在上面的示例中,将该行替换为
您可以稍后使用以下命令获取该对象的句柄
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
hf
is the figure handle. I'm not sure how you would which element ofhc
is equal to yourHc
, i.e. which is thefirst
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,
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
tag
property. In the above example, replace the linewith
You can then get the handle to this object later with