简体   繁体   English

将Matlab数据导出到python

[英]export matlab data to python

I have a sentence ' i am unhappy' stored in variable 'a' in matlab. 我有一个句子“我不高兴”存储在matlab的变量“ a”中。 i want to extract data from matlab to python. 我想从Matlab提取数据到python。 like if i write 'a' in python, it should display the whole sentence stored in MATLAB. 就像我在python中写“ a”一样,它应该显示存储在MATLAB中的整个句子。 and yes further i want to convert that sentence into speech using gtts. 是的,我想进一步使用gtts将该句子转换为语音。

i have tried,this: 我试过了,这:

from scipy.io import loadmat
test = loadmat('value.mat')

but it gives error: 但它给出了错误:

MatReadWarning: Duplicate variable name "None" in stream - replacing 
previous with new Consider mio5.varmats_from_mat to split file into 
single variable files matfile_dict = MR.get_variables(variable_names)

I could test it like this and had no problems: 我可以这样测试,没有问题:

In matlab: 在Matlab中:

>> a = ' hello mat file'

a =

 hello mat file

>> filename = 'test.mat'

filename =

test.mat

>> save(filename)

and then in python: 然后在python中:

import scipy.io as scio

data = scio.loadmat('test.mat')

data
Out:
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Tue Nov 27 10:20:58 2018',
 '__version__': '1.0',
 '__globals__': [],
 'a': array([' hello mat file'], dtype='<U15'),
 'filename': array(['test.mat'], dtype='<U8')}

data['a']
Out: array([' hello mat file'], dtype='<U15')

So no error message here. 所以这里没有错误信息。

However, note that in Matlab save by default saves all content of the current workspace in the mat-file. 但是请注意,在Matlab save默认保存在垫子文件中的当前工作区的所有内容。 Could it be possible, that you have created some other variables which perhaps are of an unsupported type? 您是否有可能创建了其他一些类型可能不受支持的变量?

You could check by simply explicitly saving only your test variable a : 您可以通过仅显式保存测试变量a来进行检查:

save(filename, 'a')

Does this lead to an importable mat-file in python? 这会导致在python中导入可导入的mat文件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM