Matlab:如何获取图形句柄中的所有轴句柄?

发布于 2024-09-27 16:43:27 字数 324 浏览 7 评论 0 原文

如何获取图形句柄中的所有轴句柄?

给定图形句柄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!

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

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

发布评论

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

评论(4

柒七 2024-10-04 16:43:27

使用 FINDALL

allAxesInFigure = findall(figureHandle,'type','axes');

如果您想在 Matlab 中的任何位置获取所有轴句柄,您可以执行以下操作:

allAxes = findall(0,'type','axes');

编辑

要回答问题的第二部分:您可以通过获取句柄type属性来测试句柄列表是否是轴:

isAxes = strcmp('axes',get(listOfHandles,'type'));

axes 类型的句柄,>isAxes 将为 true。

EDIT2

要仅选择不是图例的轴句柄,您需要清理轴列表(ax 句柄,方法是删除标签不是 'legend' 的所有句柄“颜色条”

axNoLegendsOrColorbars= ax(~ismember(get(ax,'Tag'),{'legend','Colobar'}))

Use FINDALL:

allAxesInFigure = findall(figureHandle,'type','axes');

If you want to get all axes handles anywhere in Matlab, you could do the following:

allAxes = findall(0,'type','axes');

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 = strcmp('axes',get(listOfHandles,'type'));

isAxes will be true for every handle that is of type axes.

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':

axNoLegendsOrColorbars= ax(~ismember(get(ax,'Tag'),{'legend','Colobar'}))
以可爱出名 2024-10-04 16:43:27

乔纳斯的解决方案对我不起作用,因为有一些句柄引用了传说。令人惊讶的是,图例似乎是作为轴实现的,至少在 Matlab 2010a 中是这样。
如果您只需要坐标轴,而不需要任何图例或其他内容,这是一个解决方案。

axesHandles = get(fig, 'Children');
classHandles = handle(axesHandles);
count = length(axesHandles);
isNotInstanceOfSubtype = false(1, count);
for i = 1:count
    isNotInstanceOfSubtype(i) = strcmp(class(classHandles(i)), 'axes') == 1;
end
axesHandles = axesHandles(isNotInstanceOfSubtype);

该脚本的工作原理是对显示为轴类型子类型的句柄进行排序,例如 scribe.legend。

对那些试图改进上述代码片段的人的警告:使用类似的东西

classHandles = cellfun(@(x) handle(x), axesHandles)

可能无法按预期工作:

??? Error using ==> cellfun
scribe.legend type is not currently implemented.

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.

axesHandles = get(fig, 'Children');
classHandles = handle(axesHandles);
count = length(axesHandles);
isNotInstanceOfSubtype = false(1, count);
for i = 1:count
    isNotInstanceOfSubtype(i) = strcmp(class(classHandles(i)), 'axes') == 1;
end
axesHandles = axesHandles(isNotInstanceOfSubtype);

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

classHandles = cellfun(@(x) handle(x), axesHandles)

might not work as intended:

??? Error using ==> cellfun
scribe.legend type is not currently implemented.
小草泠泠 2024-10-04 16:43:27

“Jonas”和“tm1”的答案对某些人有用。然而,正如 tm1 指出的那样,“axes”类型中有几个项目。

要准确引用图例或轴本身(可能存在其他项目),您需要使用它们的特征属性来区分它们。

在我的示例中,我打开“属性编辑器”并查找轴和图例的不同属性(因为它们都属于“类型,轴”)。我试图复制我的轴及其图例:

copied_axes = findobj(temp_fig,'type','axes','Tag','');
copied_legend = findobj(temp_fig,'type','axes','Tag','legend');

除了“标签”属性,我还可以使用“属性检查器”中的其他属性。问题是,它们一定是不同的。它们的大部分属性都是相同的。

"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:

copied_axes = findobj(temp_fig,'type','axes','Tag','');
copied_legend = findobj(temp_fig,'type','axes','Tag','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.

三人与歌 2024-10-04 16:43:27

@tm1 的解决方案非常好。我的稍微简单一些(如果您熟悉函数式编程):

% initialize `fig` somehow, i.e., "fig=gcf()" for the current figure or
% "fig=get(0,'children')" for all open figures; can be vector or scalar.

ax = findall(fig, 'type', 'axes');
ax = ax(arrayfun(@(i) strcmp(class(handle(i)), 'axes'), ax));

ax 将仅包含绘图轴。这是有效的,因为图例或颜色条对象的 classaxes 不同。

编辑 @Jonas 指出了过滤 findall 结果的潜在改进,因为至少图例和颜色条似乎具有非空 Tag 属性:将上面代码片段中的最后一行替换为

ax = ax(strcmp('', get(ax, 'Tag')))

这两种技术都很笨拙,并且将来可能会崩溃(与 ggplot2 或 Bokeh 进行比较可能会很有趣)。

The solution by @tm1 is excellent. Mine is a little less complicated (if you're ok with functional programming):

% initialize `fig` somehow, i.e., "fig=gcf()" for the current figure or
% "fig=get(0,'children')" for all open figures; can be vector or scalar.

ax = findall(fig, 'type', 'axes');
ax = ax(arrayfun(@(i) strcmp(class(handle(i)), 'axes'), ax));

ax will contain only plot axes. This works because the class of a legend or colorbar object is different from axes.

Edit @Jonas points out a potential improvement to filter the results of findall, because at least legends and colorbars seem to have non-empty Tag properties: replace the last line in the above code snippet with

ax = ax(strcmp('', get(ax, 'Tag')))

Both these techniques are kludgy and may break in the future (a comparison against ggplot2 or Bokeh might be interesting).

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