在python中访问.mat文件中的数组内容 [英] Accessing array contents inside .mat file in python

查看:118
本文介绍了在python中访问.mat文件中的数组内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读.a文件,该文件位于 http://www.eigenvector .com/data/tablets/index.html .要访问此文件中的数据,我尝试以下操作:

I want to read a .mat file available at http://www.eigenvector.com/data/tablets/index.html. To access the data inside this file, I am trying the follwing:

import scipy.io as spio
import numpy as np
import matplotlib.pyplot as plt
mat = spio.loadmat('nir_shootout_2002.mat')

# http://pyhogs.github.io/reading-mat-files.html
def print_mat_nested(d, indent=0, nkeys=0):
    if nkeys>0:
        d = {k: d[k] for k in d.keys()[:nkeys]}
    if isinstance(d, dict):
        for key, value in d.iteritems():
            print '\t' * indent + 'Key: ' + str(key)
            print_mat_nested(value, indent+1)

    if isinstance(d,np.ndarray) and d.dtype.names is not None:
        for n in d.dtype.names:
            print '\t' * indent + 'Field: ' + str(n)
            print_mat_nested(d[n], indent+1)

print_mat_nested(mat, nkeys=1)

以上命令显示字典中的第一个键为"validate_1",并且其字段为"data".要访问此字段,我尝试:

Above command shows that the first key in the dictionary is "validate_1" and it has a field "data". To access this field, I try:

t = mat['validate_1']
print(t['data'])

它打印一个数组,但是当我使用np.shape(t['data'])时,它仅返回(1,1),而数据似乎更大.我不确定如何访问t ['data']内部的数组.

It prints an array but when I use np.shape(t['data']), it just returns (1,1) whereas the data seems to be larger. I am not sure how to access array inside t['data'].

感谢您的帮助.

推荐答案

我发现以下工作有效:

t = mat['validate_1']['data'][0,0]
print(np.shape(t))

返回t是形状为(40,650)的数组.

It returns that t is an array of shape (40,650).

这篇关于在python中访问.mat文件中的数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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