listing of file using num2str

5 ビュー (過去 30 日間)
Chiranjibi
Chiranjibi 2014 年 11 月 12 日
コメント済み: Geoff Hayes 2014 年 11 月 14 日
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.

回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 11 月 13 日
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 件のコメント
Chiranjibi
Chiranjibi 2014 年 11 月 13 日
編集済み: Chiranjibi 2014 年 11 月 13 日
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 2014 年 11 月 14 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by