如何在Python中使用loadmat访问从.mat文件导入的结构中的字段? [英] How to access fields in a struct imported from a .mat file using loadmat in Python?

查看:1025
本文介绍了如何在Python中使用loadmat访问从.mat文件导入的结构中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循这个问题,询问(和答案)如何读取.mat文件这是使用Scipy在Matlab中创建的,我想知道如何访问导入的结构中的字段。

我有一个Matlab的文件,我可以导入一个结构:

Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs.

I have a file in Matlab from which I can import a struct:

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

>> 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]

Now I want to do the same in Python:

问题是,如何访问结构G的成员: Inp Tgt Ltr 相关,我可以在Matlab中使用吗?
首先,如果可能的话,我建议升级到Scipy svn - 最近有一些真正戏剧化的加速,已经有了matlab io的积极开发。

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)

p>

如前所述,可能值得用 struct_as_record = True 进行尝试。你可以通过交互式地玩弄它。

Also as mentioned it might be worth trying with struct_as_record=True. But otherwise you should be able to get it out by playing around interactively.

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

Your G is an array of mio struct objects - you can check G.shape for example. In this case I think G = x['G'][0,0] should give the object you want. Then you should be able to access G.Inp etc.

这篇关于如何在Python中使用loadmat访问从.mat文件导入的结构中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆