Merge 864 single column text files and 60,000 rows in single file. How to get a matrix with 60000 rows and 864 column

2 views (last 30 days)

I have 864 single column text files with 60,000 row in single text file.i would like to get a matrix with 60000 rows and 864 columns. Please help me

  3 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 9 Aug 2022
Edited: dpb on 9 Aug 2022
It would be simpler if you had named the files so they would sort lexically as C001, C002, ... etc., but it's still not too bad...
N=864; % the number files -- better/more general would be to use dir() and count number returned
L=60000; % length of each -- better/more general would be to read first and determine its length
M(L,N)0; % preallocate the output array to those sizes
for i=1:N
fname=char("C"+i+.txt);
M(:,i)=readmatrix(fname);
end
If the filenames were in proper normalized format, I'd be inclined to use the OS to copy and concatenate all into one long file, then read and reshape the resulting long vector instead of the loop...
  5 Comments
dpb
dpb on 11 Aug 2022
You can type as easily as I ... you've got to put at least SOME effort into solving your own problems rather than relying on being spoon-fed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!