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
    评论
这个问题可能是由于C++程序没有正确解析h5py存储的mat文件导致的。h5py是一种Python库,用于读写HDF5文件,并且支持Python中的Numpy数组。因此,如果你想在C++中读取由h5py存储的Numpy数组,你需要使用HDF5 C++库,并且需要了解Numpy数组在HDF5文件中的存储结构。 在C++中,你需要使用HDF5 C++库中的H5File类打开HDF5文件,并使用H5DataSet类读取数据集。然后你可以使用read()方法数据集中读取数据,并将其存储到一个C++变量中。需要注意的是,HDF5 C++库中的数据类型与Numpy数据类型不完全相同,因此你需要使用H5T类来转换数据类型。 总体来说,你需要了解以下几个步骤: 1. 使用H5File类打开HDF5文件 2. 使用H5DataSet类读取数据集 3. 使用H5T类转换数据类型(如果需要) 4. 使用read()方法读取数据,并将其存储到C++变量中 下面是一个简单的示例代码,可以帮助你开始使用HDF5 C++库读取HDF5文件中的数据: ```c++ #include <iostream> #include <string> #include "H5Cpp.h" // Define the name of the file and the dataset const H5std_string FILE_NAME("example.h5"); const H5std_string DATASET_NAME("my_dataset"); int main() { try { // Open the file and the dataset H5::H5File file(FILE_NAME, H5F_ACC_RDONLY); H5::DataSet dataset = file.openDataSet(DATASET_NAME); // Get the data type of the dataset H5::DataType datatype = dataset.getDataType(); // Get the dataspace of the dataset H5::DataSpace dataspace = dataset.getSpace(); // Get the number of dimensions in the dataset int rank = dataspace.getSimpleExtentNdims(); // Get the dimensions of the dataset hsize_t dims[rank]; dataspace.getSimpleExtentDims(dims, NULL); // Create a buffer to store the data int data[dims[0]][dims[1]]; // Read the data from the dataset dataset.read(data, datatype); // Print the data to the console for (int i = 0; i < dims[0]; i++) { for (int j = 0; j < dims[1]; j++) { std::cout << data[i][j] << " "; } std::cout << std::endl; } // Close the dataset and the file dataset.close(); file.close(); } catch (H5::Exception& e) { std::cerr << "Error reading HDF5 file: " << e.getCDetailMsg() << std::endl; return -1; } return 0; } ``` 需要注意的是,在上面的示例代码中,我们假设数据集的数据类型为int,数据集的维度为2。如果你的数据集的数据类型和维度不同,你需要相应地修改代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值