find certain numbers in txt file

5 vues (au cours des 30 derniers jours)
lena kappa
lena kappa le 27 Juil 2022
Commenté : lena kappa le 27 Juil 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.

Réponse acceptée

Jan
Jan le 27 Juil 2022
Modifié(e) : Jan le 27 Juil 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);
  1 commentaire
lena kappa
lena kappa le 27 Juil 2022
Thank you!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by