将来自不同文本文件的文本连接成一个

时间:2014-04-28 05:40:13

标签: matlab

假设有五个文本文件。文件内容为textfile1 = i saw an alligatortextfile2 = alligator was sitting near a treetextfile3 = alligator was sleepingtextfile4 = parrot was flyingtextfile5 = 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的文本文件的路径。有没有办法将包含所需单词的文本文件串连接到单元格数组中。

0 个答案:

没有答案