www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/WriteVariabletoNewnetCDFFileExample.m

    %% Write Variable to New netCDF File  
% Create a new netCDF file and write a variable to the file.   

%% 
% Create a 50 element vector for a variable. 
my_vardata = linspace(0,50,50);  

%% 
% Open the netCDF file. 
ncid = netcdf.create('foo.nc','NOCLOBBER');  

%% 
% Define the dimensions of the variable. 
dimid = netcdf.defDim(ncid,'my_dim',50);  

%% 
% Define a new variable in the file. 
my_varID = netcdf.defVar(ncid,'my_var','double',dimid);  

%% 
% Leave define mode and enter data mode to write data. 
netcdf.endDef(ncid);  

%% 
% Write data to variable. 
netcdf.putVar(ncid,my_varID,my_vardata);  

%% 
% Verify that the variable was created. 
[varname xtype dimid natts ] = netcdf.inqVar(ncid,0)  

%% 
% Close the file. 
netcdf.close(ncid)