MATLAB Answers

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

46 views (last 30 days)
Nike Dattani
Nike Dattani on 8 Aug 2017
Edited: per isakson on 13 Aug 2017
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:
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?


Nike Dattani
Nike Dattani on 9 Aug 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 on 13 Aug 2017
"an input file with extension *.h5" I assume that's a HDF5 file.
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.

Sign in to comment.

Answers (0)

Sign in to answer this question.

Translated by