如何获取图形句柄中的所有轴句柄?
给定图形句柄hf
,我发现get(hf, 'children')
可能返回所有轴的句柄。然而,Matlab 帮助表明它可能返回的不仅仅是轴句柄:
人物的孩子。包含所有轴的手柄、图形中显示的用户界面对象的向量。您可以更改手柄的顺序,从而更改显示屏上对象的堆叠。
有没有办法只获取图形句柄中的轴句柄?或者我如何知道 get(hf, 'children')
返回的句柄是否是斧头句柄?
谢谢!
How do I obtain all the axes handles in a figure handle?
Given the figure handle hf
, I found that get(hf, 'children')
may return the handles of all axes. However, the Matlab Help suggests that it may return more than just the axes handles:
Children of the figure. A vector containing the handles of all axes, user-interface objects displayed within the figure. You can change the order of the handles and thereby change the stacking of the objects on the display.
Is there any way to obtain only the axes handle in the figure handle? Or how do I know if the handle returned by get(hf, 'children')
is an axe handle?
Thanks!
发布评论
评论(4)
使用 FINDALL:
如果您想在 Matlab 中的任何位置获取所有轴句柄,您可以执行以下操作:
编辑
要回答问题的第二部分:您可以通过获取句柄
type
属性来测试句柄列表是否是轴:axes 类型的句柄,>isAxes 将为 true。
EDIT2
要仅选择不是图例的轴句柄,您需要清理轴列表(
ax
句柄,方法是删除标签不是'legend' 的所有句柄
或“颜色条”
:Use FINDALL:
If you want to get all axes handles anywhere in Matlab, you could do the following:
EDIT
To answer the second part of your question: You can test for whether a list of handles are axes by getting the handles
type
property:isAxes
will be true for every handle that is of typeaxes
.EDIT2
To select only axes handles that are not legends, you need to cleanup the list of axes (
ax
handles by removing all handles whose tag is not'legend'
or'Colorbar'
:乔纳斯的解决方案对我不起作用,因为有一些句柄引用了传说。令人惊讶的是,图例似乎是作为轴实现的,至少在 Matlab 2010a 中是这样。
如果您只需要坐标轴,而不需要任何图例或其他内容,这是一个解决方案。
该脚本的工作原理是对显示为轴类型子类型的句柄进行排序,例如 scribe.legend。
对那些试图改进上述代码片段的人的警告:使用类似的东西
可能无法按预期工作:
Jonas' solution didn't work for me, because there were some handles referring to legends. Surprisingly, legends seem to be implemented as axes, at least in Matlab 2010a.
Here is a solution if you only want the axes, not any legends or other stuff.
The script works by sorting out handles which reveal to be of a sub-type of type axes, for example
scribe.legend
.A warning for those trying to improve the above code snippet: using something like
might not work as intended:
“Jonas”和“tm1”的答案对某些人有用。然而,正如 tm1 指出的那样,“axes”类型中有几个项目。
要准确引用图例或轴本身(可能存在其他项目),您需要使用它们的特征属性来区分它们。
在我的示例中,我打开“属性编辑器”并查找轴和图例的不同属性(因为它们都属于“类型,轴”)。我试图复制我的轴及其图例:
除了“标签”属性,我还可以使用“属性检查器”中的其他属性。问题是,它们一定是不同的。它们的大部分属性都是相同的。
"Jonas" and "tm1" have answers that work for some. However, as tm1 pointed the issue, there are several items inside type 'axes'.
To exactly refer to the legend or axes itself (there may exist other items), you need to differentiate them, using their characteristic properties.
In my example, I opened "property editor" and looked for differing properties for axes and legend (since they both belong to "type, axes"). I was trying to copy my axes and its legend:
Instead of 'Tag' property, I also could use an other property from the "Property Inspector". The thing is, they must differ. Most of their properties are the same.
@tm1 的解决方案非常好。我的稍微简单一些(如果您熟悉函数式编程):
ax 将仅包含绘图轴。这是有效的,因为图例或颜色条对象的
class
与axes
不同。编辑 @Jonas 指出了过滤
findall
结果的潜在改进,因为至少图例和颜色条似乎具有非空Tag
属性:将上面代码片段中的最后一行替换为这两种技术都很笨拙,并且将来可能会崩溃(与 ggplot2 或 Bokeh 进行比较可能会很有趣)。
The solution by @tm1 is excellent. Mine is a little less complicated (if you're ok with functional programming):
ax
will contain only plot axes. This works because theclass
of a legend or colorbar object is different fromaxes
.Edit @Jonas points out a potential improvement to filter the results of
findall
, because at least legends and colorbars seem to have non-emptyTag
properties: replace the last line in the above code snippet withBoth these techniques are kludgy and may break in the future (a comparison against ggplot2 or Bokeh might be interesting).