How to merge text files vertically?

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일

0 개 추천

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월 1일
Ok thank you. I have a question. I have created a loop. I want to create multiple summary files. My code creates 100 .txt files, I merge them with your command, but I want for each iteration to create different output files.
How could I write the command
writematrix?
KSSV
KSSV 2020년 6월 1일
Already the data is in different text files...why you want to write into different files now?
Ivan Mich
Ivan Mich 2020년 6월 1일
편집: Ivan Mich 2020년 6월 1일
Because, I am running this code with a loop, and has many iterations. That's why I want to create mulpitle output files. Because with every iteration the previous files are getting overwritted.
So I want for first iteration to have a final1.txt file.
For the second iteration I want to have fianl2.txt file etc
for n=1:numel(st);
for i=1:size(nam);
FP=fopen(sprintf('m%g0.txt',i),'wt');
fprintf(FP,'%s\t',num2str(Results));
fclose(FP);
txtFiles = dir('misfit*.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)
end
end
I am sending you my code.
How can I add the writematrix command?
for i = 1:10
outFile = strcat('final',num2str(i),'.txt') ;
dlmwrite(outFile,data)
end
Ivan Mich
Ivan Mich 2020년 6월 1일
I am sorry, but this does not help me.
Do you know an alternative way?
Ivan Mich
Ivan Mich 2020년 6월 1일
I mean one output file has this format:
10,11.905
100,8.6826
110,10.063
120,11.433
20,9.0166
200,12.784
210,14.11
I would like to have one .txt file, with two columns , and NOT a comma between the two numbers in each line. Secondly , I would like the numbers in the first column to be ascending.
To sum up, I want my output file to have this format:
10 11.905
20 9.0166
100 8.6826
110 10.063
120 11.433
200 12.784
210 14.11
Could you help me please?
Walter Roberson
Walter Roberson 2020년 6월 1일
dlmwrite permits specifying space as the delimiter.
KSSV
KSSV 2020년 6월 1일
You can use fprintf. Read about it.
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일

0 개 추천

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

카테고리

질문:

2020년 6월 1일

편집:

2020년 6월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by