【问题标题】:Strange array from .mat file. How to access the data?.mat 文件中的奇怪数组。如何访问数据?
【发布时间】:2021-03-04 14:38:11
【问题描述】:

我正在使用 Jupyter Lab!

我使用 hdf5storage 打开了一个“.mat”文件。该文件是由操作数据采集仪器的软件生成的(Rowe 的 ADCP,谁可能会问!海洋学的东西......)

我可以访问键并从除一个之外的所有键中提取数据...“Gps”包含纬度和经度数据,假设在两列和多行数组中...但它的形状是 (1x1),当我打印它时,我可以看到数据,但不知道如何访问它!

如何访问这些数据?

文件在这里....

https://drive.google.com/file/d/15JEA5y5_Zt52FpPa--Cp_wwl2TYrnlQZ/view?usp=sharing

这是笔记本(适合屏幕),但你可以明白我的意思。

【问题讨论】:

  • 使用索引x[0,0]x.dtype 是什么?
  • 形状为 (1,1),显示为 [[,索引处理了这一点。但是在array 前面添加了一个(,表示object dtype 层或结构化数组。 x[0,0].item() 应该负责对象层。
  • x.dtype = [('lat', 'O'), ('lon', 'O')]
  • 我使用了 x = f['Gps'][0,0] 并且可以访问数据!谢谢!

标签: arrays python-3.x numpy numpy-slicing


【解决方案1】:

看起来像“旧”样式的 MATLAB mat 文件,我可以使用 scipy.io.loadmat 加载它

In [3]: data = io.loadmat('../Downloads/ADP_1911041032.mat')
In [5]: data['__header__']
Out[5]: b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Dec 06 10:01:19 2019'
In [6]: data['Gps']
Out[6]: 
array([[(array([[-32.05298353, -32.0529503 , -32.0528978 , -32.05284081,
                -32.05270699, -32.05265406, -32.05266599, -32.05267098,
                -32.05268485, -32.05271432, -32.0526721 , -32.05260368,
                -32.05254208, -32.05249182, -32.05245293, -32.05241743,
...
                -52.04648597, -52.0463069 , -52.04610979, -52.04591461,
                -52.04569035, -52.04549773, -52.04532165]]))                                                                ]],
      dtype=[('lat', 'O'), ('lon', 'O')])
In [7]: gps=data['Gps']
In [8]: gps.shape
Out[8]: (1, 1)
In [9]: gps.dtype
Out[9]: dtype([('lat', 'O'), ('lon', 'O')])
In [10]: gps[0,0].shape
Out[10]: ()
In [11]: gps[0,0]['lat'].shape
Out[11]: (1, 103)
In [12]: gps[0,0]['lat'].dtype
Out[12]: dtype('<f8')

loadmat 将 MATLAB 单元和结构转换为 numpy 数组对象。这里返回一个 (1,1) 形状的数组(MATLAB 总是 2+d),带有一个复合 dtype,2 个字段

In [13]: gps[0,0]['lat']
Out[13]: 
array([[-32.05298353, -32.0529503 , -32.0528978 , -32.05284081,
        -32.05270699, -32.05265406, -32.05266599, -32.05267098,
        -32.05268485, -32.05271432, -32.0526721 , -32.05260368,
         ...
        -32.0491144 , -32.04907168, -32.04903315]])

gps.item()[0] 返回相同的数组。

基本上,您需要注意shapedtype,并准备逐步通过结构“向下”工作。它有助于理解numpy 数组、结构化以及对象。

【讨论】:

  • 谢谢。奇怪的是,对于其他键,我不需要这样做!只为GPS!?而且我还了解到这个文件是在 Matlab 本身中生成的......并且使用 hdf5storage 做同样的工作(并与其他 Matlab MAT 版本一起工作!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 2018-09-08
  • 1970-01-01
  • 2014-04-22
  • 2022-08-18
  • 2019-03-16
  • 1970-01-01
相关资源
最近更新 更多