且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用h5py在python中读取HDF5格式的MATLAB文件

更新时间:2022-12-08 13:41:56

在Octave中,我创建了一个包含单元格和矩阵的文件

In Octave I created a file with cell and matrix

>> xmat = [1,2,3;4,5,6;7,8,9];
>> xcell = {1,2,3;4,5,6;7,8,9};
>> save -hdf5 testmat.h5 xmat xcell

ipythonh5py中,我发现此文件包含2个组

In ipython with h5py, I find that this file contains 2 groups

In [283]: F = h5py.File('../testmat.h5','r')
In [284]: list(F.keys())
Out[284]: ['xcell', 'xmat']

矩阵组具有typevalue数据集:

In [285]: F['xmat']
Out[285]: <HDF5 group "/xmat" (2 members)>
In [286]: list(F['xmat'].keys())
Out[286]: ['type', 'value']
In [287]: F['xmat']['type']
Out[287]: <HDF5 dataset "type": shape (), type "|S7">
In [288]: F['xmat']['value']
Out[288]: <HDF5 dataset "value": shape (3, 3), type "<f8">
In [289]: F['xmat']['value'][:]
Out[289]: 
array([[ 1.,  4.,  7.],
       [ 2.,  5.,  8.],
       [ 3.,  6.,  9.]])

单元格具有相同的typevalue,但value是另一个组:

The cell has the same type and value, but value is another group:

In [291]: F['xcell']['type']
Out[291]: <HDF5 dataset "type": shape (), type "|S5">
In [292]: F['xcell']['value']
Out[292]: <HDF5 group "/xcell/value" (10 members)>

In [294]: list(F['xcell']['value'].keys())
Out[294]: ['_0', '_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', 'dims']
...
In [296]: F['xcell']['value']['dims'][:]
Out[296]: array([3, 3])

我必须使用[...]来获取单元格的值,因为它是一个0d数组:

I had to use the [...] to fetch the value of a cell, since it is a 0d array:

In [301]: F['xcell']['value']['_0']['value'][...]
Out[301]: array(1.0)

要真正重复这个问题,我应该创建字符串单元格值,但是我认为这很好地说明了单元格的存储方式-作为数据组内的命名数据集.

To really replicate the question I should have created string cells values, but I think this illustrates well enough how a cells are stored - as named datasets within a data group.

我假设Octave h5存储与MATLAB兼容.

I'm assuming the Octave h5 storage is compatible with MATLAB's.