Merge files with the same prefix.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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?
채택된 답변
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
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
2020년 6월 24일
You need to read the file to a format that is suitable for your use.
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
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:
- select a function that suits your file format, or
- upload a sample file so that someone here can help you to select an appropriate function.
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
@Ivan Mich: Which of these do you want:
- keep only the numeric data, discarding the headers.
- retain the header from the first file.
- retain the header from each file.
- 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
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
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
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
