MATLAB: Import various txt files into matlab

importMATLABtxtfiles

Im trying to create a loop to get some informations from all the txt files (like row 4 to row 28) and then get all this information together. The question is this code dont read the txt files and i don't know how to add the result together. If someone can help me would be awesome. The files are all on the same directory and the names are like a1.txt to a328.txt.
numFiles = 328;
startRow = 4;
endRow = 28;
myData = cell(1,numFiles);
for fileNum = 1:numFiles
fileName = sprintf('file%02d.txt',fileNum);
myData{fileNum} = importfile(fileName,startRow,endRow);
end

Best Answer

One thing that certainly does not look right is the format specifier of sprintf if there are indeed 328 files. That specifier will generate filenames of the form:
file01.txt
file02.txt
file10.txt
file99.txt
file100.txt %not consistent!
file328.txt
If the files are indeed called file1.txt, file10.txt, file100.txt, then the format specified should 'file%d.txt', and if the files are called
file001.txt
file010.txt
file099.txt
file328.txt
then the format should be 'file%03d.txt'.
edit: Note that asking your computer for the list of files (with dir) is always more robust than building yourself a list of files that may or may not actually exist.