matlab官方文档只给出了简单的写入一个变量情况,以下程序给出了同时写入多个变量到nc格式文件的例子。
经纬度是一维情况:
clear
clc
% 构造变量名和变量尺寸
nccreate('myncfile.nc','data',...
'Dimensions', {'x',3,'y',6,'z',100},...
'FillValue','disable');
nccreate('myncfile.nc','lon','Dimensions',{'x',3});
nccreate('myncfile.nc','lat','Dimensions',{'y',6});
nccreate('myncfile.nc','time','Dimensions',{'z',100});
% 存入数据,注意数据尺寸一致
ncwrite('myncfile.nc','lon',zeros(3,1));
ncwrite('myncfile.nc','lat',zeros(6,1));
ncwrite('myncfile.nc','time',zeros(100,1));
ncwrite('myncfile.nc','data',zeros(3,6,100));
% 查看信息
info = ncinfo('myncfile.nc'); %检验是否成功写入nc文件
经纬度是二维情况:
nccreate('myncfile.nc','data','Dimensions',{'x',39,'y',52,'z',12},'FillValue','disable');
nccreate('myncfile.nc','lon','Dimensions',{'x',39,'y',52});
nccreate('myncfile.nc','lat','Dimensions',{'x',39,'y',52});
nccreate('myncfile.nc','time','Dimensions',{'z',12});
% 存入数据,注意数据尺寸一致
ncwrite('myncfile.nc','lon',lon);
ncwrite('myncfile.nc','lat',lat);
ncwrite('myncfile.nc','time',time);
ncwrite('myncfile.nc','data',data);