How to read netcdf files containing string arrays?
35 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
per isakson
2013-3-5
编辑:per isakson
2013-3-5
I use R2012a.
ncdisp says: Datatype: UNSUPPORTED DATATYPE. See below. Why not accept that? What is the source of string_test.nc?
>> 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
3 个评论
per isakson
2013-3-5
编辑:per isakson
2013-3-5
I think:
- strings can be stored in more than one way (format).
- Matlab is often slow to adapt to new versions of supported software.
- When there is a high and a low-level set of functions in Matlab some functionality is typically missing in the high-level functions. I easily believe that Matlab's high-level support is limited. However, I often find it difficult to spot the critical statements (supported or not supported) in the documentation.
- in this context one should avoid the word "must"
My guess:
- ncread (R2012a) doesn't support variable length strings
更多回答(3 个)
Peter Kuma
2018-8-20
Just to let others know, I have submitted a new bug report (since I could not find the one submitted by Erik). The answer was:
As you correctly pointed out, this is a current limitation of the
netCDF implementation. It is being actively investigated for
enhacement.
The workaround is to utilize the fact that netCDF4 formats are a
subset of the HDF5 format and we can utilize the HDF5 functionality
to read the data
Workaround:
Instead of ncdisp, ncread utilize h5disp, h5read
I hope the above workaround solves your issue. If you have any
further queries, please reach out to me.
1 个评论
Anna Weeks
2018-8-21
The ability to write a mapped array section of values into a netCDF variable (i.e the 'NF_PUT_VARM_ type' functionality) would be highly desirable.
Maxime Hervo
2017-3-31
Is there any update for this issue ? I still have the same problem with MATLAB R2016b !
0 个评论
liu jibao
2018-1-31
Is there any update for this issue ? I still have the same problem with MATLAB R2017b !
3 个评论
Natalie Perlin
2019-7-24
Thank you for filing the report, I wonder if anything has been done since then!
I have the same problem with Matlab not being able to read string variables in R2017a, R2018b.
chidi
2023-1-23
I've tried using the h5read and hdf5read function but I'm not getting desired results
For ref. VX= h5read('d20220602.nc','.............../current velocity down')
Output comes out as [15532×1 single] . . .[15532×1 single]
The expected output are water current data and its 56x1 array
How can I resolve this issue
Thanks
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!