python--reading matlab data Files *.mat

Source: Internet
Author: User

in view of the future goal is mainly to take advantage of existing MATLAB data (. Mat Or. txt), mainly consider python import matlab data problem. The following code resolves a problem with Python reading. mat Files. The main use of Sicpy.io can Be. Sicpy.io provides two functions Loadmat and savemat, very convenient. # adapted from http://blog.csdn.net/rumswell/article/details/8545087import Scipy.io as Sio#import Matplotlib.pyplot as Pltfrom Pylab Import *import NumPy as NP  matfn= ' E:\\pythonrun\\myuse\\matdata.mat ' # The path of. mat dataData=sio.loadmat (matfn)  xx=data[' matdata ']figure (1)plot (xx)Show () the following code (http://blog.csdn.net/gadfly7/article/details/9906315) is read into the TXT data and converted to an array, the method is more stupid, more effective methods to be studied. from numpy Import * def file2list (filename):FR = Open (filename)array = fr.readlines () #以文件中的每行为一个元素 to form a list of listsnum = Len (array)Returnmat = Zeros ((num,3)) #初始化元素为0的, The number of line numbers list, where each element is still a list, the number of elements is 3, in this expression matrixindex = 0For line in array:line = Line.strip () #去掉一行后的回车符号linelist = line.split (') #将一行根据分割符, divided into multiple elements of the listreturnmat[index,:] = linelist[0:3] #向矩阵赋值, Note that this assignment is clumsyIndex +=1return Returnmat fname = ' E:\\pythonrun\\myuse\\num_data.txt 'data= file2list (fname)

python--reading matlab data Files *.mat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.