Guto Schettini
Guto Schettini

Reputation: 299

Strange array from .mat file. How to access the data?

I´m using Jupyter Lab!

I opened a '.mat' file, using hdf5storage. This file was generated by a software which operates an instrument of data acquisition (ADCP by Rowe, for who may ask! Oceanographic stuff...)

I can access the keys and extract the data from all keys but one... the 'Gps' contains latitude and longitude data, supposely in a two columns and many rows array... but its shape is (1x1), and when I print it I can see the data, but don´t know how to access it!

How can I access this data?

The file is here....

https://drive.google.com/file/d/15JEA5y5_Zt52FpPa--Cp_wwl2TYrnlQZ/view?usp=sharing

This is the notebook (what fits on the screen) but you can see what I mean.

enter image description here

Upvotes: 0

Views: 579

Answers (1)

hpaulj
hpaulj

Reputation: 231665

Looks like this the 'old' style MATLAB mat file, which I can load with scipy.io.loadmat

In [3]: data = io.loadmat('../Downloads/ADP_1911041032.mat')
In [5]: data['__header__']
Out[5]: b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Dec 06 10:01:19 2019'
In [6]: data['Gps']
Out[6]: 
array([[(array([[-32.05298353, -32.0529503 , -32.0528978 , -32.05284081,
                -32.05270699, -32.05265406, -32.05266599, -32.05267098,
                -32.05268485, -32.05271432, -32.0526721 , -32.05260368,
                -32.05254208, -32.05249182, -32.05245293, -32.05241743,
...
                -52.04648597, -52.0463069 , -52.04610979, -52.04591461,
                -52.04569035, -52.04549773, -52.04532165]]))                                                                ]],
      dtype=[('lat', 'O'), ('lon', 'O')])
In [7]: gps=data['Gps']
In [8]: gps.shape
Out[8]: (1, 1)
In [9]: gps.dtype
Out[9]: dtype([('lat', 'O'), ('lon', 'O')])
In [10]: gps[0,0].shape
Out[10]: ()
In [11]: gps[0,0]['lat'].shape
Out[11]: (1, 103)
In [12]: gps[0,0]['lat'].dtype
Out[12]: dtype('<f8')

loadmat converts the MATLAB cell and struct into numpy array objects. Here it's returned a (1,1) shaped array (MATLAB is always 2+d), with a compound dtype, 2 fields

In [13]: gps[0,0]['lat']
Out[13]: 
array([[-32.05298353, -32.0529503 , -32.0528978 , -32.05284081,
        -32.05270699, -32.05265406, -32.05266599, -32.05267098,
        -32.05268485, -32.05271432, -32.0526721 , -32.05260368,
         ...
        -32.0491144 , -32.04907168, -32.04903315]])

gps.item()[0] returns the same array.

Basically you need to pay attention to the shape and dtype and be prepared to work your way 'down' through the structure step by step. It helps to understand numpy arrays, structured as well as object.

Upvotes: 1

Related Questions