Merge files with the same prefix.

1 回表示 (過去 30 日間)
Ivan Mich
Ivan Mich 2020 年 6 月 24 日
コメント済み: Rik 2020 年 6 月 24 日
I have multiple .txt files. From these files have the same three letters in their name. I want to create a new file that will contain the data of all the files that have the first three letters in their name (e.g. "ABC****P.txt, ABC****T.txt, BCA****P.txt, BCA****T.txt". I want to megre in one file the first two .txt and one file the last two .txt files)
could you help me please?

採用された回答

Stephen23
Stephen23 2020 年 6 月 24 日
編集済み: Stephen23 2020 年 6 月 24 日
This should get you started (untested, but gives an outline of how you could do this):
D = 'path to the folder where the files are saved';
S = dir(fullfile(D,'*.txt'));
C = {S.name};
T = regexp(C,'^.{3}','once','match');
U = unique(upper(T));
for k1 = 1:numel(U)
X = find(strncmpi(C,U{k1},3));
N = numel(X);
A = cell(1,N);
for k2 = 1:N
F = fullfile(D,C{X(k2)});
A{k2} = ...whatever file importing that suits your file format.
end
M = vertcat(A{:});
F = sprintf('%s merged.txt',U{k1});
... save matrix M with filename F using whatever file exporting that suits your data
end
  9 件のコメント
Ivan Mich
Ivan Mich 2020 年 6 月 24 日
I would like to merge them horizontally (I mean I want to have 4 columns, which the firts two are from the one file and the last two are from the second file). I do not want to have the headers.
Rik
Rik 2020 年 6 月 24 日
Have you read the documentation for every function you didn't understand in the answer?
Let me give you a short-cut: read the documentation for sprintf.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by