Matlab-使用h5py在Python中读取.mat 7.3时间序列数组

时间:2019-03-08 12:13:49

标签: python matlab

我正在尝试读取由同事创建的matlab文件。我正在使用python 3.7和h5py来将数据转换为商品格式。来自matlab的数据位于所附的屏幕截图中:Timeseries Matalab Screenshot

我需要能够访问存储的时间序列数据以将其放入numpy数组中。我花了一整天的时间回顾了herehere的各种技巧和窍门,但是似乎有些卡住了。我似乎无法处理数据甚至找不到信号。我已经得到一个简单的文件样本,该文件应具有3个信号,如上所示,我正在尝试提取数据:

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
>>> import h5py as h5
... mat_dir = r'C:\Users\Perry\Desktop\testing\Matlab'
... file_name = r'\threePhaseSignal.mat'
... f = h5.File(mat_dir + file_name, 'r')
>>> list(f.keys())
['#refs#', '#subsystem#', 'tfrOut']
>>> tfr = f['tfrOut']
>>> tfr['signals']
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
    new_dtype = readtime_dtype(self.id.dtype, names)
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
    raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types
>>> tfr['Time']
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
    new_dtype = readtime_dtype(self.id.dtype, names)
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
    raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types
>>> tfr.dtype
dtype('uint32')
>>> tfr.ref
<HDF5 object reference>
>>> tfr.value
C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py:313: H5pyDeprecationWarning: dataset.value has been deprecated. Use dataset[()] instead.
  "Use dataset[()] instead.", H5pyDeprecationWarning)
array([[3707764736,          2,          1,          1,          1,
                 5]], dtype=uint32)
>>> tfr[0,0]
3707764736
>>> tfr['Data:1'][0,0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
    new_dtype = readtime_dtype(self.id.dtype, names)
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
    raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types

很显然,tfr是一个对象引用,但我似乎对此无能为力。有谁知道我如何使用它来实际处理时间序列数据?甚至找不到我的信号?

我也尝试过这个:

>>> for element in tfr:
...     data = f[element][0][:]
...     
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.2\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 2, in <module>
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\group.py", line 262, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\base.py", line 137, in _e
    name = name.encode('ascii')
AttributeError: 'numpy.ndarray' object has no attribute 'encode'
>>> element
array([3707764736,          2,          1,          1,          1,
                5], dtype=uint32)

1 个答案:

答案 0 :(得分:0)

这不是解决方案,而是更多的解决方法,因为我正在努力解决同一问题。您可以将*.mat文件中的所有TimeSeries数据转换为包含TimeSeries中的Time和Data值的二维数组,然后将其保存到另一个*.mat文件中。完成此操作并像上面一样加载文件后,trf.value将显示包含数据的数组。如果您要继续使用h5py,请记住使用'-v7.3'选项保存*.mat文件。