Modify contents of HDF5 file but keep it readable by external programs

13 次查看(过去 30 天)
A FORTRAN/C++ program uses an input file with extension *.h5. I want to change the contents of one array in this h5 file to single precision and re-run the FORTRAN/C++ program. I've taken the following steps in MATLAB/Octave:
p=load('data.h5');
p.second.values=cast(p.second.values,'single');
save('data.h5','p');
The FORTRAN/C++ program crashes when looking for the data in the data.h5 produced by MATLAB in this way. I came to find that p=load('data.h5') does not store the data in p.second.values, but in p.p.second.values.
I just want the h5 file read by the FORTRAN/C++ program to be the exact same as before, except with second.values to be an array of single-precision numbers rather than double. Is there an easy way?
  2 个评论
Nike Dattani
Nike Dattani 2017-8-9
I never thought I'd say it was easier to obtain a python solution, but here it is:
import h5py
import numpy as np
FILENAME = 'data.h5'
f = h5py.File(FILENAME, 'a')
f['/second/values'][:] = f['/second/values'].value.astype(np.float32)
The key is the letter 'a' on the 4th line, which means "append" the file, don't just read it. How shall I do it in MATLAB or OCTAVE?
per isakson
per isakson 2017-8-13
编辑:per isakson 2017-8-13
"an input file with extension *.h5" I assume that's a HDF5 file.
Or
save( 'data.h5', 'p', '-v7.3' );
but that creates a HDF5-file with a very special format, which would require a FORTRAN-program that recognizes this format.

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by