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
可以参考以下链接:https://docs.scipy.org/doc/scipy/reference/io.html
2、mat4py库:
功能: 将Matlab 数据导入为基本的Python数据类型。矩阵是以行为组的存储方式(使用列表的列表)。 Matlab结构体Struct和元胞Cell 使用Python的词典表示。
import mat4py
student1 = 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-file
The 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-file
Python 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)
链接:https://pypi.python.org/pypi/mat4py/0.4.0
3、h5py
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
import h5py
datapath = '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)
注意:可能会出现打不开文件问题,可以参考有关链接:http://docs.h5py.org/en/latest/quick.html