0

I have many .mat files that I need to bring into python and do things with. I found a method to easily deal with the .mat files here. This works very well but now I would like to iterate over all the values. How do I do this?

wObs = 'path/to/matfile.mat'
mat = loadmat(wObs)
mat = mat['wObjs']

#function I wrote to get the field names of the .mat file. Returns a list of field names.
fields = mat_feildnames(wObs, 'wObjs') 

#now I want to itterate over the data and do things... 
for row in range(len(mat)):
    for field in fields:

        #field is string value.. how do I make this work?
        #this format if field is a valid field name will return what I want. 
        print(mat[row].field)


>>mat['wObjs'] 
   Returns:
   array([<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe5e80>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe5eb8>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7080>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7048>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe70f0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe71d0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe72b0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7518>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe75c0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7668>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe76a0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7748>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe77f0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe78d0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7940>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe79b0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7a20>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7a90>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7b00>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7b70>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7be0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7c50>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7cc0>,
   <scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7d30>], dtype=object)
Community
  • 1
  • 1
Matt Camp
  • 1,448
  • 3
  • 17
  • 38

1 Answers1

0

I re-read the linked to post and figured I'd try one of the lesser responses to see what it did.. it worked.. yay! Below is the answer to my above question.

import scipy.io as spio

vig=spio.loadmat(wObs,squeeze_me=True)
for field in fields:
    print(vig['wObjs'][field])
Matt Camp
  • 1,448
  • 3
  • 17
  • 38