Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Forked from iaalm/convert-mat.py
Created June 14, 2021 22:02
Show Gist options
  • Save n1ckfg/7c28198019ce330b933412256a0aba87 to your computer and use it in GitHub Desktop.
Save n1ckfg/7c28198019ce330b933412256a0aba87 to your computer and use it in GitHub Desktop.
Convert between .mat and .h5
#!/usr/bin/python3
import h5py
import numpy
import scipy.io
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("file", help="h5 file or mat file")
args = parser.parse_args()
data = {}
if args.file.endswith('.h5'):
with h5py.File(args.file) as fd:
for i in fd.keys():
data[i] = fd[i][...]
scipy.io.savemat(args.file[:-3] + '.mat', data)
elif args.file.endswith('.mat'):
data = scipy.io.loadmat(args.file)
with h5py.File(args.file[:-4] + '.h5', 'w') as fd:
for i in data.keys():
if i not in ['__globals__', '__header__', '__version__']:
fd[i] = numpy.squeeze(data[i])
else:
raise ValueError('filename must ends with .h5 or .mat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment