Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

netcdf.putAtt

写入 netCDF 属性

语法

netcdf.putAtt(ncid,varid,attrname,attrvalue)
netcdf.putAtt(ncid,varid,attrname,attrvalue,xtype)

说明

netcdf.putAtt(ncid,varid,attrname,attrvalue) 将名为 attrname 的属性及其值 attrvalue 写入 varid 指定的 netCDF 变量。要指定全局属性,可对 varid 使用 netcdf.getConstant('NC_GLOBAL')

ncidnetcdf.createnetcdf.open 返回的 NetCDF 文件标识符。

netcdf.putAtt(ncid,varid,attrname,attrvalue,xtype)attrvalue 作为在 xtype 中指定的数据类型写入。将 xtype 的值指定为:

  • 包含以下值之一的字符向量或字符串标量

    xtype 的值MATLAB®
    NC_DOUBLEdouble
    NC_FLOATsingle
    NC_INT64(仅限 NetCDF-4 文件)int64
    NC_UINT64(仅限 NetCDF-4 文件)uint64
    NC_INTint32
    NC_UINT(仅限 NetCDF-4 文件)uint32
    NC_SHORTint16
    NC_USHORT(仅限 NetCDF-4 文件)uint16
    NC_BYTEint8
    NC_UBYTE(仅限 NetCDF-4 文件)uint8
    NC_CHARchar
    NC_STRING(仅限 NetCDF-4 文件)string
  • netcdf.getConstant 函数返回的等效数值

  • netcdf.defVlen 函数返回的数值类型标识符(用于对应于元胞数组的用户定义的 NC_VLEN 类型的属性)

注意

不能使用 netcdf.putAtt 设置 NetCDF4 文件的 '_FillValue' 属性。使用 netcdf.defVarFill 函数设置变量的填充值。

netcdf.putAtt 函数对应于 NetCDF 库 C API 中的几个属性 I/O 函数。要使用此函数,应该熟悉 NetCDF 编程范式。

示例

全部折叠

本例创建一个新 netCDF 文件、定义维度和变量并将数据添加到变量中,然后创建与该变量相关联的属性。要运行本例,必须拥有当前目录的写访问权限。

% Create a variable in the workspace.
my_vardata = linspace(0,50,50);

% Create a netCDF file.
ncid = netcdf.create('foo.nc','NC_WRITE');

% Define a dimension in the file.
dimid = netcdf.defDim(ncid,'my_dim',50);
 
% Define a new variable in the file.
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,varid,my_vardata);

% Re-enter define mode.
netcdf.reDef(ncid);

% Create an attribute associated with the variable.
netcdf.putAtt(ncid,0,'my_att',10);

% Verify that the attribute was created.
[xtype xlen] = netcdf.inqAtt(ncid,0,'my_att')

xtype =

     6


xlen =

     1

本例创建一个新 netCDF 文件、指定一个全局属性并为该属性赋值。

ncid = netcdf.create('myfile.nc','CLOBBER');
varid = netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,varid,'creation_date',datestr(now));
netcdf.close(ncid);

NC_STRING 类型的字符串数组写入 NetCDF-4 文件中的一个全局属性。然后,返回该全局属性的值。

创建一个 NetCDF-4 文件,并将字符串数组 ["°​F","°​C"] 作为全局属性 Units 的值写入。

ncid = netcdf.create("myfile.nc","NETCDF4");
netcdf.putAtt(ncid, netcdf.getConstant("NC_GLOBAL"),"Units",["°​F","°​C"])
netcdf.close(ncid)

返回该全局属性的值,然后关闭 NetCDF-4 文件。

ncid = netcdf.open("myfile.nc");
netcdf.getAtt(ncid, netcdf.getConstant("NC_GLOBAL"),"Units")
netcdf.close(ncid)

将数据作为字符串数据写入 NetCDF-4 文件中的一个全局属性。

创建一个 NetCDF-4 文件。然后,将字符向量 'March' 作为 'NC_STRING' 类型写入全局属性 Month

ncid = netcdf.create("myfile.nc","NETCDF4");
netcdf.putAtt(ncid, netcdf.getConstant("NC_GLOBAL"), ...
      "Month",'March','NC_STRING')
netcdf.close(ncid)

返回全局属性 Month 的值,然后关闭 NetCDF-4 文件。

ncid = netcdf.open("myfile.nc");
netcdf.getAtt(ncid, netcdf.getConstant("NC_GLOBAL"),"Month")
netcdf.close(ncid)

提示

  • 如果 attrvalue 有多个维度,则 netcdf.putAtt 函数会在写入属性值之前按列优先顺序扁平化 attrvalue。例如,将 attrvalue 指定为 [1 2 3; 4 5 6] 和将 attrvalue 指定为 [1 4 2 5 3 6] 具有相同的作用。

    此外,对于 NC_VLEN 类型的变量,如果 attrvalue 包含任何具有多个维度的条目,则 netcdf.putAtt 函数在写入值之前按列优先顺序扁平化这些条目。例如,对于 NC_VLEN 类型的变量,将 attrvalue 指定为

    {[0.5 0.3]; [0 -0.7 5.2; 4.6 2.5 1.8]}

    attrvalue 指定为

    {[0.5; 0.3] [0; 4.6; -0.7; 2.5; 5.2; 1.8]}

    具有相同的作用。

版本历史记录

全部展开