MATLAB Answers

How to import a set of NetCDF Files?

19 views (last 30 days)
YH
YH on 8 Nov 2018
Commented: CHANG XV on 14 Nov 2019
Hallo, Does someone know how to import a complete Dataset of NetCDF files. I have 54 files representing the sea levels from 1948 to 2011. so far I know how to read one file with ncread but I couldnt't find out how to import the entire set of the files.

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 8 Nov 2018
ncfiles = dir('*.nc') ; % you are in the folder where nc files are present
N = length(ncfiles) ; % number of files
% loop for each file
for i = 1:N
thisfile = ncfiles(i).name;
% load the data and do what you want
end

  4 Comments

Show 1 older comment
KSSV
KSSV on 9 Nov 2018
ncfiles = dir('*.nc') ;
variables_to_load = {'lat' , 'lon' , 'time' , 'e'} ;
for i = 1:length(ncfiles) ;
% loop over the variables
filename = ncfiles(i).name ;
for j=1:numel(variables_to_load)
% extract the jth variable (type = string)
var = variables_to_load{j}
% use dynamic field name to add this to the structure
data_struct.(var) = ncread(filename,var);
% convert from single to double
if isa(data_struct.(var),'single')
data_struct.(var) = double(data_struct.(var));
end
end
lat=getfield(data_struct,'lat');
lon = getfield(data_struct,'lon');
e= getfield(data_struct,'e');
time= getfield(data_struct,'time');
end
YOur code can be further simplified.
YH
YH on 9 Nov 2018
I tried to do so, it gives me back 3 errors regarding ncread
Error using internal.matlab.imagesci.nc/openToRead (line 1272) Could not open cD-2_TRIM-NP-2d-Baltic_6.4_e_001.nc for reading.
Error in internal.matlab.imagesci.nc (line 121) this.openToRead();
Error in ncread (line 53) ncObj = internal.matlab.imagesci.nc(ncFile); Do you have an idea, why I am getting these errors?
CHANG XV
CHANG XV on 14 Nov 2019
Have you sorted out this problem? I encounter the same problem this days. I have no idea about that. Thanks !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!