find certain numbers in txt file

2 visualizaciones (últimos 30 días)
lena kappa
lena kappa el 27 de Jul. de 2022
Comentada: lena kappa el 27 de Jul. de 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.

Respuesta aceptada

Jan
Jan el 27 de Jul. de 2022
Editada: Jan el 27 de Jul. de 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);

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by