How do I change the data type in an HDF5 file from MATLAB?

£可爱£侵袭症+ 提交于 2019-12-13 14:06:01

问题


I have a HDF5 datafile that has an array of int32 data values. I wish to change the data stored in that array to different values which are of a different format (double).

for example I can query the data type with the following:

 finf=h5info('file.hdf5');
 finf.Datasets(1).Datatype

 ans = 

      Name: ''
      Class: 'H5T_INTEGER'
      Type: 'H5T_STD_I32LE'
      Size: 4
      Attributes: []

If I try to recreate the data in the same node location it gives me the following error that the data set already exists:

t=double(h5read([filepath filename],'/t'));

% more t calculations here....

h5create('file.hdf5','/t',size(t),'DataType','double');

Error using h5create>create_dataset (line 159)
The dataset '/t' already exists.

Error in h5create (line 69)
create_dataset(options);

I've looked in the Matlab docs for a function for deleting a data set in an hdf5 file but can't find any references. Any one have any ideas?


回答1:


It is not possible to either delete a dataset or change its datatype. From section 5.3.2 of the HDF5 manual:

The datatype is set when the dataset is created and can never be changed.

This is due to how space is assigned in an HDF5 file. While it's not possible to delete a dataset (for the same reasons), it can be "unlinked" and be made inaccessible, but this does not reclaim the used space.

If you really need to change the datatype, you have two choices: the first is to unlink the old dataset and create a new one in its place. The new dataset can have the same name as the old one. However, if space is a concern, you may prefer to just create an entirely new HDF5 file, and copy the old data into the new one.




回答2:


According to This post which is a similar problem there is no mechanism for deleting a dataset in an HDF5 file. It also indicates that it is possible to Modify in place.



来源:https://stackoverflow.com/questions/17994790/how-do-i-change-the-data-type-in-an-hdf5-file-from-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!