Skip to content

Instantly share code, notes, and snippets.

@iaalm
Created October 18, 2018 06:50
Show Gist options
  • Save iaalm/fc4133707a33c99c693408c304d426bd to your computer and use it in GitHub Desktop.
Save iaalm/fc4133707a33c99c693408c304d426bd 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')
@iaalm
Copy link
Author

iaalm commented Jun 24, 2023

Hi @AgriPooja , is it possible to share you .mat files? I can have a try with it.

@AgriPooja
Copy link

Hello, i am not able to share the .mat files here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment