MATLAB: How to read netcdf files containing string arrays

loadnetcdfstring array

I have a netCDF file which consists of a float array and a string array as verified by ncdump. In Matlab, I am able to load the float array, but I fail in loading the string array.
In particular, ncread produces an error:
ncread('string_test.nc','StringArray')
Error using netcdf.getVar (line 130)
12 is not a recognized netCDF datatype.
"12" is the code for NC_STRING and it seems like Matlab can not handle that.
The .nc file I'm trying to load can be downloaded from http://piotr.majdak.com/temp/netcdf/ where also more description is available.
I'm using Matlab 2011b – any ideas what I can do?

Best Answer

  • I use R2012a.
    ncdisp says: Datatype: UNSUPPORTED DATATYPE. See below. Why not accept that? What is the source of string_test.nc?
    HDFView 2.9 reads string_test.nc as if it was an HDF5-file. So does Matlab's h5disp and h5read.
    >> ncdisp( 'string_test.nc')
    Source:
    h:\m\cssm\string_test.nc
    Format:
    netcdf4
    Dimensions:
    test = 4
    Variables:
    FloatArray
    Size: 4x1
    Dimensions: test
    Datatype: single
    StringArray
    Size: 4x1
    Dimensions: test
    Datatype: UNSUPPORTED DATATYPE
    >> str = h5read( 'h:\m\cssm\string_test.nc', '/StringArray' )
    str =
    'NH10'
    'CI03'
    'HI17'
    'NH11'
    >>
    >> h5disp( 'h:\m\cssm\string_test.nc', '/StringArray' )
    HDF5 string_test.nc
    Dataset 'StringArray'
    Size: 4
    MaxSize: 4
    Datatype: H5T_STRING
    String Length: variable
    Padding: H5T_STR_NULLTERM
    Character Set: H5T_CSET_ASCII
    Character Type: H5T_C_S1
    ChunkSize: []
    Filters: none
    Attributes:
    'DIMENSION_LIST': H5T_VLEN