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

时间:2019-08-28 09:58:14

标签: matlab find cell

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

fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(xdata, ydata,'-r')
ax.plot(xdata2, ydata2,'-b')
ax.plot((0),(0),'o',markersize=15, markerfacecoloralt='tab:red')
plt.axis('scaled')
plt.show()

现在,我想重新排列此数组,使其仅包含最后四个字符串。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)))

如果wD * .nc是wD4.nc都可以,但是我当然只选择一个值而不选择我想要的四个值。 如何使用Index = find(contains(names,'wD*.nc')); names2=names(Index)

2 个答案:

答案 0 :(得分:1)

我不得不进行一些谷歌搜索,但是发现了这个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)

答案 1 :(得分:1)

在带有match选项的正则表达式中一行:

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