2016-07-05 29 views
7

Có cách nào tiêu chuẩn để chuyển đổi MATLAB.mat tệp (dữ liệu đã được định dạng MATLAB) vào Panda DataFrame?tệp dữ liệu MATLAB vào gấu trúc DataFrame

Tôi biết rằng có thể giải quyết vấn đề bằng cách sử dụng scipy.io nhưng tôi tự hỏi liệu có cách đơn giản để thực hiện điều đó không.

Trả lời

8

Tôi đã tìm thấy 2 cách: scipy hoặc mat4py.

  1. mat4py

tải dữ liệu từ MAT-file

Chức năng loadmat tải tất cả các biến được lưu trữ trong MAT-file thành một cấu trúc dữ liệu Python đơn giản, chỉ sử dụng dict Python và liệt kê các đối tượng . Các mảng số và ô được chuyển đổi thành các danh sách được xếp theo thứ tự được liệt kê theo hàng . Mảng được ép để loại bỏ mảng chỉ với một phần tử. Cấu trúc dữ liệu kết quả bao gồm các loại đơn giản là tương thích với định dạng JSON.

Ví dụ: Load một MAT-file thành một cấu trúc dữ liệu Python:

data = loadmat('datafile.mat') 

Từ:

https://pypi.python.org/pypi/mat4py/0.1.0

  1. scipy:

Ví dụ:

import numpy as np 
from scipy.io import loadmat # this is the SciPy module that loads mat-files 
import matplotlib.pyplot as plt 
from datetime import datetime, date, time 
import pandas as pd 

mat = loadmat('measured_data.mat') # load mat-file 
mdata = mat['measuredData'] # variable in mat file 
mdtype = mdata.dtype # dtypes of structures are "unsized objects" 
# * SciPy reads in structures as structured NumPy arrays of dtype object 
# * The size of the array is the size of the structure array, not the number 
# elements in any particular field. The shape defaults to 2-dimensional. 
# * For convenience make a dictionary of the data using the names from dtypes 
# * Since the structure has only one element, but is 2-D, index it at [0, 0] 
ndata = {n: mdata[n][0, 0] for n in mdtype.names} 
# Reconstruct the columns of the data table from just the time series 
# Use the number of intervals to test if a field is a column or metadata 
columns = [n for n, v in ndata.iteritems() if v.size == ndata['numIntervals']] 
# now make a data frame, setting the time stamps as the index 
df = pd.DataFrame(np.concatenate([ndata[c] for c in columns], axis=1), 
        index=[datetime(*ts) for ts in ndata['timestamps']], 
        columns=columns) 

Từ:

http://poquitopicante.blogspot.fr/2014/05/loading-matlab-mat-file-into-pandas.html

  1. Cuối cùng, bạn có thể sử dụng PyHogs nhưng vẫn sử dụng scipy:

Reading phức tạp .mat tập tin.

máy tính xách tay này cho thấy một ví dụ về đọc một file .mat Matlab, chuyển đổi dữ liệu vào một cuốn từ điển có thể sử dụng với các vòng, một âm mưu đơn giản của dữ liệu.

http://pyhogs.github.io/reading-mat-files.html

+0

Giải pháp tuyệt vời. Điều này phải được chọn câu trả lời. – Jack

4

cách để làm điều này:
Như bạn nói scipy

import scipy.io as sio 
test = sio.loadmat('test.mat') 

Sử dụng matlab engine:

import matlab.engine 
eng = matlab.engine.start_matlab() 
content = eng.load("example.mat",nargout=1) 
Các vấn đề liên quan