MATLAB: How to create nc file having lat, long, time, and 2D matrix

nccreatencwritencwriteatt

Helo everyone,
I am trying to create a .nc file which will have lat, long, time and 2D matrix simultaneously.
I have loaded lat, long, and time parameters , but how will I put the corresponding 2D matrix for each lat long and time while creating .nc file.
The answer should come in a matrix which has time in 1st column, lat and long in 1st & 2nd rows, and parameter values corresponding to time and lat_long.
Thank you in advance

Best Answer

Refer this demo example:
% nc filename to be written
file = 'myfile.nc' ;
%% Write lon and lat variables
% Get data
lon = 1:10 ;
lat = 1:10 ;
nx = length(lon) ;
nccreate(file,'lon','Dimensions',{'lon',1,nx},'DeflateLevel',7) ;
ny = length(lat) ;
nccreate(file,'lat','Dimensions',{'lat',1,ny},'DeflateLevel',7) ;
nccreate(file,'time','Dimensions',{'time',1,Inf},'DeflateLevel',7) ;
nccreate(file,'z','Dimensions',{'lon','lat','time'},'DeflateLevel',7) ;
for i = 1:10
ncwrite(file,'time',i,i) % write time
data = rand(10) ;
ncwrite(file,'z',data,[1,1,i]) ; % write 3D data
end
Related Question