我有一个.mat文件,其中包含两个字符串格式的DateTime数组。数组类似于:
A = ["15-Nov-2014 22:42:16",
"16-Dec-2014 04:14:07",
"20-Jan-2015 17:05:32"]我将两个字符串数组保存在一个.mat文件中。我尝试用以下命令在Python中加载它们:
import hdf5storage
Input = hdf5storage.loadmat('Input.mat')或者这个命令:
import scipy
Input = scipy.io.loadmat('Input.mat')这两种方法都会导致在Python中阅读字典,这是预期的,但我不能将这两个数组的名称作为字典键。
有什么想法吗?
发布于 2020-01-07 20:59:23
我建议您将字符串转换为字符数组。
显然,没有从HDF5存储中读取MATLAB字符串的文档化解决方案(MATLAB字符串是对象,具有无文档的内部存储格式)。
用MATLAB将字符数组保存到Input.mat (而不是HDF5格式):
A = ["15-Nov-2014 22:42:16"; "16-Dec-2014 04:14:07"; "20-Jan-2015 17:05:32"];
% Convert A from array of strings to 2D character array.
% Remark: all strings must be the same length
A = char(A); % 3*20 char array
% Save A to mat file (format is not HDF5).
save('Input.mat', 'A');使用A阅读Python中的scipy.io.loadmat:
from scipy import io
# Read mat file
Input = io.loadmat('Input.mat') # Input is a dictioanry {'A': array(['15-Nov-2014 ...pe='<U20'), ...}
# Get A from Input (A stored in MATLAB - character arrays in MATLAB are in type utf-16)
A = Input['A']; # A is 2D numpy array of type '<U20' array(['15-Nov-2014 22:42:16', '16-Dec-2014 04:14:07', ...], dtype='<U20')https://stackoverflow.com/questions/59635318
复制相似问题