0

i have a .mat file.I want to read its each column and want to save each column to separate text file like data_1.txt data_2.txt....data_10.txt

My script is

import numpy as np
import h5py


c1 = h5py.File('test_data.mat', 'r')  
out1=c1.get('dat')


for x in range(10):
   dd=out1[x]
   np.savetxt('data_x.txt', dd,fmt='%10.2f')

But it doesnot write to separate text file, can anybody suggest a better solution for the same. Thanks.

manas
  • 479
  • 2
  • 14

1 Answers1

0

you're close - change the filename by including the index in it

...
    np.savetxt(f"data_{x}.txt", dd,fmt='%10.2f')

the change is subtle: this uses an f-string, which allows you to easily write variables into a string by-name

ti7
  • 16,375
  • 6
  • 40
  • 68