Reading a mat file into Python

According to a StackOverflow post, one can use scipy to read a matlab file, but it does not support the latest format Version 7.3 and later. Instead, one can use h5py since the format Version 7.3 and later follows HDF5.

Then, I looked for a solution to read a matlab file by using h5py and found another StackOverflow post, which describes how to extract the names of variables and their values. Since I know that Panda‘s DataFrame is good to deal with labelled datasets, I dug Pandas documents. What is possible is to convert a dict to Pandas Series. I think it is because the array for each label can be multi-dimensional.

import numpy as np
import pandas as pd
import h5py

f = h5py.File('FileName','r')

dataset = dict()

for name, data in f.items():
    if type(data) is h5py.Dataset:
        dataset[name] = data.value

series = pd.Series(dataset)

So one can address the content of a variable x by series['x'].

Advertisements

1 thought on “Reading a mat file into Python

  1. Pingback: Reading HDF5 in MATLAB – Jae-Seung's Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s