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

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 commentaires

Are Looking the variable names in all the files same?

Connectez-vous pour commenter.

Réponses (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 commentaires

Michelle De Luna
Michelle De Luna le 1 Août 2020
Modifié(e) : Michelle De Luna le 1 Août 2020
Hi @Walter Roberson! I know this is a long shot, but I was hoping to see if you could help me adapt this code to a situation where each of my .nc files has a 4x1 column vector. That is, there are four timesteps in each of the .nc files, and I am trying to combine all of the corresponding data values from all of my .nc files into one large file that I can work with in the future.
Your .nc file has only one variable of interest, and it is always a 4 x 1 result in each file?
How are your files named? In particular if they are numbered files, then we need to know if the numbers have leading zeros -- are the files data1.nc data2.nc ... data10.nc data11.nc ..., etc., or are they data001.nc, data002.nc, data003.nc, ... data010.nc, data011.nc, and so on ? If there are no leading zeros but they are numbered, then we have to compensate a bit to arrange them in the correct order.
Hi Walter, how would one adapt this code to enable it to plot the 'lats' 'lons' and 'precips' data as individual subplots in a figure using the pcolor function?
ntimes = max(cellfun(@length, times));
for K = 1 : num_files
these_times = times{K};
for T = 1 : length(these_times)
%plots are numbered along rows, reverse of usual way!!
subplot(num_files, ntimes, sub2ind([num_files, ntimes], T, K));
pcolor(lons{K}, lats{K}, precip{K}(:,:,T));
title( filenames{K} + " " + string(these_times(T)) );
xtitle('long'); ytitle('lat');
end
end
@Walter Roberson this has been a great help, thank you!
Do you know of a way to have this program go through multiple folders to search for and combine the .nc files? I have similar data spread over multiple folders that I would like joined.
The modification to the code I posted several years ago would be
projectdir = 'C:\some\location';
dinfo = dir( fullfile(projectdir, '**', '*.nc4') );
num_files = length(dinfo);
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
The '**' I used means "all sub-folders at any depths". If you want to use all the folders that are immediate children of the parent directory then use '*' instead of '**'
Bram Loef
Bram Loef le 15 Sep 2022
Modifié(e) : Bram Loef le 15 Sep 2022
And how do I modify this in a way that I only substract the precips with timeseries for a location (thus one value for lat and lon) for all different .nc files?
This program is not working.
What error message are you encountering?

Connectez-vous pour commenter.

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by