how can I read multiple netcdf file using ncread in matlab?

조회 수: 17 (최근 30일)
Deep Shah
Deep Shah 2017년 9월 2일
편집: Walter Roberson 2023년 6월 14일
I have 6000 files netcdf files.In each file there are four variables like latitude, longitude,precipitation and time.
I made a variable
filename=dir(location of folder in which files are)
now I want to read data from each 6000 files for every variable I write
for j=3:length(filename)
a=ncread('\folder',filename(j).name,variable);
end
but files are not being read. can u tell me how can I read these files?
the format of file is 3B42_Daily.20020315.7.nc4
  댓글 수: 2
KSSV
KSSV 2017년 9월 2일
Are Looking the variable names in all the files same?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2017년 9월 2일
ncvars = {'latitude', 'longitude', 'precipitation', 'time'};
projectdir = 'C:\some\location';
dinfo = dir( fullfile(projectdir, '*.nc4') );
num_files = length(dinfo);
filenames = fullfile( projectdir, {dinfo.name} );
lats = cell(num_files, 1);
lons = cell{num_files, 1);
precips = cell(num_files, 1);
times = cell(num_files, 1);
for K = 1 : num_files
this_file = filenames{K};
lats{K} = ncread(this_file, ncvars{1});
lons{K} = ncread(this_file, ncvars{2});
precips{K} = ncread(this_file, ncvars{3});
times{K} = ncread(this_file, ncvars{4});
end
  댓글 수: 10
Tsehaye Negash
Tsehaye Negash 2023년 6월 14일
This program is not working.
Walter Roberson
Walter Roberson 2023년 6월 14일
What error message are you encountering?

댓글을 달려면 로그인하십시오.


Dushantha Sandaruwan WIJENDRA NAIDHELAGE
편집: Walter Roberson 2023년 6월 14일
This code can be used to substract the specific region from the set of netcdf files
clear all;clc
av_file=dir('*.nc');
ncdisp(av_file(1).name);
lon1=ncread(av_file(1).name,'lon');
lat1=ncread(av_file(1).name,'lat');
% temp1=zeros(141,121,365);
b=[1982:2021];
count=0;
for i=1:40
a=['sst.day.mean.',num2str(b(i)),'.nc'];
sst0=double(ncread(a,'sst'));
a1=find(lon1>=40&lon1<=110);
b1=find(lat1>=-10&lat1<=30);
lon_ind=lon1(a1);lat_ind=lat1(b1);
arb=sst0(a1,b1,:);
[~,~,nt]=size(lhflx1);
sst_final(:,:,count+1:count+nt)=arb(:,:,:);
count=count+nt;
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by