我有一个文本文件,其内容例如是:
[1] John where are you bring me copy number = 1 4 5
[2] Hi, Sam what is the cost of your calculator = 200 500 800
[3] Nancy, bring me the no. of copy that I gave you = 4 8 5
[4] Hi, how many litres of milk you have = 10 12 6
[5] Peter, give your copy numb = 23 45 32
& so on.
我对那些包含“复制”一词的行很感兴趣。下面的代码为我提供了文本文件中包含“copy”一词的行号。
fid = fopen(‘my_file.txt', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
% Search for string ‘copy’ and find all rows that matches it
D = strfind(C{1}, 'copy');
rows = find(~cellfun('isempty', D));
我想在另一个文本文件中打印带有'copy'一词的那些行。
答案 0 :(得分:1)
首先,您需要选择所需的内容:
C2=C{1}(rows);
fprint
可以编写逗号分隔列表,允许在一行中写入文件:
fid2=fopen(....
fprintf(fid2,'%s\n',C2{:});
fclose(fid2);