主要内容

netcdf.putAtt

将数据写入 netCDF 属性

    说明

    netcdf.putAtt(ncid,varid,attname,attvalue) 将名为 attname 且值为 attvalue 的属性写入由 varid 指定的 netCDF 文件或组中由 ncid 标识的 netCDF 变量。写入的属性值属于与 MATLAB®attvalue 数据类型最匹配的 netCDF 数据类型。有关 MATLAB 如何确定最佳匹配的详细信息,请参阅MATLAB 到 NetCDF 数据类型的转换

    示例

    netcdf.putAtt(ncid,varid,attname,attvalue,xtype)attvalue 作为由 xtype 指定的数据类型写入。

    示例

    示例

    全部折叠

    创建示例 netCDF 文件的副本,并打开该副本进行写入。向 avogadros_number 变量写入一个属性。

    copyfile("example.nc","myfile.nc")
    ncid = netcdf.open("myfile.nc","NC_WRITE");
    varid = netcdf.inqVarID(ncid,"avogadros_number");
    
    attname = "significant_figures";
    attvalue = 9;
    netcdf.putAtt(ncid,varid,attname,attvalue)

    使用 netcdf.getAtt 函数检查值是否按预期写入。

    netcdf.getAtt(ncid,varid,attname)
    ans = 
    9
    

    关闭 netCDF 文件。

    netcdf.close(ncid)

    创建并打开一个 netCDF 文件。

    ncid = netcdf.create("myfile.nc","NOCLOBBER");

    写入一个全局属性。对 varid 使用 netcdf.getConstant("NC_GLOBAL")

    varid = netcdf.getConstant("NC_GLOBAL");
    
    attname = "creation_time";
    attvalue = string(datetime("now"));
    netcdf.putAtt(ncid,varid,attname,attvalue)

    使用 netcdf.getAtt 函数检查值是否按预期写入。

    netcdf.getAtt(ncid,varid,attname)
    ans = 
    '13-Jul-2025 18:25:48'
    

    关闭 netCDF 文件。

    netcdf.close(ncid)

    创建并打开一个 netCDF-4 文件。

    ncid = netcdf.create("myfile.nc","NETCDF4");

    使用 MATLAB 字符串数组写入一个全局属性。对 varid 使用 netcdf.getConstant("NC_GLOBAL")

    varid = netcdf.getConstant("NC_GLOBAL");
    
    attname = "temperature_units";
    attvalue = ["°F" "°C"];
    netcdf.putAtt(ncid,varid,attname,attvalue)

    使用 netcdf.getAtt 函数检查值是否按预期写入。

    netcdf.getAtt(ncid,varid,attname)
    ans = 1×2 string
        "°F"    "°C"
    
    

    关闭 netCDF 文件。

    netcdf.close(ncid)

    创建示例 netCDF 文件的副本,并打开该副本进行写入。向 temperature 变量写入一个属性。将 xtype 指定为 "NC_STRING"

    copyfile("example.nc","myfile.nc")
    ncid = netcdf.open("myfile.nc","NC_WRITE");
    varid = netcdf.inqVarID(ncid,"temperature");
    
    attname = "Month";
    attvalue = "March";
    xtype = "NC_STRING";
    netcdf.putAtt(ncid,varid,attname,attvalue,xtype)

    使用 netcdf.getAtt 函数检查值是否按预期写入。

    netcdf.getAtt(ncid,varid,attname)
    ans = 
    "March"
    

    关闭 netCDF 文件。

    netcdf.close(ncid)

    输入参数

    全部折叠

    netCDF 源的标识符,指定为非负整数标量。netCDF 源可以是 netCDF 文件或 netCDF 组。

    数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    netCDF 变量的标识符,指定为非负整数标量。

    要写入全局属性,请将 varid 指定为 netcdf.getConstant("NC_GLOBAL")

    数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    要写入的属性名称,指定为字符串标量或字符向量。

    示例: "myAtt"

    属性值,指定为数值数组、文本或元胞数组。

    注意

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

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

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

    attvalue 指定为

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

    具有相同的作用。

    数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string | cell

    NetCDF 数据类型,指定为字符串标量、字符向量或非负整数标量。

    • 您可以将 xtype 指定为以下字符串标量或字符向量之一。

      xtype 的值MATLAB 类
      "NC_DOUBLE"double
      "NC_FLOAT"single
      "NC_INT"int32
      "NC_SHORT"int16
      "NC_BYTE"int8
      "NC_CHAR"char
      "NC_INT64" (*)int64
      "NC_UINT64" (*)uint64
      "NC_UINT" (*)uint32
      "NC_USHORT" (*)uint16
      "NC_UBYTE" (*)uint8
      "NC_STRING" (*)string

      (*) xtype 的这些值仅对格式为 netcdf4 的源有效。

    • 您可以将 xtype 指定为 netcdf.getConstant 函数返回的数值。

    • 对于对应于 MATLAB 元胞数组的用户定义的 NC_VLEN 类型的属性,您可以将 xtype 指定为由 netcdf.defVlen 函数返回的数值。

    数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

    详细信息

    全部折叠

    提示

    • 您无法使用 netcdf.putAtt 来设置 netCDF-4 文件中变量的 _FillValue 属性。使用 netcdf.defVarFill 函数设置变量的填充值。

    • 此函数对应于 netCDF 库 C API 中的几个 (nc_put_att_*) 函数。

      要使用此函数,必须熟悉 netCDF C 接口。您可以在 netCDF 网站上访问 netCDF 文档。

    版本历史记录

    在 R2008b 中推出

    全部展开