本文是博主在实践中发现的一些优美的Matlab代码,总结在此,持续更新。。。

1.多层级目录批量读文件

以REMSS的TMI卫星观测数据为例,目录结构为“年/月/日”,传统方式可使用三重循环嵌套来读取数据,相对而言比较浪费时间;当目录名称有字符合并时,不如巧妙运用通配符和dir来实现单循环完成,同时还可结合并行循环parfor来实现高速读取。最巧妙的语句在于data_dir = dir([data_path,'y*/m*/*_d3d.gz']);

addpath('/Volumes/Science/DATA/REMSS/tmi/tmi_support/matlab/');
data_path = '/Volumes/Science/DATA/REMSS/tmi/bmaps_v07.1/';
data_dir  = dir([data_path,'y*/m*/*_d3d.gz']);
data_dir  = data_dir(4:2576);% first 3 day are bad
%% read sst
sst=zeros(1440,310,2573,'single');sst(:)=nan;
for iDate = 1:2573 
    iDate
    date(iDate) = datetime(data_dir(iDate).name(5:12),'InputFormat','yyyyMMdd');
    data_file = [data_dir(iDate).folder,'/',data_dir(iDate).name(1:end-3)];
    [sst_temp,~,~,~,~,~]=read_tmi_averaged_v7(data_file);
    sst_temp(sst_temp>250)=nan; 
    sst(:,:,iDate)=sst_temp(:,206:515);
    clear sst_temp
end
save('../02_output/TMI_sst_wind.mat','sst','-append','-v7.3');
clear sst

多层级目录

Last modification:March 14th, 2020 at 03:05 pm
If you think my article is useful to you, please feel free to appreciate