matlab中使用的数据一般会以mat的格式存储,用python读取有以下几种方法
1、使用scipy,具体实现如下:
import scipy.io as scio import pandas as pd data_path="train.mat" #Method 1 data = scio.loadmat(data_path) data_train_label=data_train.get('label')#取出字典里的label data_train_data=data_train.get('data')#取出字典里的data
可以参考以下链接:/doc/scipy/reference/io.html
2、mat4py库:
功能: 将Matlab 数据导入为基本的Python数据类型。矩阵是以行为组的存储方式(使用列表的列表)。 Matlab结构体Struct和元胞Cell 使用Python的词典表示。
import mat4pystudent1 = mat4py.loadmat('student.mat')student1 = student1['student']print type(student1) #dict print ','.join(['%s' % key for key,val in student1.iteritems()]) # age,score,name,sex
Load data from MAT-fileThe function loadmat loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict and list objects. Numeric and cell arrays are converted to row-ordered nested lists. Arrays are squeezed to eliminate arrays with only one element. The resulting data structure is composed of simple types that are compatible with the JSON format.Example: Load a MAT-file into a Python data structure:data = loadmat('datafile.mat')The variable data is a dict with the variables and values contained in the MAT-file.Save Python data structure to a MAT-filePython data can be saved to a MAT-file, with the function savemat. Data has to be structured in the same way as for loadmat, i.e. it should be composed of simple data types, like dict, list, str, int and float.Example: Save a Python data structure to a MAT-file:savemat('datafile.mat', data)
链接:/pypi/mat4py/0.4.0
3、h5py
from pandas import Series,DataFrameimport pandas as pdimport numpy as npimport h5pydatapath = 'data10.mat'file = h5py.File(datapath,'r')def Print(name):print(name)data = file['CH01'][:]dfdata = pd.DataFrame(data)datapath1 = 'data3.txt'dfdata.to_csv(datapath1)
注意:可能会出现打不开文件问题,可以参考有关链接:/en/latest/quick.html