find certain numbers in txt file
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
lena kappa
am 27 Jul. 2022
Kommentiert: lena kappa
am 27 Jul. 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.
0 Kommentare
Akzeptierte Antwort
Jan
am 27 Jul. 2022
Bearbeitet: Jan
am 27 Jul. 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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!