【问题标题】:Matlab - printing selected lines from a text file in new text fileMatlab - 从新文本文件中的文本文件中打印选定的行
【发布时间】:2014-03-25 13:29:43
【问题描述】:

我有一个文本文件,其内容例如:

[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.

我对那些包含“复制”一词的行感兴趣。下面的代码为我提供了文本文件中包含“复制”一词的行号。

 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));

我想在另一个文本文件中打印带有“复制”一词的那些行。

【问题讨论】:

    标签: string matlab text printf


    【解决方案1】:

    首先,你需要选择你想要的内容:

    C2=C{1}(rows);
    

    fprint可以写一个逗号分隔的列表,这样可以将文件写在一行中:

    fid2=fopen(....
    fprintf(fid2,'%s\n',C2{:});
    fclose(fid2);
    

    【讨论】:

    • 我尝试了上述方法,但出现错误:索引超出矩阵维度。 untitled2 中的错误(第 12 行)C2 = C(rows);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多