针对特定属性过滤数组中的字符串 Matlab
Filter Strings in Array for Specific Attributes Matlab
我在 table 中有一列字符串,表示由 dir
函数生成的文件夹中的文件名。
tmpList = struct2table(dir('myFolder'));
该文件夹包含许多不同的文件类型和文件夹。我只想要 excel 个文件,我可以使用以下方法找到这些文件:
filesData = [dir(['myFolder','\*.xlsx']);dir(['myFolder','\*.xls'])];
但是,我该如何展开/替换它,以便我可以过滤 tmpList.name
以仅包含具有以下属性的文件:
- 前三个字母是:'DTE'(大写或小写)
- DTE后面的所有字符都只是数字(并且只有6到8个字符长的数字)
- 扩展名是 .xlsx 或 .xls
例如,对于以下列表,仅标识 1 和 2 以保留:
- 'DTE123456.xlsx'
- 'Dte01234567.xls'
- 'abc12345678.xlsx'
- 'DTE12345c34.xls'
- 'DTE123456.doc'
您可以使用正则表达式查找符合条件的文件名,然后 cellfun
在 tmpList
:
中查找这些文件名的索引
tmpList = struct2table(dir('myFolder')); % your beginning
filteredTmpList = regexpi(tmpList.name(:), '^dte\d{6,8}\.xlsx?$', 'match');
finalList = tmpList(~cellfun('isempty',filteredTmpList), :);
我在 table 中有一列字符串,表示由 dir
函数生成的文件夹中的文件名。
tmpList = struct2table(dir('myFolder'));
该文件夹包含许多不同的文件类型和文件夹。我只想要 excel 个文件,我可以使用以下方法找到这些文件:
filesData = [dir(['myFolder','\*.xlsx']);dir(['myFolder','\*.xls'])];
但是,我该如何展开/替换它,以便我可以过滤 tmpList.name
以仅包含具有以下属性的文件:
- 前三个字母是:'DTE'(大写或小写)
- DTE后面的所有字符都只是数字(并且只有6到8个字符长的数字)
- 扩展名是 .xlsx 或 .xls
例如,对于以下列表,仅标识 1 和 2 以保留:
- 'DTE123456.xlsx'
- 'Dte01234567.xls'
- 'abc12345678.xlsx'
- 'DTE12345c34.xls'
- 'DTE123456.doc'
您可以使用正则表达式查找符合条件的文件名,然后 cellfun
在 tmpList
:
tmpList = struct2table(dir('myFolder')); % your beginning
filteredTmpList = regexpi(tmpList.name(:), '^dte\d{6,8}\.xlsx?$', 'match');
finalList = tmpList(~cellfun('isempty',filteredTmpList), :);