我正在尝试读取同事创建的MATLAB文件。我正在使用Python 3.7和H5PY,以将数据转换为com trade格式。来自MATLAB的数据在附带的屏幕截图中:次数MATALAB屏幕快照
我需要能够访问存储的时间序列数据,以将其放入Numpy数组中。我花了一天的时间在这里和这里回顾各种技巧和技巧,但似乎被卡住了。我似乎无法解决数据,甚至无法找到信号。我已经有一个示例到一个简单的文件,该文件应该具有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:UsersPerryDesktoptestingMatlab'
... 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 FilesPython37libsite-packagesh5py_hldataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:Program FilesPython37libsite-packagesh5py_hldataset.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 FilesPython37libsite-packagesh5py_hldataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:Program FilesPython37libsite-packagesh5py_hldataset.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 FilesPython37libsite-packagesh5py_hldataset.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 FilesPython37libsite-packagesh5py_hldataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:Program FilesPython37libsite-packagesh5py_hldataset.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 FilesJetBrainsPyCharm Community Edition 2018.3.2helperspydev_pydevd_bundlepydevd_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 FilesPython37libsite-packagesh5py_hlgroup.py", line 262, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "C:Program FilesPython37libsite-packagesh5py_hlbase.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
文件中的所有时间表数据转换为包含来自时间表的时间和数据值的二维数组,然后将其保存到另一个*.mat
文件中。完成此操作并像上述一样加载文件,trf.value
将显示包含数据的数组。如果要继续使用H5PY,请记住使用" -v7.3"选项保存*.mat
文件。