Searching cell array with regex

前端 未结 1 1798
时光取名叫无心
时光取名叫无心 2021-01-06 17:15

I often find myself trying to search cell arrays like I would want to search a database with a sql query. In this case, I\'ve got a number of military bases (bases.shp)

相关标签:
1条回答
  • 2021-01-06 17:38

    Given the output of regexp you can index back into the original cell array just by checking if each item in the resultant cell array is empty. You can do this using cellfun to apply a function to each cell.

    To get an array of logicals, for non-empty items you can do:

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

    Or more cleanly using an anonymous function:

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

    Then, to filter:

    filtered = base_strings(ind);
    
    0 讨论(0)
提交回复
热议问题