【问题标题】:Searching cell array with regex使用正则表达式搜索元胞数组
【发布时间】:2011-07-26 02:00:24
【问题描述】:

我经常发现自己尝试搜索元胞数组,就像我想使用 sql 查询搜索数据库一样。在这种情况下,我有许多军事基地(bases.shp)

bases = shaperead('us-military-bases.shp')

然后我想过滤形状文件以获得空军基地,例如regexp({bases.FAC_NAME}','Air Force')。但是我得到的输出相当繁琐:

[]
[]
[ 4]
[]
[]
[ 9]
[]

我确信过滤单元格数组或 shapefile 非常普遍,并且必须有一些好的做法。感谢您提供任何见解。

我也在尝试这样的事情:

trif = arrayfun(@(x)regexp(x.FAC_NAME,'Griff','match'),af_bases)

【问题讨论】:

    标签: matlab


    【解决方案1】:

    鉴于regexp 的输出,您只需检查结果元胞数组中的每个项目是否为空,即可索引回原始元胞数组。您可以使用cellfun 将函数应用于每个单元格。

    要获得一个逻辑数组,对于非空项目,您可以这样做:

    base_strings = {bases.FAC_NAME}';
    
    ind = ~cellfun(@isempty, regexp(base_strings, 'Air Force'))
    

    或者更简洁地使用匿名函数:

    ind = cellfun(@(x)( ~isempty(x) ), regexp(base_strings, 'Air Force'))
    

    然后,过滤:

    filtered = base_strings(ind);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2011-09-02
      • 1970-01-01
      • 2013-11-02
      相关资源
      最近更新 更多