Read mat file python pandas

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

Loading Matlab formatted data (.mat) into pandas

There are many instances where we may have to switch between languages while working with data. One of the situations I came across recently was having to deal with data presented in matlab formatted (.mat) file while working in python. Thanks to denizens of the internet this was not much of an issue for me. However, I thought of presenting how to go over the data set if you are ever in this situation.

Firstly, Matlab formatted data refers to data stored in a .mat extension file which is binary data container designed by Matlab. For more information on this data format refer to: Matlab Formatted Data

For this exercise I am going to use loadmat function from scipy library to load the matlab data set and pandas DataFrame to store data in python. Let's say your matlab data is in a file named "data_set.mat".

In order to load data, we use the following lines of code:

from scipy.io import loadmat

data_set = loadmat("data_set.mat")

Now the data is loaded into data_set variable which is essentially a dictionary. The level and complexity of encoding in the dictionary will depend upon the nature of your .mat file. Therefore, it is best to go over the dictionary to understand it.

One of the keys of the dictionary will indicate labels which corresponds to column names in a pandas dataframe. In my file it is called rows. This data will mostly be stored in a list of 2d arrays consisting of column name and data type. There will another key refering to the actual data stored which will be listed in the same order as the column names.

The following code will extract the data and store it in a dictionary

rows = data_set['rows']
dataVal = data_set['data']
data_dict = {}

for i,v in enumerate(rows):
    data_dict[v[0][0]] = dataVal[i]

You must look at the rows variable above before extracting data from it. Once you have the data loaded in the dictionary, you create a pandas dataframe using that dictionary

df = pd.DataFrame(data=data_dict)

Note: This is the simplest way to create dataframe in pandas. You can encode more information in the dataframe using options like index, columns

Reference: https://stackoverflow.com/questions/38197449/matlab-data-file-to-pandas-dataframe

Can we read .MAT file in Python?

Matlab 7.3 and greater Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see in Matlab). These files can be read in Python using, for instance, the PyTables or h5py package.

How do I read a .MAT file?

How to Open an MAT File. MAT files that are Microsoft Access Shortcut files can be created by dragging a table out of Access and to the desktop or into another folder. Microsoft Access needs to be installed in order to use them. MATLAB from MathWorks can open MAT files that are used by that program.

How do you call a .MAT file in Python?

mat file in Python..
Install the package: pip install pymatreader..
Import the relevant function of this package: from pymatreader import read_mat..
Use the function to read the matlab struct: data = read_mat('matlab_struct. mat').
use data. keys() to locate where the data is actually stored..

How do you convert a mat to a data frame?

A matrix can be converted to a dataframe by using a function called as. data. frame().