Select Specific NC Files in Folder Using Date Included in Filename
1 次查看(过去 30 天)
显示 更早的评论
Good evening! I hope this message finds everyone healthy and well.
I'm currently working with a series of .nc files which represent over five years of data. These files are all contained in the same folder and have a similar format (e.g MERRA2_100.inst6_3d_ana_Np.19800604.SUB.nc). I also have a matrix with three columns, where the first column indicates a month, the second column a day, and the third column a year (e.g. 06, 04, 1980).
My goal is to select all of the .nc files within my folder that match the dates included in my matrix. So, if the first row in my matrix indicates June 4th, 1980 (06, 04, 1980), I would like my code to select the appropriate .nc file from my folder (MERRA2_100.inst6_3d_ana_Np.19800604.SUB.nc). I would then like to read and average the variables in my selected files, but I think I know how I would like to go about with that. Any suggestions on this matter would be greatly appreciated. Thanks in advance!
Here's what I have so far:
matrix = [month day years]
myFolder = 'C:\Users\michelle\files'
filePattern = fullfile(myFolder, '*.nc')
theFiles = dir(filePattern)
for k = 1:length(theFiles)
baseFileName = theFiles(k).name
fullFileName = fullfile(theFiles(k).folder, baseFileName)
fprintf(1, 'Now reading %s\n', fullFileName)
end
0 个评论
回答(1 个)
per isakson
2021-1-22
Something like this should do it
matrix = [ 6, 4, 1980
6, 4, 1981 ];
dates = string( datestr( [matrix(:,[3,1,2]), zeros(size(matrix))], 'yyyymmdd' ) );
theFiles = dir( fullfile( 'C:\Users\michelle\files', 'MERRA*.nc' ) );
for jj = 1 : numel( theFiles )
filespec = string( fullfile( theFiles(jj).folder, theFiles(jj).name ) );
if any( contains( filespec, dates(jj) ) )
fprintf( 'Now reading %s\n', filespec )
end
end
Not tested!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!