Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 276
sara_123 Avatar asked Feb 12 '23 13:02

sara_123


1 Answers

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.

like image 53
Andrey Rubshtein Avatar answered Feb 15 '23 10:02

Andrey Rubshtein