In Octave
>> A = [1,2,3;4,5,6];
>> B = [1,2,3,4];
>> save -hdf5 abc.h5 A B
In Ipython
In [138]: import h5py
In [139]: f = h5py.File('abc.h5')
In [140]: list(f.keys())
Out[140]: ['A', 'B']
In [141]: list(f['A'].keys())
Out[141]: ['type', 'value']
In [142]: f['A']['value']
Out[142]: <HDF5 dataset "value": shape (3, 2), type "<f8">
In [143]: A = f['A']['value'][:]
In [144]: A
Out[144]:
array([[ 1., 4.],
[ 2., 5.],
[ 3., 6.]])
See also links in the sidebar.
Basically it's a matter of finding the desired dataset, and then loading it as described in http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data
https://pypi.python.org/pypi/hdf5storage/0.1.14 - this package has MATLAB MAT v7.3 file support
. I haven't used it yet.
In [550]: import hdf5storage
In [560]: bar = hdf5storage.read(filename='abc.h5')
In [561]: bar
Out[561]:
array([ ([(b'matrix', [[ 1., 4.], [ 2., 5.], [ 3., 6.]])], [(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])])],
dtype=[('A', [('type', 'S7'), ('value', '<f8', (3, 2))], (1,)), ('B', [('type', 'S7'), ('value', '<f8', (4, 1))], (1,))])
So the file has been loaded as a structured array, with shape (1,) and 2 fields, 'A' and 'B' (the 2 variable names). Each in turn has a 'type' and 'value' field.
In [565]: bar['A']['value']
Out[565]:
array([[[[ 1., 4.],
[ 2., 5.],
[ 3., 6.]]]])
Or using its loadmat
:
In [570]: out = hdf5storage.loadmat('abc.h5',appendmat=False)
In [571]: out
Out[571]:
{'A': array([(b'matrix', [[ 1., 4.], [ 2., 5.], [ 3., 6.]])],
dtype=[('type', 'S7'), ('value', '<f8', (3, 2))]),
'B': array([(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])],
dtype=[('type', 'S7'), ('value', '<f8', (4, 1))])}
out
is a dictionary:
In [572]: out['B']['value']
Out[572]:
array([[[ 1.],
[ 2.],
[ 3.],
[ 4.]]])
For reading a simple MATLAB file this doesn't add much. It may add more with cells or structs. But for writing a MATLAB compatible file it should be a big help (though for writing one could stick with scipy.io.savemat
).