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일

0 개 추천

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일
Thank you.
Excuse me but I do not understand line:
A{k2} = ...whatever file importing that suits your file format.
What do you mean?
Rik
Rik 2020년 6월 24일
You need to read the file to a format that is suitable for your use.
Ivan Mich
Ivan Mich 2020년 6월 24일
Excuse me but I am really confused. Could you give me an example of what you mean?
What should I write?
Stephen23
Stephen23 2020년 6월 24일
"What should I write?"
I don't know, because you did not give any information on the file format.
Different file formats can be imported by different functions. You need to either:
  1. select a function that suits your file format, or
  2. upload a sample file so that someone here can help you to select an appropriate function.
Ivan Mich
Ivan Mich 2020년 6월 24일
편집: Ivan Mich 2020년 6월 24일
One file of these I have mention is the file I am importing. I import this in order to understand what do you mean.
All my files have this format
Stephen23
Stephen23 2020년 6월 24일
편집: Stephen23 2020년 6월 24일
@Ivan Mich: Which of these do you want:
  1. keep only the numeric data, discarding the headers.
  2. retain the header from the first file.
  3. retain the header from each file.
  4. something else...
If your answer is 1, then you can use dlmread to import the file, e.g.:
A{k2} = dlmread(F,'',5,0);
and then dlmwrite to export the data at the end.
Rik
Rik 2020년 6월 24일
How do you want to merge them? Do files with the same starting letters have the same date? Should the header be repeated? What does 'merging' mean for your specific data format?
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개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2020년 6월 24일

댓글:

Rik
2020년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by