find certain numbers in txt file

5 visualizzazioni (ultimi 30 giorni)
lena kappa
lena kappa il 27 Lug 2022
Commentato: lena kappa il 27 Lug 2022
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.

Risposta accettata

Jan
Jan il 27 Lug 2022
Modificato: Jan il 27 Lug 2022
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);

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by