How to write a for-loop to read 1000 text files in Matlab?

matlab, text-files

Solution

You should use `sprintf` to generate the relevant strings:

for i=1:1000
    fileName = sprintf('A%04d.txt',i);
    A{i} = textread(fileName ,'%s')
end

The `%04d` tells `sprintf` that the number should have leading zeroes.

Problem

I have 1000 text files that their names are A0000.txt, A0001.text, ..., A1000.text. I want to read in the information in the text files and store some of them in an excel file or a csv file (csv is preferred). How should I define a for loop that can do this task? I can use this function `A = textread('A0000.txt','%s')` to read one text file, but I don't know how should I put it in a for loop. If the name of the files were 1.txt, 2.txt,..., 1000.txt it would be easier. I would be thankful if you can provide any help.

Original source