Created
November 24, 2011 23:27
-
-
Save pgoeser/1392506 to your computer and use it in GitHub Desktop.
quick converter for .mat files into loadable python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# this scripts converts a matlab mat-file to an importable python-file | |
import scipy.io.matlab as mio | |
from scipy import array | |
import re, pprint | |
def mat_to_py(matfile, pyfile=None): | |
if not pyfile: | |
pyfile = re.sub(".mat$",".py$",matfile) | |
mat = mio.loadmat(matfile) | |
fd = file(pyfile,"w") | |
fd.write("# file autogenerated by mat_to_py.py from %s\n\n"%matfile) | |
for k,v in mat.items(): | |
if k.startswith("__"): | |
continue | |
try: | |
if 1 in v.shape: | |
v=v.flatten() | |
l = v.tolist() | |
fd.write(k+" = ") | |
pprint.pprint(l, fd, indent=4) | |
print "processed %s"%k | |
except: | |
print "error processing %s"%k | |
fd.close() | |
if __name__=="__main__": | |
import sys | |
try: | |
matfile = sys.argv[1] | |
except: | |
print "convert mat files to python files" | |
print "usage: matfile inputfile.mat [outputfile.py]" | |
try: | |
pyfile = sys.argv[2] | |
except: | |
pyfile = None | |
mat_to_py(matfile, pyfile) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment