如何将 Python 1D、2D 或 3D Numpy 数组保存到 MATLAB .mat 中

How to save Python 1D, 2D or 3D NumpPy array into MATLAB .mat

Python 的 SciPy 包有一个函数可以将 Python 变量保存到 MATLAB 的 .mat 文件中 https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html 但是,文档缺少示例,我不知道它想要作为输入的变量字典是什么。

假设我有一个 2D NumPy 数组 A 和一个 3D NumPy 数组 B,如何将它们保存到名为 my_arrays.mat.mat 文件中?

我可以在 .mat 中保存多个变量还是每个变量都需要一个文件?

如何将 .mat 文件读入 Python 以及我如何知道如何访问已加载的变量?

保存和加载示例

import scipy.io as sio

# dummy data
A = np.random.randint(0,10,100)
B = np.random.randint(0,10,100)

# collect arrays in dictionary
savedict = {
    'A' : A,
    'B' : B,
}

# save to disk
sio.savemat('my_arrays.mat', savedict)

#load from disk
data = sio.loadmat('my_arrays.mat')

# alternative way to load from disk:
data = {}
sio.loadmat('my_arrays.mat', mdict=data)