MATLAB: Nc file creation in matlab

.ncMATLAB

I have a lot of ascii files with different lat lon. Like
65.125 5.125 1901 1 1 -999
65.375 5.125 1901 1 1 -999
65.625 5.125 1901 1 1 -999
65.875 5.125 1901 1 1 -999
66.125 5.125 1901 1 1 -999
... plus 1352 more lines
... the full file is attached
I have to create nc file. How can I do it.

Best Answer

clc ; clear all ;
data = importdata('nc.txt') ;
lon = data(:,1) ;
lat = data(:,2) ;
ncfile = 'myncfile.nc' ;
% Longitude
nccreate(ncfile,'lon','Dimensions',{'lon',1,length(lon)},'DeflateLevel',5) ;
ncwrite(ncfile,'lon',lon) ;
ncwriteatt(ncfile,'lon','long_name','longitude');
ncwriteatt(ncfile,'lon','units','degrees');
% Latitude
nccreate(ncfile,'lat','Dimensions',{'lat', 1, length(lat)},'DeflateLevel',5) ;
ncwrite(ncfile,'lat',lat)
ncwriteatt(ncfile,'lat','long_name','latitude');
ncwriteatt(ncfile,'lat','units','degrees');
Related Question