listing of file using num2str

8 views (last 30 days)
Chiranjibi
Chiranjibi on 12 Nov 2014
Commented: Geoff Hayes on 14 Nov 2014
I'm trying to make a list of file name of specific path name, when I tried this code it just make a file name of only last day(i.e 19).But I want to make a list of file from 12 to 19.
firstDay= 12 ;
lastDay = 19;
a=[];
f = '/data/raw/';
for day = firstDay:lastDay
if (day<10)
tDay = ['0' num2str(day)];
else
tDay = num2str(day);
end
fName = fullfile(f,['VData0203_1311' tDay '*']);
a=[a,dir(fName)];
end
Even when I tried tDay(day) - shows error
Any hint would be appreciated.

Answers (1)

Geoff Hayes
Geoff Hayes on 13 Nov 2014
Chiranjibi - your code seems to be producing a distinct file name at each iteration of the for loop, though depending upon the number of files in each directory, you may observe a horzcat error. I would try using a cell array for your a matrix so that folders with a different number of files do not cause a problem.
You can also simplify your code by removing the if statement. The num2str function allows you to choose the format specification for your integer so you can automatically pad a single digit with a zero or leave as is according to
tDay = num2str(day,'%02d');
Your code then becomes
firstDay = 12;
lastDay = 19;
a = {};
f = '/data/raw/';
for day = firstDay:lastDay
tDay = num2str(day,'%02d');
fName = fullfile(f,['VData0203_1311' tDay '*']);
a=[a,dir(fName)];
end
where fName becomes one of the following on each iteration of the loop
/data/raw/VData0203_131112*
/data/raw/VData0203_131113*
/data/raw/VData0203_131114*
/data/raw/VData0203_131115*
/data/raw/VData0203_131116*
/data/raw/VData0203_131117*
/data/raw/VData0203_131118*
/data/raw/VData0203_131119*
  2 Comments
Chiranjibi
Chiranjibi on 13 Nov 2014
Edited: Chiranjibi on 13 Nov 2014
Thanks Geoffs, but my fName gives only one file;
/data/raw/VData0203_131119*
When I put tDay(day) that also gives error- elements must be same. Any hint?
Geoff Hayes
Geoff Hayes on 14 Nov 2014
Chiranjibi - you will need to clarify what you mean by but my fName gives only one file. What are you expecting to see? Your code (as it stands) just loops through all 12 days creating the fName filter which you then query on to get a set of files matching that filter.
As for the error with tDay(day), you will need to describe how you are using this. What are you initializing tDay to?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!