使用 Scipy 将字典从 Python 保存到 Matlab

Saving dictionaries from Python to Matlab with Scipy

我发现将生成的整洁数据保存到 .mat 文件中时遇到了一些问题。我认为使用 Scipy 更直接,但似乎我弄错了。

这是我要保存的数据示例:

out = {'features': array([[  5.00088905e+01,   1.51847522e+01,   4.93513862e+01,
          3.76548415e+00,  -3.96946513e+01,  -2.11885850e+01,
          9.85304035e+00,  -6.30005764e+00,   1.19987435e+01,
          3.89762536e+00,  -1.31554755e+00,  -1.66890836e+01,
          4.75289017e-02,   3.65829480e-01,  -4.77872832e-01,
          1.13641908e+00,  -1.08742775e-01,  -2.42751445e-01,
         -1.13054913e-01,   3.39011561e-01,   1.37158960e-01,
         -2.80760116e-01,  -4.15187861e-01,   9.85433526e-02,
         -8.66144928e-02,   9.18260870e-03,  -7.38139130e-01,
          8.04136232e-01,   2.31623188e-02,  -7.88927536e-02,
         -2.17779710e-01,   2.85428986e-01,  -8.16231884e-02,
          1.79710145e-03,  -3.47710145e-01,  -9.84115942e-02,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01]]), 'tags': [['rock', 'metal']]}

它是矩阵的单行,可以与标签列表相关联(长度可变)。

我们的想法是创建一个包含矩阵和列表元胞数组的 .mat 文件。当我这样做时:

scipy.io.savemat('./test.mat',out)

标签在 Matlab 中的结果各不相同。对于上面的示例,我有一个 1x2x5 字符矩阵

val(:,:,1) =    rm    
val(:,:,2) =    oe    
val(:,:,3) =    ct    
val(:,:,4) =    ka    
val(:,:,5) =     l

如果我尝试使用矩阵而不是单行向量,我会得到一个元胞数组,每行都有一个元胞,但列表已合并,特定行的元胞将为:rmoectkal.

我试着举例说明:

>>> genre_tags_matrix = np.array(genre_tags, dtype=np.object) 
>>> print(genre_tags_matrix) 
[['classical', 'pop'] ['classical'] ['classical'] ['classical']]
>>> out = {'tags' : genre_tags_matrix}
>>> scipy.io.savemat('./test.mat',out)

这是我在 Matlab 中看到的:

到底是怎么回事?有解决办法吗?

问题是 MATLAB 和 Octave 中的字符串实际上只是一个字符数组,所以下面的语句 实际上 一个 3D 数组

[['rock', 'metal']]

如果我们用数字替换字符,使它更清楚一点,它是一个 3D 数组,我们会得到类似这样的东西

[[[1,2,3], [4,5,6]]]

当您使用 savemat 将其中任何一个保存到 .mat 文件时,它将被视为 3D 数组。

如果您想要一个元胞数组,则必须手动创建一个 numpy 对象的 numpy 数组

import scipy
import numpy as np

out = {'tags': np.array(['rock', 'metal'], dtype=np.object)}

scipy.io.savemat('test.mat', out)

然后在 MATLAB 或 Octave 中

data = load('test.mat')
%    tags =
%    {
%      [1,1] = rock
%      [1,2] = metal
%    }

更新

如果是 嵌套 元胞数组,您希望成为元胞数组的每个级别都必须 是一个 numpy numpy 对象数组

out = {'tags': np.array([
            np.array(['classical', 'pop'], dtype=np.object),    # A nested cell array
            'classical', 'classical', 'classical'], dtype=np.object)}