Read csv files by regulare expression function

Hi,
I have multiple .csv files in the following format from:
TRMM_1998_01_newntcl.csv
TRMM_1998_02_newntcl.csv
.
.
.
TRMM_1998_12_newntcl.csv
TRMM_1999_01_newntcl.csv
.
.
.
TRMM_1999_12_newntcl.csv
I have the following code and I need to modify to read by month (1-12). I appreciate if someone can help me to figure this out and modify the following code.
Thanks in advance.
sad = dir('TRMM_*_newntcl.csv');
cac = regexp( {sad.name}, '1998_[01]\d_[0-3]\_' );
ism = not( cellfun( @isempty, cac ) );
d = sad(ism);
ymdh=cell2mat(textscan([d.name], ...
'TRMM_%4d_%2d_%2d%2d_newntcl.csv', ...
'collectoutput',true));

 採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 7 日

1 投票

month_to_match = 8; %for example
str_for_month = sprintf('%02d', month_to_match);
regexp_for_month = [ '^TRMM_[12]\d{3}_[01]\d_' str_for_month '_'];
cac = regexp( {sad.name}, regexp_for_month );

3 件のコメント

Damith
Damith 2015 年 5 月 7 日
編集済み: Damith 2015 年 5 月 7 日
This part
'^TRMM_[12]\d{3}_[01]\d_'
in the following line does not read as TRMM_year_ but 'str_for_month' displaying correct month 08 in the above example. Therefore, it reads all the files.
I have 48 csv files starting from TRMM_1998_01_newntcl.csv to TRMM_2001_12_newntcl.csv.
regexp_for_month = [ '^TRMM_[12]\d{3}_[01]\d_' str_for_month '_']; This line does not work.
Walter Roberson
Walter Roberson 2015 年 5 月 8 日
regexp_for_month = [ '^TRMM_[12]\d{3}_' str_for_month '_'];
Damith
Damith 2015 年 5 月 8 日
OK. Thanks. This worked.

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

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 5 月 7 日
編集済み: Stephen23 2015 年 5 月 7 日

0 投票

You could avoid the whole regexp thing entirely by using my FEX submission datenum8601:
>> N = cellfun(@datenum8601, strrep({sad.name},'_','-'));
>> datevec(N)
ans =
1998 1 1 0 0 0
1998 2 1 0 0 0
1998 12 1 0 0 0
1999 1 1 0 0 0
1999 12 1 0 0 0

1 件のコメント

Damith
Damith 2015 年 5 月 7 日
Thanks Stephen.But, I need to somehow use to above approach with regexp function.

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2015 年 5 月 7 日

コメント済み:

2015 年 5 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by