python open mat file

Record the problems encountered in the process of writing code.

Open a 2D array:

Open the three-dimensional array - 1:
For example,
insert image description here
the code:

#  最初用loadmat读取数据
import numpy as np
from scipy import io

mat = io.loadmat('cscs3.mat')

If an error is reported: NotImplementedError: Please use HDF reader for matlab v7.3 files
to read by the method,

import h5py
import numpy as np

mat = h5py.File('GPM_E_30min_20150105.mat')
# mat文件里可能有多个cell,各对应着一个dataset
# 可以用keys方法查看cell的名字
data1 = mat['E'][:]
print(data1)

print(mat.keys())

print(list(mat.keys()))
# print(list(mat['E'].keys()))

# 可以用values方法查看各个cell的信息
print(mat.values())
# 可以用shape查看维度信息
print(mat['E'].shape)
# 注意,这里看到的shape信息与你在matlab打开的不同
# 这里的矩阵是matlab打开时矩阵的转置
# 所以,我们需要将它转置回来
mat_t = np.transpose(mat['E'])
# mat_t 是numpy.ndarray格式
print(mat_t)
print(mat_t.shape)
# 或则将其存为npy格式文件
# np.save('test.npy', mat_t)

# matrix = np.load('test.npy')

h5py library link: link

Open three-dimensional array - 2:

Guess you like

Origin blog.csdn.net/weixin_44836370/article/details/128118151