假设有五个文本文件。文件内容为textfile1 = i saw an alligator
,textfile2 = alligator was sitting near a tree
,textfile3 = alligator was sleeping
,textfile4 = parrot was flying
,textfile5 = parrot was flying
。
我使用下面的代码查找包含单词alligator
的文本文件的路径:
sdirectory = 'C:\Users\anurag\Desktop\Animals\Annotations\';
textfiles = dir([sdirectory '*.eng']);
num_of_files = length(textfiles);
C = cell(num_of_files, 1);
for w = 1:length(textfiles)
file = [sdirectory textfiles(w).name];
%// load string from a file
STR = importdata(file);
BL = cellfun(@lower, STR, 'uni',0);
%// extract string between tags
%// assuming you want to remove the angle brackets
B = regexprep(BL, '<.*?>','');
B(strcmp(B, '')) = [];
%// split each string by delimiters and add to C
tmp = regexp(B, '/| ', 'split');
C{w} = [tmp{:}];
end
where = [];
for j = 1:length(C)
file1 = [sdirectory textfiles(j).name];
if find(ismember(C{j},'alligator'))
where = [where num2str(j) '.eng, '];
disp(file1)
end
end
最后,变量file1
将逐个显示包含单词alligator
的文本文件的路径。有没有办法将包含所需单词的文本文件串连接到单元格数组中。