Created
May 17, 2017 22:50
-
-
Save cpatdowling/54a409e1ee36bc9f550c87ae1ebc46ef to your computer and use it in GitHub Desktop.
Read a Matlab text array into NumPy array
This file contains hidden or 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
def read_matlab_float(strin): | |
strin = strin.strip() | |
tokens = strin.split("e") | |
sign_ = tokens[1][0] | |
if sign_ == "-": | |
sign = -1.0 | |
else: | |
sign = 1.0 | |
p = float(tokens[1][1:].strip()) | |
mant = float(tokens[0]) | |
return( mant * np.power(10.0, (sign*p)) ) | |
def read_matlab_txt_array(filepath, delimiter=","): | |
with open(filepath) as f: | |
rows_ = f.readlines() | |
rows = [ row.strip().split(delimiter) for row in rows_ ] | |
output = np.zeros( (len(rows), len(rows[0])) ) | |
for i in range(len(rows)): | |
for j in range(len(rows[i])): | |
output[i,j] = read_matlab_float(rows[i][j]) | |
return(output) | |
test = "1.122949e-13" | |
read_matlab_float(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment