Python 到 MATLAB : exporting list of strings using scipy. io

标签 python string matlab scipy mat-file

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

我试过:

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 documentation 中采用了这种方式.

关于Python 到 MATLAB : exporting list of strings using scipy. io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2433924/

相关文章:

c++ - 对于计算密集型函数,类函数参数是否比 C++ 中的类变量更好?

python - 如何在 Simulink 的 MATLAB 函数中索引数组值?

java - 查找导入的变量来自哪里

C 正则表达式如何匹配任何以“或任何空字符串结尾的字符串?

c++ - 麻省理工学院球体模拟设置中的 Matlab 错误

MATLAB parfor 切片问题?

python - 如何在软件中可靠地生成以太网帧错误?

返回 AttributeError : __enter__ 的 Python Json

python - Tox 在安装模块时无法复制非 python 文件

string - PowerShell 中的多行字符串到单行字符串的转换