最佳实践来确定输入的是数字或轴处理?
108(30天)
显示旧的评论
我一直困惑于MATLAB图形自改变新的图形系统。我不知道我可以检查一个变量是如何处理图或轴。函数的想到的是:
ishandle
isgraphics
ishghandle
isa (x,“matlab.ui.Figure”)% x变量代表一个数字或轴
isa (x,“matlab.graphics.axis.Axes”)%哇,这些真的是繁琐的命令!
%等。
什么是最佳实践,以确保最大兼容大多数MATLAB版本检测什么样的输入我处理吗?为什么没有
isfigure
或
isaxes
功能内置?
4评论
adams13
2017年1月18日
如果你在谈论兼容性比我还将使用一种有效的方法在八度:
函数isAxes = isAxes (ax)
试一试
isAxes = strcmp (get (ax,“类型”),“轴”);
抓
isAxes = false;
结束
结束
接受的答案
Bjorn Gustavsson
2016年8月25日
简便性,自己编写这些功能,沿着线:
函数好吧= isfigure (h)
如果比较字符串(get (h,“类型”),“图”)
好吧= 1;
其他的
好吧= 0;
结束
你可以装饰你的代码来处理数组输入,错误检查和做输出适当的布尔值输出,但核心部分是看类型的图形处理领域。
HTH
2的评论
亚当Danz
2019年5月4日
编辑:亚当Danz
2021年3月2日
像其他两个答案,这种方法也不能区分图类的matlab.ui的把手。图' and a double precision integer that happens to have the same value as a figure number ( I provided examples below the other two answers ). This becomes an issue in the case of a function with a variable number of input arguments (varargin) where an input could be a figure handle or an integer that has the same value as a figure number.
在r2014b进行区分,之后,检查变量的类。
tf = strcmp (get (h,“类型”),“图”)& isa (h,“matlab.ui.Figure”);
更多的答案(3)
埃里克
2016年10月1日
2的评论
亚当Danz
2019年5月4日
编辑:亚当Danz
2021年3月2日
注意,
isgraphics ()
不区分图类的matlab.ui的把手。图' and a double precision integer that happens to have the same value as a figure number (for backward compatibility
r2014b之前
)。
关闭所有
无花果=图();%分配图1
isfig = isgraphics(图,“图”)
isfig = isgraphics (1,“图”)
这是因为
处理(1)
只接受数字处理和不匹配整数r2014b和之后,
关闭所有
无花果=图();%分配图1
isfig = isgraphics(图,“图”)& & isa(图“matlab.ui.Figure”)
isfig = isgraphics (1,“图”)& & isa (1,“matlab.ui.Figure”)
PepijnB
2018年7月11日
编辑:PepijnB
2018年7月11日
在“eyediagram”功能(内置Matlab函数),Mathworks检查的有效性的处理代码:
ishghandle (h,“图”)
检查手柄是否从一个轴,使用以下:
ishghandle (h,“轴”)
亚当Danz
2021年3月2日
编辑:亚当Danz
2021年3月2日
我发现自己重新审视这个页面的时间和想分享的另一种选择
最佳实践
以确定数组的输入数据或轴。它是基于
埃里克
的解决方案使用
isgraphics (h型)
这是记录至少早在
r2015b
在更古老的版本和功能,但两个差异:
- 区分图或轴处理和双精度值匹配处理值,r2014a之后。例如,关闭所有;h =图;isgraphics(图)返回真正的这可能是r14a后问题在某些情况下。
- 包括polaraxes is-axes测试
测试r2020b & r2014a后删除功能的演示并不存在。
% h是一个数组;函数返回一个逻辑数组大小一样的h。
isfigure = @ (h) isgraphics (h,“图”)& (verLessThan (matlab的,“8.4”)| ~ isa (h,“双”));
isaxes = @ (h) (isgraphics (h,“轴”)| isgraphics (h,“polaraxes”)& (verLessThan (matlab的,“8.4”)| ~ isa (h,“双”));
演示:
% 5人物和5轴
关闭所有
vis =“关闭”;
图(h (1) =“可见”、vis);%能见度不需要演示
h(2) =图(“可见”、vis);
h(3) =图(“可见”、vis);
h (4) = uifigure (“可见”、vis);
h (5) = uifigure (“可见”、vis);
h(6) =轴(h (1));
h (7) = polaraxes (h (2));
h(8) =次要情节(2 2 1,“父”h (3));
h (9) = uiaxes (h (4));
tlo = tiledlayout (h (5), 1, 2);
h (10) = nexttile (tlo);
清理= onCleanup(@()删除(h));
isfig = isfigure (h)
% isfig =
% 1×10逻辑数组
% 1 1 1 1 1 0 0 0 0 0
isax = isaxes (h)
% isax =
% 1×10逻辑数组
% 0 0 0 0 0 1 1 1 1 1
这个方法正确地拒绝双精度值匹配的图和轴处理转换为双(> r14a):
val =双(h (1:3))
% val =
% 1 2 3
isfig = isfigure (val)
% isfig =
% 1×3逻辑数组
% 0 0 0
如果你是故意使用图形处理后的双精度版本r2014a您可以删除这些条件的函数或转换处理的输入,
isfig = isfigure(处理(val))
% isfig =
% 1×3逻辑数组
% 1 1 1