walid
walid

Reputation: 305

How to retreive data as a 2d matrix?

I got a file (.txt) containing data such as this sort:

Y=[]; 
Y=[Y ;0.178,0.745,0.247,] ; 
Y=[Y ;0.237,0.932,0.347,] ; 
Y=[Y ;0.555,0.666,0.777,] ; 
     .
     .
     .

I want to extract the data as a 2d matrix like:

array([[0.178,0.745,0.247],        
       [0.237,0.932,0.347],        
       [ 0.555,0.666,0.777],               
                .               
                .               
                .       
       ]) 

I would appreciate any help. And thanks

Upvotes: 0

Views: 25

Answers (1)

Alexey Mints
Alexey Mints

Reputation: 448

Something like that?

nn = []
with open("log.txt") as infile:
    for line in infile:
        n = re.sub(r'[^\,\.0-9]', '', line).split(',')
        if len(n) <= 2:
            continue
        nn.append(n[:-1])
nn = np.array(nn, dtype=float)
print(nn)

upd: you should have mentioned it's matlab. Never worked with it, but pretty sure there should be some python tool to read matlab data

Upvotes: 1

Related Questions