【问题标题】:How to access fields in a struct imported from a .mat file using loadmat in Python?如何在 Python 中使用 loadmat 访问从 .mat 文件导入的结构中的字段?
【发布时间】:2010-12-31 09:07:29
【问题描述】:

this question 询问(并回答)如何读取使用 Scipy 在 Matlab 中创建的 .mat 文件之后,我想知道如何访问导入结构中的字段。

我在 Matlab 中有一个文件,可以从中导入结构:

>> load bla % imports a struct called G
>> G

G = 

         Inp: [40x40x2016 uint8]
         Tgt: [8x2016 double]
         Ltr: [1x2016 double]
    Relevant: [1 2 3 4 5 6 7 8]

现在我想在 Python 中做同样的事情:

x = scipy.io.loadmat('bla.mat')
>>> x
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Jun 07 21:17:24 2006', 'G': array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object), '__globals__': []}
>>> x['G']
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
>>> G = x['G']
>>> G
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)

问题是,如何访问结构 G 的成员:InpTgtLtrRelevant,就像在 Matlab 中那样?

【问题讨论】:

标签: python matlab file-io scipy mat-file


【解决方案1】:

首先,如果可能,我建议升级到 Scipy svn - matlab io 一直在积极开发,最近有一些非常显着的加速。

如前所述,struct_as_record=True 可能值得一试。但除此之外,您应该能够通过互动玩弄它。

您的 G 是一组 mio 结构对象 - 例如,您可以检查 G.shape。在这种情况下,我认为G = x['G'][0,0] 应该提供您想要的对象。那么你应该可以访问G.Inp等。

【讨论】:

  • 这对我也有用,但是我必须将它作为下标/字典来访问,即 x['G'][0, 0]['Inp']
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 2017-12-25
  • 2017-07-28
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多