How do I import files from a folder one at a time

I have many files (>250) named '1.txt','2.txt',etc. If I'm reading them using
Filelist = dir('*.txt')
The information is getting altered in the program (I don't know how). So, I want to read files one by one. like
Filelist = dir('1.txt')
and so on... I can't figure out how to change the file name from 1.txt to 2.txt... after I have read the first file. Kindly help.

 Accepted Answer

If the information is getting altered by the program, make a backup copy of the variable?

3 Comments

I meant to say, somehow matlab is loading wrong values. The value in txt is 3.458 but matlab shows it as 1.3151 (there are many such values in all files) if I use *.txt but this doesn't happen if I load files one by one.
As far as MATLAB is concerned, there is no difference between
Filelist = dir('1.txt')
and
thisfile = '1.txt';
Filelist = dir(thisfile)
But do be careful if your actual work involves multiple directories: you should be careful to use fullfile() to form proper file names.
Thank you, sir. Your answer has been really helpful.
Now, I'm doing this:
for i=1: number of files to be read
ext = '.txt';
name = strcat(num2str(i),ext);
filename = dir(name);
.....{working on the specific loaded file}
end
It works fine. Computation increased a bit because of the 'for loop' but works just fine.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!