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

14 visualizaciones (últimos 30 días)
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 comentarios
Nike Dattani
Nike Dattani el 9 de Ag. de 2017
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 el 13 de Ag. de 2017
Editada: per isakson el 13 de Ag. de 2017
"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.

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by