How to merge text files vertically?

조회 수: 7 (최근 30일)
Ivan Mich
Ivan Mich 2020년 6월 1일
편집: Stephen23 2020년 6월 12일
Hello,
How to merge vertically multiple .txt files with the names:
results1.txt, results2.txt,...., results1000.txt?
I want ti merge them to one file.
JHow could I make it?
  댓글 수: 1
Stephen23
Stephen23 2020년 6월 12일
편집: Stephen23 2020년 6월 12일
Note that KSSV's answer will not process the files in alphanumeric order as you probably expect, it will concatenate them in the order that the names are returned by the OS, most likely this order:
results1.txt
results10.txt
results100.txt
results1000.txt
results101.txt
results102.txt
results103.txt
results104.txt
results105.txt
results106.txt
results107.txt
results108.txt
results109.txt
results11.txt
results110.txt
... etc.
results988.txt
results989.txt
results99.txt
results990.txt
results991.txt
results992.txt
results993.txt
results994.txt
results995.txt
results996.txt
results997.txt
results998.txt
results999.txt
which means that your data will be concatenated together in an order that is unlikely to make much sense.
If you want the file data concatenated in an order that follows the number in the filename then you will have to sort the filenames yourself, e.g. downloading and using my FEX submission natsortfiles:

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
KSSV 2020년 6월 1일
txtFiles = dir('*.txt') ; % get the text files in the present folder
N = length(txtFiles) ; % Total number of text files
iwant = cell(N,1) ; % initlaize the data required
% loop for each file
for i = 1:N
thisFile = txtFiles(i).name ;
iwant{i} = importdata(thisFile) ; % read data of the text file
end
iwant = cell2mat(iwant) ;
  댓글 수: 11
Ivan Mich
Ivan Mich 2020년 6월 10일
편집: Ivan Mich 2020년 6월 10일
KSSV, I used your way but command window shows me:
Error using importdata (line 10)
Unable to load file.
Use readtable for more complex formats.
Caused by:
Index exceeds the number of array elements (1).
I am importing one of my .txt files in order to understand
Walter Roberson
Walter Roberson 2020년 6월 12일
That file is odd. It has text numbers. It has what appears to be tab separated fields. It has stretches of binary 0 ("NUL") characters.
It appears that the binary 0 is sometimes put in place of missing data. However there are some places where there is only one binary 0 byte, and there are other places where there are 5 in a row. Without more information we cannot guess whether there is any significance to that.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 6월 1일
projectdir = 'directory files are in';
outdir = 'directory files are to be written to'; %should not be same as projectdir
dinfo = dir(fullfile(projectdir, '*.txt')) ; % get the text files in the present folder
filenames = fullfile(projectdir, {dinfo.name});
N = length(filenames);
Nok = 0;
for K = 1:N
inFile = filenames{K};
[~, basename, ext] = fileparts(inFile);
outfile = fullfile(outdir, [basename ext]);
S = fileread(thisFile);
S = regexprep(S, ',', ' ');
[fid, msg] = fopen(outfile, 'w');
if fid < 0
fprintf('Failed to open output file "%s" because "%s"', inFile, msg);
continue;
end
fwrite(fid, S);
fclose(fid);
Nok = Nok + 1;
end
fprintf('%d of %d files written to "%s"\n', Nok, N, outdir);
It is recommended that outdir not be the same as the source directory. If they are the same, then if something goes wrong with the writing of the replacement information, some or all of the file contents could be lost.
Reliable updating of a file "in-place" is tricky, especially if network file systems are involved; see https://www.mathworks.com/matlabcentral/answers/321087-saving-file-over-ssh-not-working#answer_252188

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by