Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab data file to pandas DataFrame [duplicate]

Tags:

Is there a standard way to convert matlab .mat (matlab formated data) files to Panda DataFrame?

I am aware that a workaround is possible by using scipy.io but I am wondering whether there is a straightforward way to do it.

like image 906
Heberto Mayorquin Avatar asked Jul 05 '16 07:07

Heberto Mayorquin


People also ask

How do I convert a pandas Dataframe to a MATLAB table?

Sign in to answer this question. A simple and effective, but perhaps inelegant, solution is to first save the Pandas DataFrame as a CSV file and then read that data into MATLAB as a table datatype (as long as you have MATLAB R2013b or newer).

What is a Dataframe in pandas?

Picture by Tyler Franta, Unsplash. Pandas allows you to import data from a wide range of data sources directly into a dataframe. These can be static files, such as CSV, TSV, fixed width files, Microsoft Excel, JSON, SAS and SPSS files, as well as a range of popular databases, such as MySQL, PostgreSQL and Google BigQuery.

What is Dataframe in MATLAB?

Power of matrices, robustness of tables. Frames is a package that introduces a new kind of data type for Matlab, the DataFrame. Demo: html/framesDemo.html This data type (or class) helps when working with data matrices that are referenced by column and row identifiers (e.g. time series which have variable and observation names).

How to convert a mat file to a data frame?

Since your mat file is a dictionary, you will have to convert your dictionary to series then series you can covert to data frame using pandas. Try this way. Since your mat file is a dictionary, you will have to convert your dictionary to series then series you can covert to data frame using pandas. Try this way.


2 Answers

I found 2 way: scipy or mat4py.

  1. mat4py

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') 

From:

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

  1. Scipy:

Example:

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) 

From:

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

  1. Finally you can use PyHogs but still use scipy:

Reading complex .mat files.

This notebook shows an example of reading a Matlab .mat file, converting the data into a usable dictionary with loops, a simple plot of the data.

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

like image 182
Destrif Avatar answered Sep 20 '22 02:09

Destrif


Ways to do this:
As you mentioned scipy

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

Using the matlab engine:

import matlab.engine eng = matlab.engine.start_matlab() content = eng.load("example.mat",nargout=1) 
like image 36
SerialDev Avatar answered Sep 21 '22 02:09

SerialDev