-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnpy_to_matlab.py
executable file
·64 lines (42 loc) · 1.41 KB
/
npy_to_matlab.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import sys
import scipy.io
import numpy as np
import os
FOLDER = 'input'
def npy_to_matlab(name):
files = (os.listdir(FOLDER))
path = os.path.join(os.getcwd(),FOLDER)
npyFiles=[]
matStructure = {}
for f in files:
extension = os.path.splitext(f)[1]
if extension == '.npy':
npyFiles.append(f)
if not npyFiles:
print "Error: There are no .npy files in %s folder"%(FOLDER)
sys.exit(0)
for f in npyFiles:
currentFile= os.path.join(path,f)
variable = os.path.splitext(f)[0]
#MATLAB only loads variables that start with normal characters
variable = variable.lstrip('0123456789.-_ ')
try:
values = np.load(currentFile)
except IOError:
print "Error: can\'t find file or read data"
else:
matStructure[variable] = values
filename = name + '.mat'
if matStructure:
scipy.io.savemat(filename, matStructure)
def printUsage():
print "Usage: python %s output_filename "%(sys.argv[0])
if __name__ == "__main__":
if len(sys.argv) < 2:
printUsage()
sys.exit(0)
if not os.path.exists(FOLDER):
os.makedirs(FOLDER)
filename=str(sys.argv[1])
npy_to_matlab(filename)