I have to combine 90 txt files in one txt file

1 view (last 30 days)
I have to concatenate 90 txt files (001.txt; 002.txt;...) in a single txt file; each of them has two columns, the first column start always from 0 to 299999 (which is time in ms) and the second column has coordinates (positive and negative number). How can I write a script to do that? I will write an example: First file would look like this:
0 -0.015
1 0.003
2 -0.050
.. ....
299999 0.002
Combined file would look like this:
0 -0.015
1 0.003
2 -0.050
.. ....
299999 0.002
300000 -0.015
300001 0.003
.. ....
1000000 0.022
I tried to combine with several software, but they combine the first column from 0 to 299999 every time for each file. If it would not be for the first column there will no be problem.
I really appreciate any kind of help. This will improve my work. Thank you!

Accepted Answer

Jon
Jon on 1 Dec 2021
If you don't want to create a huge matrix before writing it out you could do this
% define some parameters
outfile = 'alldata.txt' % name for concatenated output file
numFiles = 90; % number of files to be concatenated
numPoints = 299999; % number of data points in file at 1ms intervals
% start the output file with the first datafile
copyfile('001.txt',outfile)
% loop through list of files reading and concatenating (appending)
startTime = 0; % initialize value for starting time
for k = 2:numFiles
% generate file name of form 001.txt, 002.txt, ...
filename = [num2str(k,'%03.f'),'.txt'];
% read the data
data = readmatrix(filename);
% increment the start time
startTime = startTime + numPoints;
% increment the time column
data(:,1) = data(:,1) + startTime;
% append the data (creates file on first pass)
writematrix(data,outfile,'WriteMode', 'append')
end
  8 Comments
Jon
Jon on 7 Dec 2021
Glad it worked for you. I'm not sure that you are getting NaN's form the column being very large, maybe something else is going on there.
By the way, if in the end you found that this approach was what ended up solving your problem, it would probably be good to mark this as the accepted answer, just for people who might have a similar question. The other answer is good to, it just depends upon what fit your needs most closely.

Sign in to comment.

More Answers (1)

DGM
DGM on 1 Dec 2021
This is a start. In this simplified example, there are three files, each with three lines.
You might have to tweak the delimiter specification if you want the output to follow some particular format.
nfiles = 3;
D = [];
for f = 1:nfiles
thisdata = readmatrix(sprintf('test_%03d.txt',f),'range','B:B');
D = [D; thisdata];
end
D = [(0:numel(D)-1).' D]
D = 9×2
0 -0.0150 1.0000 0.0030 2.0000 -0.0500 3.0000 -0.0250 4.0000 0.0730 5.0000 -0.0300 6.0000 -0.0170 7.0000 0.1930 8.0000 -0.2500
writematrix(D,'cattedfile.txt')

Community Treasure Hunt

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

Start Hunting!