Matlab-read.mat 7.3 timeseries数组(Python中带有h5py)
我正在读一个同事创建的matlab文件。我使用Python3.7和h5py将数据转换为comtrade格式。来自matlab的数据见所附屏幕截图: 我需要能够访问存储的时间序列数据,以便将其放入numpy数组中。我花了一天时间复习了各种技巧,但似乎被卡住了。我似乎无法处理数据,甚至无法找到信号。我得到了一个简单文件的示例,该文件应该有3个信号,如上图所示,我正在尝试提取数据:Matlab-read.mat 7.3 timeseries数组(Python中带有h5py),python,matlab,Python,Matlab,我正在读一个同事创建的matlab文件。我使用Python3.7和h5py将数据转换为comtrade格式。来自matlab的数据见所附屏幕截图: 我需要能够访问存储的时间序列数据,以便将其放入numpy数组中。我花了一天时间复习了各种技巧,但似乎被卡住了。我似乎无法处理数据,甚至无法找到信号。我得到了一个简单文件的示例,该文件应该有3个信号,如上图所示,我正在尝试提取数据: Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28)
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
win32上的Python 3.7.2(tags/v3.7.2:9A3FFC049220018年12月23日23:09:28)[MSC v.1916 64位(AMD64)]
>>>进口h5py作为h5
... mat_dir=r'C:\Users\Perry\Desktop\testing\Matlab'
... 文件\u name=r'\threePhaseSignal.mat'
... f=h5.File(mat_dir+文件名'r')
>>>列表(f.keys())
['#参考#','#子系统#','tfrOut']
>>>tfr=f['tfrOut']
>>>tfr[‘信号’]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“h5py\\u objects.pyx”,第54行,在h5py.\u objects.with\u phil.wrapper中
文件“h5py\\u objects.pyx”,第55行,在h5py.\u objects.with\u phil.wrapper中
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第506行,在\uuu getitem中__
new\u dtype=readtime\u dtype(self.id.dtype,名称)
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第48行,在readtime\u数据类型中
raise VALUERROR(“仅允许复合类型使用字段名”)
ValueError:字段名称仅允许用于复合类型
>>>tfr[“时间”]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“h5py\\u objects.pyx”,第54行,在h5py.\u objects.with\u phil.wrapper中
文件“h5py\\u objects.pyx”,第55行,在h5py.\u objects.with\u phil.wrapper中
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第506行,在\uuu getitem中__
new\u dtype=readtime\u dtype(self.id.dtype,名称)
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第48行,在readtime\u数据类型中
raise VALUERROR(“仅允许复合类型使用字段名”)
ValueError:字段名称仅允许用于复合类型
>>>tfr.dtype
数据类型('uint32')
>>>tfr.ref
>>>tfr值
C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py:313:H5pyDeprecationWarning:dataset.value已被弃用。改为使用数据集[()]。
“改为使用数据集[()”,H5pyDeprecationWarning)
数组([[3707764736,2,1,1,1,
5] ],dtype=uint32)
>>>tfr[0,0]
3707764736
>>>tfr['数据:1'][0,0]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“h5py\\u objects.pyx”,第54行,在h5py.\u objects.with\u phil.wrapper中
文件“h5py\\u objects.pyx”,第55行,在h5py.\u objects.with\u phil.wrapper中
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第506行,在\uuu getitem中__
new\u dtype=readtime\u dtype(self.id.dtype,名称)
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\dataset.py”,第48行,在readtime\u数据类型中
raise VALUERROR(“仅允许复合类型使用字段名”)
ValueError:字段名称仅允许用于复合类型
很明显,tfr是一个对象引用,但我似乎对此无能为力。有人知道我如何使用它来实际处理timeseries数据吗?或者找到我的信号
我也试过:
>>> 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)
tfr中元素的>>:
... 数据=f[元素][0][:]
...
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm社区版2018.3.2\helpers\pydev\\u pydevd\u bundle\pydevd\u exec2.py”,第3行,在Exec中
执行官(exp、全局变量、本地变量)
文件“”,第2行,在
文件“h5py\\u objects.pyx”,第54行,在h5py.\u objects.with\u phil.wrapper中
文件“h5py\\u objects.pyx”,第55行,在h5py.\u objects.with\u phil.wrapper中
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\group.py”,第262行,在\uuu getitem中__
oid=h5o.open(self.id,self.\u e(名称),lapl=self.\u lapl)
文件“C:\Program Files\Python37\lib\site packages\h5py\\u hl\base.py”,第137行,在
name=name.encode('ascii')
AttributeError:'numpy.ndarray'对象没有属性'encode'
>>>元素
数组([3707764736,2,1,1,1,
5] ,dtype=uint32)
这不是一个解决方案,而是一个解决方案,因为我正在努力解决同一个问题。您可以将*.mat
文件中的所有时间序列数据转换为包含时间序列中的时间和数据值的二维数组,然后将其保存到另一个*.mat
文件中。一旦完成并像上面那样加载文件,trf.value
将显示包含数据的数组。如果您想继续使用h5py,请记住使用“-v7.3”选项保存*.mat
文件。如果您有可用的matlab安装,您只需将matlab数组保存到*.npy文件,使用这些文件可以通过将numpy作为np导入到Python中;arr=np.load(“File.npy”),不幸的是我没有。我一直在使用我得到的*.mat文件,我会看看是否可以与我的同事一起调整输出。我假设您正在matlab中转换为二维数组,因为我无法从python访问timeseries数据。是的,抱歉,我不清楚。我在Matlab中做了二维数组的转换。