简体   繁体   中英

open .mat file with python 2.7

i'm looking for an equivalent to this python 3.6 code :

import scipy.io as sio
file = sio.loadmat('file.mat')
data = file['data']

I have to do the same thing in python 2.7, i tryed

import h5py
f = h5py.File('file.mat')

But it doesn't work, do you have an idea?

Thank you

If you don't want to use scipy, you can use h5py. It support Matlab arrays saved as v3.7 arrays.

import numpy as np
import h5py 

f = h5py.File('file.mat','r') 
data = f.get('data/variable_name') 

# convert to numpy array
data = np.array(data)

Save the data from Matlab like so save('filename', '-v7.3', 'var1');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM