How to copy a line from multiple files
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
viet le
am 23 Mär. 2017
Bearbeitet: Stephen23
am 26 Apr. 2021
Hello everyone. I have 360 files(.o) (from test(0).o-test(360).o), as figure. I want to copy a line (red framed) and put it in the new .txt file (ordinarily from 0 to 360 means in 361 lines)
Could you guys help me how to do that.
Thank a lot.
2 Kommentare
Akzeptierte Antwort
Stephen23
am 23 Mär. 2017
Bearbeitet: Stephen23
am 26 Apr. 2021
Assuming that that line is uniquely identified by the word 'total' and the line always has the same format, then you can use fileread and regexp in a loop. Here is an outline to get you started (untested):
P = 'dirpath';
S = dir(fullfile(P,'*.o'));
%S = natsortfiles(S); % optional, see comment below
fmt = '\n\s+total\s+(\S+)\s+(\S+)';
for k = 1:numel(S)
F = fullfile(P,S(k).name);
str = fileread(F);
C = regexp(str,fmt,'tokens','once');
vec = str2double(C)
..
end
If you want to process the filenames in the order of the numbers in their names, then you can use my FEX submission natsortfiles:
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!