Main Content

netcdf.getAtt

返回 netCDF 属性

语法

attrvalue = netcdf.getAtt(ncid,varid,attname)
attrvalue = netcdf.getAtt(ncid,varid,attname,output_type)

说明

attrvalue = netcdf.getAtt(ncid,varid,attname) 返回 attrvalue,即由 attname 指定的属性名称的值。将 attname 指定为字符串标量或字符向量。返回的属性值 attrvalue 属于与 attname 的 netCDF 数据类型最匹配的 MATLAB® 数据类型。有关 MATLAB 如何确定最佳匹配的详细信息,请参阅详细信息

attrvalue = netcdf.getAtt(ncid,varid,attname,output_type) 使用 output_type 指定的输出数据类型返回 attrvalue。可将 output_type 指定为以下值之一:

  • "double"

  • "single"

  • "int64"

  • "uint64"

  • "int32"

  • "uint32"

  • "int16"

  • "uint16"

  • "int8"

  • "uint8"

  • "char"

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

示例

本例打开 MATLAB 所含的 netCDF 示例文件 example.nc 并获取与第一个变量关联的属性值。本例还会获取文件中的全局变量值。

% Open a NetCDF file.
ncid = netcdf.open("example.nc","NC_NOWRITE");

% Get name of first variable.
[varname vartype vardimIDs varatts] = netcdf.inqVar(ncid,0);

% Get ID of variable, given its name.
varid = netcdf.inqVarID(ncid,varname);

% Get attribute name, given variable id.
attname = netcdf.inqAttName(ncid,varid,0);

% Get value of attribute.
attval = netcdf.getAtt(ncid,varid,attname);

% Get name of global attribute
gattname = netcdf.inqAttName(ncid,netcdf.getConstant("NC_GLOBAL"),0);

% Get value of global attribute.
gattval = netcdf.getAtt(ncid,netcdf.getConstant("NC_GLOBAL"),gattname)

gattval =

09-Jun-2008

详细信息

全部折叠

NetCDF 到 MATLAB 数据类型的转换

如果未指定可选输入 output_type,则 netcdf.getAtt 会根据下表自动选择与 netCDF 数据类型最匹配的 MATLAB 数据类型。

NetCDF 数据类型MATLAB 数据类型
NC_DOUBLEdouble
NC_FLOATsingle
NC_INTint32
NC_SHORTint16
NC_BYTEint8
NC_CHARchar
NC_STRING (*)string
NC_INT64 (*)int64
NC_UINT64 (*)uint64
NC_UINT (*)uint32
NC_USHORT (*)uint16
NC_UBYTE (*)uint8
用户定义的 NC_VLEN 类型 (*)cell

(*) 这些 netCDF 数据类型仅适用于格式为 netcdf4 的文件。

版本历史记录

全部展开