How to read Hd5 files in a folder with respect to dates which is itself in the file name

조회 수: 3 (최근 30일)
I have a folder with 100s of hd5 files. file names are identical,only change is in date and time for eg:
3DIMG_01DEC2017_0600_L2G_AOD
3DIMG_02DEC2017_0530_L2G_AOD
,are two files of 2 days. I want to read files of a particular day say of dec 1,and find average of them and store it as a tiff files on same folder. tiff files have to be named automatically with date of the hd5 files which are read.how can it be done?can anyone provide sample codes similar to my issue.Thanks.

답변 (1개)

dpb
dpb 2018년 12월 23일
편집: dpb 2018년 12월 23일
If you just want to retrieve files of a given date, then the simplest solution is
mydate='01DEC'; % the date, write UI code for ease of use
d=dir(sprintf('*%s*.HD5',mydate)); % return the directory listing of those files
for i=1:numel(d)
%read each file
...
end
mn=mean(data);
The output file name can be constructed as desired similarly as to that shown for the input.
The above will pick all years as well; if only one year desired, include it in the defined date string.
If you want to be much more elegant and flexible in the date handling capabilities, return the full list of files (or maybe a sublist if know only care about a single year, say), and convert the embedded time stamp to a datetime and then you can do ranges or whatever is desired to select times easily...
Presuming a list of files of the above format, the first step would be:
% sample dir() return filenames would be...
dh5.name=char('3DIMG_01DEC2017_0600_L2G_AOD.HD5','3DIMG_02DEC2017_0530_L2G_AOD.HD5');
fmt='''3DIMG_''ddMMMyyyy''_''HHmm''_L2G_AOD.HD5'''; % the format string to convert date string
dt=datetime(cellstr(dh5.name),'InputFormat',fmt); % and read the file names, make datetime array.
% show what we get...
>> dt =
2×1 datetime array
01-Dec-2017 06:00:00
02-Dec-2017 05:30:00
>>
Now all the power of logical tests and numeric ordering for ranges, etc., is available to select any given subset of files by date and time, no matter how complex the criteria...

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by