[英]reading HDF5-format MATLAB file in python with h5py
I am trying to read a HDF5-format MATLAB file in python, using the h5py library. 我正在尝试使用h5py库在python中读取HDF5格式的MATLAB文件。 This file is called "Q_visSDF_accurate.mat" and has two keys: "filename" and "sdf". 该文件称为“ Q_visSDF_accurate.mat”,并具有两个键:“文件名”和“ sdf”。 "filename contains a cell array strings. "sdf" is a [6001, 49380] matrix containing floats. I had no problem to extract the variable sdf using the following code: “文件名包含一个单元格数组字符串。“ sdf”是一个包含浮点数的[6001,49380]矩阵。我可以使用以下代码来提取变量sdf:
import h5py
data = h5py.File("Q_visSDF_accurate.mat", 'r')
sdf = data.get("sdf")[:,:]
sdf = sdf.astype(float)
However, I cant read the filename variable. 但是,我无法读取文件名变量。 I tried: 我试过了:
filename = data.get("filename")[0]
but the code returns: 但是代码返回:
array([<HDF5 object reference>, <HDF5 object reference>,
<HDF5 object reference>, ..., <HDF5 object reference>,
<HDF5 object reference>, <HDF5 object reference>], dtype=object)
I can I de-reference the containt of the filename variable? 我可以取消引用文件名变量的包含吗? Using the hdf5storage package is not a solution, as it works only for python 32 bits and can only read a subset of matlab variables. 使用hdf5storage包不是解决方案,因为它仅适用于python 32位,并且只能读取matlab变量的子集。
In Octave I created a file with cell and matrix 在Octave中,我创建了一个包含单元格和矩阵的文件
>> 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
In ipython
with h5py
, I find that this file contains 2 groups 在ipython
与h5py
,我发现这个文件包含2组
In [283]: F = h5py.File('../testmat.h5','r')
In [284]: list(F.keys())
Out[284]: ['xcell', 'xmat']
The matrix group has a type
and value
dataset: 矩阵组具有type
和value
数据集:
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.]])
The cell has the same type
and value
, but value
is another group: 单元格具有相同的type
和value
,但value
是另一个组:
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])
I had to use the [...]
to fetch the value of a cell, since it is a 0d array: 我不得不使用[...]
来获取一个单元格的值,因为它是一个0d数组:
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. 为了真正重复这个问题,我应该创建字符串单元格值,但是我认为这很好地说明了单元格的存储方式-作为数据组内的命名数据集。
I'm assuming the Octave h5 storage is compatible with MATLAB's. 我假设Octave h5存储与MATLAB兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.