Skip to main content
 首页 » 编程设计

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

2024年11月24日9飞鱼

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

我需要能够访问存储的时间序列数据以将其放入 numpy 数组中。我花了一天时间回顾各种提示和技巧 here和 here但似乎被卡住了。我似乎无法处理数据,甚至无法找到信号。我有一个样本到一个简单的文件,应该有 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) 

请您参考如下方法:

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