find certain numbers in txt file

5 ビュー (過去 30 日間)
lena kappa
lena kappa 2022 年 7 月 27 日
コメント済み: lena kappa 2022 年 7 月 27 日
Hi everyone i have a txt that looks like this :
% Theta(AOI) 0.00 Phi(AOI) 0.00
DHR
0.7470
ScatteringPts
1.36
Shadowing
0.000
Warnings
0.09
Errors
0.00
and repeats this sequence like 200 times and i want to keep only the number under shadowing and save it in a new txt and was wondering if there was a way to do it.
I am attaching the file if anyone wants to take a look at it.

採用された回答

Jan
Jan 2022 年 7 月 27 日
編集済み: Jan 2022 年 7 月 27 日
FileName = '20220727_150919_results.txt';
S = fileread(FileName);
C = strtrim(strsplit(S, newline));
m = find(strcmp(C, 'Shadowing'));
NewFile = 'Output.txt';
[fid, msg] = fopen(NewFile, 'w');
assert(fid > 0, '%s', msg);
fprintf(fid, '%s\n', C{m + 1});
fclose(fid);
With modern Matlab versions:
FileName = '20220727_150919_results.txt';
S = readlines(FileName, 'WhitespaceRule', 'trim');
m = find(S == 'Shadowing');
NewFile = 'Output.txt';
writelines(S(m + 1), NewFile);
  1 件のコメント
lena kappa
lena kappa 2022 年 7 月 27 日
Thank you!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by