在Python3中加载.mat文件

时间:2018-06-27 10:54:18

标签: arrays python-3.x numpy scipy mat-file

我正在尝试将某些.mat文件加载到python中。在为我创建它们时,我无法用其他方式保存它们。

我正在尝试以下操作:

import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio

dat = sio.loadmat(Filename)

当我打印dat时,我得到:

{'__header__': b'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Nov 17 19:32:02 2017',
 '__version__': '1.0',
 '__globals__': [],
 'dat': array([[(array([[0.21798739, 0.19239525, 0.24698377, ..., 0.00362552, 0.00319988,
         0.00410779],
        [0.21927787, 0.19483694, 0.24677833, ..., 0.00520452, 0.00462442,
         0.00585724],
        [0.21614154, 0.1894833 , 0.2465355 , ..., 0.00670623, 0.0058791 ,
         0.00764926],
        ...,
        [0.01327889, 0.00534974, 0.03195079, ..., 0.10357668, 0.04172848,
         0.24921929],
        [0.00961923, 0.00301665, 0.03037653, ..., 0.07904129, 0.02478783,
         0.24960406],
        [0.00991092, 0.00340566, 0.02884204, ..., 0.08588939, 0.02951395,
         0.24994916]]), array([[0.87194958, 0.76958099, 0.98793509, ..., 0.01450209, 0.01279952,
         0.01643114],
        [0.87195011, 0.7702226 , 0.98711333, ..., 0.02069559, 0.0182811 ,
         0.02342897],
        [0.8723006 , 0.77160119, 0.98614199, ..., 0.02706488, 0.02394048,
         0.03059704],
        ...,
        [0.04238178, 0.01405455, 0.12780316, ..., 0.33058206, 0.10962685,
         0.99687717],
        [0.04284481, 0.01510769, 0.12150612, ..., 0.35205592, 0.12413998,
         0.99841624],
        [0.03964367, 0.01362265, 0.11536817, ..., 0.34355757, 0.11805581,
         0.99979663]]), array([[0.22058713],
        [0.22113018],
        [0.2205365 ],
        ...,
        [0.1313167 ],
        [0.07414598],
        [0.08590686]]), array([[0.65419524],
        [0.65456777],
        [0.65523773],
        ...,
        [0.37236851],
        [0.4207808 ],
        [0.40677318]]), array([[-0.04984448],
        [ 0.16728059],
        [ 0.05042577],
        [ 0.10540356],
        [ 0.50931724],
        [ 0.08713192],
        [ 1.34312613],
        [-0.32106699],
        [-0.14944345],
        [ 1.17045371],
        [-0.66518187],
        [-0.18501938]]))]],
       dtype=[('L', 'O'), ('Lreg', 'O'), ('R', 'O'), ('Rreg', 'O'), ('sol', 'O')])}

但是我需要的是LRLregRregsol作为numpy数组。

如何将数组与变量关联并提取所需的数据?

1 个答案:

答案 0 :(得分:0)

dat是字典。 dat['dat']是一个数组。

L = dat['dat']['L']

是该数组的字段。

然后检查Ldtype的形状。您可能需要另一层索引才能获取数字数组,例如

L[0,0]
L.item()

Accessing array contents inside .mat file in python