0

When I load in a .mat file with scipy.io.loadmat, I get a data structure which is difficult to deal with because I have to guess at what level in the structure the data is contained. The numpy or scipy representation wraps everything in deep lists. Printing it doesn't usually help because it contains a lot of data. For example:

from scipy.io import loadmat
mat = loadmat("data.mat")
val = mat["someattribute"] # not what I want, the data I can iterate over is one layer deeper (len(val) == 1)
val = mat["someattribute"][0] # len(val) is some big value

Is there some easy way to access these things without having to worry about their representation? I recently switched from python 2.6.6 to 2.7.3 and noticed that the representation in the numpy/scipy libraries changed (and hence my code broke).

Shai
  • 111,146
  • 38
  • 238
  • 371
anonymous
  • 193
  • 2
  • 5

1 Answers1

0

You might be able to get away with

val = mat['someattribute'].ravel()

which will flatten val to a 1D np.array if you know it is 1D data or

val = mat['someattribute'].squeeze()

which will remove all the len=1 dimensions (doc)

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • The matrix is given to me as a dictionary. – anonymous Dec 04 '12 at 07:21
  • @anonymous you should include that information in your question. I guessed (wrong) that you were trying get out an array. You will get better answers the more clear your question is. – tacaswell Dec 04 '12 at 15:40