代码之家  ›  专栏  ›  技术社区  ›  Nathan Fellman

如何在Python中使用loadmat访问从.mat文件导入的结构中的字段?

  •  7
  • Nathan Fellman  · 技术社区  · 16 年前

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

    >> 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的成员: Inp , Tgt Ltr Relevant ,我在Matlab中所能做到的?

    1 回复  |  直到 9 年前
        1
  •  6
  •   robince    16 年前

    首先,如果可能的话,我建议升级到Scipy svn——最近matlab io的开发非常活跃,速度有了很大的提高。

    同样如前所述,它可能值得一试 struct_as_record=True

    你的G是一个mio结构对象数组-你可以检查 G.shape 例如在这种情况下,我认为 G = x['G'][0,0] G.Inp