-
Notifications
You must be signed in to change notification settings - Fork 2
/
mat2csv.py
49 lines (34 loc) · 1.18 KB
/
mat2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import scipy.io
import argparse
import numpy
import sys
import os
import os.path
if __name__ == "__main__" :
parser = argparse.ArgumentParser()
parser.add_argument("-m","--mat-filename", help="MATLAB matrix filename", \
type=str, required=True, dest="matfilename")
if len(sys.argv) == 1:
parser.print_help()
exit(1)
args = parser.parse_args()
filename = args.matfilename
errmsg = []
if not (os.path.isfile(filename)):
print("File " + filename + " does not exist")
exit(1)
m = scipy.io.loadmat(filename)
howmany = 0
for name in m.keys():
if (isinstance( m[name],(numpy.ndarray))):
howmany += 1
print(howmany)
for name in m.keys():
if (isinstance( m[name],(numpy.ndarray))):
if (len(m[name].shape) == 2):
print(name)
print(m[name].shape[0], " , ", m[name].shape[1])
for i in range(m[name].shape[0]):
for j in range(m[name].shape[1]-1):
sys.stdout.write( str(m[name][i, j]) + " , ")
sys.stdout.write( str(m[name][i, -1]) + "\n")