在MATLAB 2015中查找图形数组中的GraphicsPlaceholder

3

最近更新到MATLAB 2015,它改变了图形句柄的存储方式(我认为)。

我将同一图形上各个线条的句柄存储在一个二维数组中。在我以前的MATLAB版本中,任何没有填充的地方都是零。现在这个数组是一个图形数组,未填充的位置是“GraphicsPlaceholder”。有没有办法搜索数组找到它们的位置?

例如,我的句柄列表如下:

    P= 

  2x7 graphics array:

  Columns 1 through 6

    Line                   Line                   Line                   Line                   Line                   Line               
    GraphicsPlaceholder    GraphicsPlaceholder    GraphicsPlaceholder    GraphicsPlaceholder    Line                   GraphicsPlaceholder

我想要做的是搜索并找到哪一列被线条填充而不是图形占位符。理想情况下,它应该是这样的:

FUNCTION(P) = 

 1     1     1     1     1     1
 0     0     0     0     1     0

谢谢

2个回答

3

处理图形变化的方式在R2014b中进行了实现

尝试这样做:

% Generate some graphics objects
myfigs(1, 1) = figure;
myfigs(1, 2) = figure;
myfigs(2, 2) = figure;

% Generate logic array, placeholders have no properties so fieldnames will
% return an empty cell array
arefigures = arrayfun(@(x) ~isempty(fieldnames(x)), myfigs);

这会生成:
myfigs = 

  2x2 graphics array:

    Graphics               Graphics           
    GraphicsPlaceholder    Graphics  

并返回:

arefigures =

     1     1
     0     1

编辑:使用isa更高效的实现,感谢@SamRoberts。

% Generate some graphics objects
myfigs(1, 1) = figure;
myfigs(1, 2) = figure;
myfigs(2, 2) = figure;

% Check array for placeholders, return a logical array of 'real' graphics objects    
arefigures = arrayfun(@(x) ~isa(x, 'matlab.graphics.GraphicsPlaceholder'), myfigs);

2
检查数组元素是否没有属性是一种相当间接的检测GraphicsPlaceholder对象的方式。相反,您可以直接使用isa(x,'matlab.graphics.GraphicsPlaceholder')进行测试。 - Sam Roberts
1
@SamRoberts 很好的观点,我只是在浏览各种is函数的描述,但isa没有完全进入我的脑海。我怪咖啡不够。现在正在编辑,谢谢。 - sco1
谢谢你们两位的帮助,两个建议都很好用。@SamRoberts 的想法有什么优点呢?它看起来更优雅,但是它会更快吗(我只查询3x7矩阵,所以计算时间在我的当前实现中并不是最重要的)? - Helos35
1
@Helos35 我的方法相当巧妙,但它依赖于 GraphicsPlaceholder 的实现保持不变(没有属性),才能正确运行。@SamRoberts 的方法更加明确,只要 GraphicsPlaceholder 保持为 matlab.graphics.GraphicsPlaceholder 对象,就能正常运行。而且它更易读;如果没有注释或者运行代码,我使用的方法为什么有效并不明显。他的方法在技术上也更快,因为只需要一个函数调用,但我不确定在哪个数据大小下差异会真正显著。 - sco1

0
对于R2014b及更高版本,isgraphics(H)可能是您的解决方案。在该函数的文档中: isgraphics(H)返回一个数组,其中包含1,表示H的元素是现有图形对象的句柄,包含0,表示它们不是图形对象或已删除的图形对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接