我正在尝试使用scipy.io将文本字符串列表从Python导出到MATLAB。我想使用scipy.io,因为我想要的.mat文件应包括两个数值矩阵(我学会了 这里)和文本单元格数组。

我试过了:

import scipy.io
my_list = ['abc', 'def', 'ghi']
scipy.io.savemat('test.mat', mdict={'my_list': my_list})

在MATLAB中,我加载test.mat并获得一个字符数组:

my_list =

adg
beh
cfi

如何使Scipy.io导出列表中的MATLAB单元格数组?

有帮助吗?

解决方案

您需要使my_list成为一个numpy对象的数组:

import scipy.io
import numpy as np
my_list = np.zeros((3,), dtype=np.object)
my_list[:] = ['abc', 'def', 'ghi']
scipy.io.savemat('test.mat', mdict={'my_list': my_list})

然后它将以单元格式保存。可能有一种更好的方法将其放入np.Object中,但是我从 Scipy文档.

其他提示

看来列表的内容已正确导出,它们只是将其移动并放置在字符数组中。您可以通过转移并使用它来轻松地将其转换为MATLAB中所需的单元字符串阵列 Cellstr, ,将每一行放在一个单独的单元格中:

>> my_list = ['adg';'beh';'cfi'];  %# Your example
>> my_list = cellstr(my_list')    %'# A 3-by-1 cell array of strings

my_list = 

    'abc'
    'def'
    'ghi'

当然,这不会解决更多 一般的 将数据导出作为从Python到MATLAB的单元格数组的问题,但应有助于 具体的 您上面列出的问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top