0

Im trying to load a .mat file in python using:

Y = scipy.io.loadmat('XYZ.mat')

These are the keys of the dictionary created

dict_keys(['__header__', '__version__', '__globals__', 'X', 'Y'])

type(Y['X']) is

<class 'scipy.io.matlab.mio5_params.MatlabObject'>

Now, i want the contents of key X as a numpy array.

Y = np.array(Y['X'])

does not work. When i print Y, this is what i get:

array([[(array([[[........]]]), array([[  5, 201,  61]], dtype=uint8))]],
  dtype=[('data', 'O'), ('size', 'O')])

How do i access the elements of the array?

  • What is the error message you are getting? – pedro_bb7 Feb 12 '22 at 12:47
  • Did you try `Y['X'][0]`? – Cris Luengo Feb 12 '22 at 14:38
  • Or you could read the documentation: https://docs.scipy.org/doc/scipy/tutorial/io.html – Cris Luengo Feb 12 '22 at 14:42
  • @CrisLuengo, Y['X'][0][0] of the matlab object did the trick! thank you for pointing in the right direction! – user15084764 Feb 12 '22 at 16:39
  • Recently I explored how a `numpy` structured array is handled by `savemat/loadmat`. At the MATLAB end it is imported as `struct`. In `scipy` is imported a structured array with object dtype fields.https://stackoverflow.com/questions/70963723/saving-a-structured-numpy-array-with-scipy-io-savemat-then-reloading-it-changes/70965097?r=SearchResults&s=1|0.0000#70965097 – hpaulj Feb 12 '22 at 16:46

0 Answers0