Deleting Rows in Text File and Assigning them to a New Text

4 ビュー (過去 30 日間)
tinkyminky93
tinkyminky93 2022 年 6 月 8 日
編集済み: Rik 2022 年 6 月 8 日
Hello,
I want to find the rows which contain "temp" word in my text file, then, I want to delete these rows. After that, what I want to do is, assigning the rest of the rows which are not deleted to a new text file or variable. How can I do it? Thank you.

採用された回答

Rik
Rik 2022 年 6 月 8 日
編集済み: Rik 2022 年 6 月 8 日
Read the text file with readlines (or my readfile function).
Then you can use contains to determine which line contains the text 'temp'. The code below can then be used to write the lines with and without this text.
fprintf(fid1,'%s\n',str{L});
fprintf(fid2,'%s\n',str{~L});
Edit:
To give a more complete code:
filename1='original_file.txt';
filename2='temp_file.txt';
str=cellstr(readlines(filename1));
L=contains(str,'temp');
fid1=fopen(filename1,'w');
fid2=fopen(filename2,'w');
fprintf(fid1,'%s\n',str{L});
fprintf(fid2,'%s\n',str{~L});
fclose(fid1);fclose(fid2);
  3 件のコメント
tinkyminky93
tinkyminky93 2022 年 6 月 8 日
I wrote like
pat = "temp";
TF = contains(text, pat);
str(TF)
Undefined function 'str' for input arguments of type 'logical'
Rik
Rik 2022 年 6 月 8 日
You need to open the orignal file and the new file with fopen to generate file IDs.
Since you apparently stored the text in a variable named text, you will have to use that in the fprintf calls as well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by