使用通配符在单元格数组中查找字符串



我有一个单元格数组,其最后一部分如下所示:

Columns 8372 through 8375
{'w20091231_2000.nc'}    {'w20091231_2020.nc'}    {'w20091231_2040.nc'}    {'w20091231_2100.nc'}
Columns 8376 through 8379
{'w20091231_2120.nc'}    {'w20091231_2140.nc'}    {'w20091231_2200.nc'}    {'w20091231_2220.nc'}
Columns 8380 through 8383
{'w20091231_2240.nc'}    {'w20091231_2300.nc'}    {'w20091231_2320.nc'}    {'w20091231_2340.nc'}
Columns 8384 through 8387
{'wD1.nc'}    {'wD2.nc'}    {'wD3.nc'}    {'wD4.nc'}

现在我想重新排列这个数组,使其只包含最后四个字符串。{'wD1.nc'} {'wD2.nc'} {'wD3.nc'} {'wD4.nc'}

我试过了

IndexC = strfind(names,'wD*.nc');
Index = find(not(cellfun('isempty',IndexC)))

Index = find(contains(names,'wD*.nc'));
names2=names(Index)

如果 wD*.nc wD4.nc,两者都有效,但当然我只选择一个值,而不是我想要的四个值。 如何使用*

我不得不做一些谷歌搜索,但发现这个 https://www.mathworks.com/matlabcentral/answers/77039-comparing-strings-with-wildcards,类似以下内容似乎有效:

IndexC = regexp(names, regexptranslate('wildcard', 'wD*.nc'));
Index = find(not(cellfun('isempty',IndexC)));
names2=names(Index)

在一行中使用正则表达式和match选项:

x = regexp([x{:}],'wDd+.nc','match')

最新更新