基于python读取.mat文件并取出信息
这篇文章主要介绍了基于python读取.mat文件并取出信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
导入所需包
fromscipy.ioimportloadmat
读取.mat文件
随便从下面文件里读取一个:
m=loadmat('H_BETA.mat')#读出来的m是一个dict(字典)数据结构
读出来的m内容:
m:{'__header__':b'MATLAB5.0MAT-file,Platform:GLNXA64,Createdon:MonAug517:14:092019', '__version__':'1.0', '__globals__':[], 'H_BETA':array([[0.68508148,0.36764355,0.73505849,...,0.27600164, 0.67968929,0.70506438], [0.74920812,1.10949748,0.47506305,...,0.32871445, 0.61247345,1.06948844], [0.83311522,1.06321302,0.97364609,...,0.85837753, 0.96296771,1.46095171], ..., [nan,nan,nan,...,nan, nan,-9.04648469], [nan,nan,nan,...,nan, nan,nan], [nan,nan,nan,...,nan,
In[29]:m.keys() Out[29]:dict_keys(['__header__','__version__','__globals__','H_BETA'])
取出.mat里所需信息
.mat文件里的数据结构是dict,所以取值要按照key:value的形式:
In[30]:m['H_BETA'] Out[30]: array([[0.68508148,0.36764355,0.73505849,...,0.27600164, 0.67968929,0.70506438], [0.74920812,1.10949748,0.47506305,...,0.32871445, 0.61247345,1.06948844], [0.83311522,1.06321302,0.97364609,...,0.85837753, 0.96296771,1.46095171], ..., [nan,nan,nan,...,nan, nan,-9.04648469], [nan,nan,nan,...,nan, nan,nan], [nan,nan,nan,...,nan, nan,nan]]) In[31]:type(m['H_BETA']) Out[31]:numpy.ndarray
预处理数据
上面读出来的数据是ndarray类型,为了方便数据的展示,我们可以将其转换为,pandas的DataFrame:
In[32]:importpandasaspd In[33]:df=pd.DataFrame(m['H_BETA']) In[34]:df.head() Out[34]: 12345678910 0.6850810.3676440.7350580.0850460.1043320.5607310.3502190.7581850.3038230.1140220.452877 0.7492081.1094970.4750630.8961001.1177720.6113560.6626690.6030770.8639300.7568700.725808 0.8331151.0632130.9736460.9350610.6316700.9168000.6629930.5432310.6715581.0279540.526402 0.4889060.9327410.9566220.5731160.8937640.9873040.3808071.2111570.5502130.8984081.153289 0.4406940.5032090.5096930.4770540.344717-0.0546621.1242130.3449060.6128980.217625-0.129715 [5rowsx2111columns]
如此,数据就比较规整了,是保存成文件,还是做其他处理,就byyourself啦!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。