Chrysovalando
Chrysovalando

Reputation: 97

Multiple Text Files to a Single Output (Matlab)

I am new to Matlab and I am struggling with a problem. I have 35 text files, each with different name, and I want to take all of these 35 text files and make them as one. Each file has 2 columns and almost 2000 rows.

The only thing I 've come up so far is to read the text files into Matlab using

for i=1:length(files)

    eval(['load ' files(i).name ' -ascii']);

end

and make the matrix manually using

final = horzcat(AA2,AA3,AA4,MN2,MN4....) 

until I got to the last one.

Is there an easier way? In the future I will be using more than 100 text files so doing it manually is really time consuming!

Thank you :)

Upvotes: 4

Views: 2679

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

You could do this outside of MATLAB:

If you really want to stick to MATLAB,

A = [];
for ii = 1:length(files)

    % load new contents
    newA = load(files(ii).name, '-ascii');

    % concatenate horizontally
    A = [A newA];  %#ok

end

% save final output
save('outputFile.txt', 'A')

Upvotes: 2

Related Questions