首页 > 解决方案 > 在 Python3 中加载 .mat 文件

问题描述

我正在尝试将一些 .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')])}

但我需要的是L, R, Lreg,Rregsolnumpy 数组。

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

标签: arrayspython-3.xnumpyscipymat-file

解决方案


dat是一本字典。 dat['dat']是一个结构阵列。

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

是该数组的一个字段。

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

L[0,0]
L.item()

在python中访问.mat文件中的数组内容


推荐阅读