Python几种读取mat格式数据的方法

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


  • 17
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值