How to copy a line from multiple files

1 回表示 (過去 30 日間)
viet le
viet le 2017 年 3 月 23 日
編集済み: Stephen23 2021 年 4 月 26 日
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 件のコメント
KSSV
KSSV 2017 年 3 月 23 日
Is the line number always same in all the files?
viet le
viet le 2017 年 3 月 23 日
yes, they are totally the same

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2017 年 3 月 23 日
編集済み: Stephen23 2021 年 4 月 26 日
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 件のコメント
viet le
viet le 2017 年 3 月 24 日
編集済み: viet le 2017 年 3 月 24 日
Could you help me see where am I wrong, please?
https://uk.mathworks.com/matlabcentral/answers/331720-help-copy-a-line-from-a-file

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by