Python 到 Mat 文件:将字符串列表导出到普通字符矩阵(不是元胞数组!)
Python 上的这段代码在 .mat 文件中创建单元格“STRINGS”:
data = {"STRINGS": numpy.empty((0),dtype=numpy.object)}
data["STRINGS"] = numpy.append( data["STRINGS"], "Some string" )
scipy.io.savemat( output_mat_file, data )
在 matlab 中,我得到单元格字符串:
>> STRINGS{1}
ans =
Some string
如何获得普通矩阵?例如:
>> strings(1,:) = char('Some ');
>> strings(1,:)
ans =
Some
编辑
如果我运行以下代码,我会误解数组修饰。
Python:
list = ['hello', 'world!!!']
scipy.io.savemat(output_mat_file, mdict={'list':list})
Matlab:
>> list
list =
hlo wrd!
This code on Python creates cell "STRINGS" in .mat-file:
data = {"STRINGS": numpy.empty((0),dtype=numpy.object)}
data["STRINGS"] = numpy.append( data["STRINGS"], "Some string" )
scipy.io.savemat( output_mat_file, data )
In matlab I get cell STRINGS:
>> STRINGS{1}
ans =
Some string
How could I get ordinary matrix? For instance:
>> strings(1,:) = char('Some ');
>> strings(1,:)
ans =
Some
EDIT
If I run following code, I'll get misunderstood array mangling.
Python:
list = ['hello', 'world!!!']
scipy.io.savemat(output_mat_file, mdict={'list':list})
Matlab:
>> list
list =
hlo wrd!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MATLAB 中,元胞数组是异构数据类型的容器,而矩阵不是,并且它们的所有元素必须属于同一类型(无论是双精度数字还是字符)。
矩阵是矩形形状(因此,如果您在每个 2D 矩阵中存储字符串)行,它们都必须具有相同的长度,或用空格填充)。这个概念也适用于多维矩阵。
Python 列表的 MATLAB 等效项是元胞数组:
Python
MALTAB
编辑:
这是一个显示差异的示例:
Python
MATLAB
现在我们可以按以下方式访问字符串:
请注意,在矩阵情况下,字符串是用空格填充的,以便所有字符串具有相同的长度(您可以使用 STRTRIM)
In MATLAB, cell arrays are containers for heterogeneous data types, while matrices are not, and all their elements must be of the same type (be it numeric doubles or characters)
Matrices are of rectangular shapes (thus if you store strings in each 2D matrix row, they all must be of the same length, or padded with spaces). This notion applies to multi-dimensional matrices as well.
The MATLAB equivalent of Python's lists are cell arrays:
Python
MALTAB
EDIT:
Here is an example to show the difference:
Python
MATLAB
Now we can access the strings as:
Note that in the matrix case, the strings were space-padded so that all strings have the same length (you could use STRTRIM)