4

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 :)

2
  • Why do you want to do this in MATLAB? Commented Jul 8, 2013 at 9:44
  • Because when I do that, I can train the data using Neural Network Tool. Is there another way I can do that except Matlab? Commented Jul 8, 2013 at 9:49

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

2 Comments

@Chrysovalando: Just remember you can mark my answer as "accepted" by clicking the big tick mark on the left of my answer. Happy to help!
Very nice as it completely avoids eval. If you have to deal with lots of files with a known number of rows and 2 columns you can consider preallocating A. Start with A = zeros(numberOfRows,length(files)*2) and assign like so: A(:,2*ii-1:2*ii) = newA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.