read date from character string

13 次查看(过去 30 天)
hi... i have data files(HDF5 format) ... in different sub folders. there are around 50,000 files. i have made a program to pick all HDF5 files from all subfolders and read. there is date and time in thair names which i want read. file names are like bellow
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151031233116_051_METOPB_16186_DLR_03.HDF5
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101001652_051_METOPA_46873_DLR_03.HDF5
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101011234_051_METOPB_16187_DLR_03.HDF5
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101015816_050_METOPA_46874_DLR_03.HDF5
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101025358_051_METOPB_16188_DLR_03.HDF5
GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101033940_050_METOPA_46875_DLR_03.HDF5
datenum(filename(42:56),'yyyymmddHHMMSS'); %% to read date and time from file name
this works fine but problem occurs when name of the file changes .there are few files whose names are different than above.for eg.
GOME_O3-NO2-NO2Tropo-BrO-H2O-HCHO_L2_20070920104919_049_METOPA_04770_DLR_03.HDF5
in this file some characters are missing (-SO2)
when i run a loop it runs properly but when loop find this type of name it stops there and said error. i can not delete this files.hope you understand my question.

采纳的回答

Stephen23
Stephen23 2015-11-5
编辑:Stephen23 2015-11-5
This is easy using regexp to match the date substring:
C = {'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151031233116_051_METOPB_16186_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101001652_051_METOPA_46873_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101011234_051_METOPB_16187_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101015816_050_METOPA_46874_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101025358_051_METOPB_16188_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-SO2-H2O-HCHO_L2_20151101033940_050_METOPA_46875_DLR_03.HDF5'
'GOME_O3-NO2-NO2Tropo-BrO-H2O-HCHO_L2_20070920104919_049_METOPA_04770_DLR_03.HDF5'};
D = regexp(C,'\d{14}','match','once');
E = datenum(D,'yyyymmddHHMMSS');
and the output:
>> datevec(E)
ans =
2015 10 31 23 31 16
2015 11 1 0 16 52
2015 11 1 1 12 34
2015 11 1 1 58 16
2015 11 1 2 53 58
2015 11 1 3 39 40
2007 9 20 10 49 19

更多回答(1 个)

Thorsten
Thorsten 2015-11-5
编辑:Thorsten 2015-11-5
This works if there is always the string "_L2_" right before the date:
i1 = strfind(filename, '_L2_')+4;
datenum(filename(i1:i1+14),'yyyymmddHHMMSS');

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by