how to modify a file. txt

6 ビュー (過去 30 日間)
Federico Paolucci
Federico Paolucci 2022 年 10 月 28 日
コメント済み: Jan 2022 年 10 月 29 日
Hello,
I would like some advice from someone to do this job: I have to copy the lines of a text file to an another text file created by me until it comes from a certain string. Arrived at this string, I stop copying and I have to insert at the end of these copied lines other lines of text coming from a third .txt file
Thanks in advance for the advice
  3 件のコメント
MR
MR 2022 年 10 月 28 日
Hi,
does this help you?
% Open file
file = fopen('myFile.txt', 'w');
% Write text into my txt file which I just created
fprintf(file, 'My own Text goes here.');
Federico Paolucci
Federico Paolucci 2022 年 10 月 28 日
@Mathieu NOE yes, i wrote some lines to load the first file
@MR Yes, I did it
Thank you

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

採用された回答

Jan
Jan 2022 年 10 月 28 日
編集済み: Jan 2022 年 10 月 29 日
C = readlines('YourFile.txt');
idx = find(contains(C, 'TheWordYouAreSearching'), 1);
writelines(C(1:idx), 'Output.txt'); % [EDITED] Data at 1st, file name at 2nd
D = readlines('The3rdFile.txt');
writelines(D, 'Output.txt', 'WriteMode', 'append'); % [EDITED], see above
  2 件のコメント
Federico Paolucci
Federico Paolucci 2022 年 10 月 28 日
thanks for these lines! But they give me an error.. can you help me?
Error using writelines
writelines('Output.txt',C(1:idx));
Invalid argument at position 2. Value must be a character vector or string scalar.
Error in MainScript (line 92)
writelines('Output.txt',C(1:idx));
Jan
Jan 2022 年 10 月 29 日
@Federico Paolucci: If a command fails, look into the documentation:
doc writelines
There you see, that the first argument is the data and the second one the file name. I've swapped them.
The code is fixed now.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by