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