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

I have 1000 text files, their names are A0000.txt, A0001.text, ..., A1000.text. I want to read the information in text files and save some of them in an excel file or csv file (preferably csv).

How to define a for loop that can accomplish this task?

I can use this function A = textread('A0000.txt','%s')to read a single text file, but I do not know how to put it in a for loop. If the file name was 1.txt, 2.txt, ..., 1000.txt, it would be easier.

I would appreciate it if you could provide any help.

+4
source share
2 answers

sprintf :

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

%04d sprintf, .

+5

txt, . . , .

filelist = dir([pwd() filesep '*.txt' ]);
fileNames = {filelist.name}';
nFiles = length(fileNames);

for i= 1:nFiles 
    TF{i} = textread(fileNames{i},'%s');  
end
+3

Source: https://habr.com/ru/post/1547949/


All Articles