Madlad
Madlad

Reputation: 155

How to load a Matlab structure (dictionary) as a Python dictionary?

I have been handed some chunky data as a Matlab dictionary and my task is to play around with this on Python. Here I am including a small subset of my matlab dictionary.

What I have tried so far to unpack this and make a dictionary in python is:

import scipy.io as spio
mydata = spio.loadmat('testdict.mat')
new = mydata.get('testdict')

If you can see from loading the .mat file with loadmat, I get a sort of array of arrays. In my original dictionary, I have 72 field names in which I want to make keys in Python. I just cannot wrap around how to iterate through this array in order to do that.

Here you can see some of the output I get:

Upvotes: 3

Views: 3434

Answers (1)

Jordan Simpson
Jordan Simpson

Reputation: 94

If I understand what you would like to do, you want to iterate through the columns so to speak of this numpy array from matplotlib.

After a bit more brewing I came up with another attempt using the data you provided. In this attempt, I iterated through the array of arrays while adding the field names of the numpy arrays and the inner most values of arrays to a dictionary as keys and values respectively:

import scipy.io as spio
import numpy as np

mydata = spio.loadmat('testdict.mat')
new = mydata.get('testdict')

recordarr = np.rec.array(new)
myDictionary = {}
myList = []

for field in recordarr.dtype.names: #iterates through field names of numpy array
    for array in recordarr[field]: #iterates through the array of each numpy array
        for value in array:
            myList.append(np.squeeze(value.flatten()[0]))
            #print(np.squeeze(value.flatten()[0]))
        
    myDictionary[field] = myList
    myList = []
    #break

Here is the result of the following code requesting the value from the key named year from the myDictionary variable:

In [153]: myDictionary['year']
Out[153]: [1852, 1852, 1853, 1853, 1853, 1853, 1853, 1853, 1853, 1853, 1854]

The above result can be seen as the output if the commented lines in the given code are uncommented.

This link helped me understand how to format the matplotlib numpy array to access its field values.

This post also helped me understand how to get values from the newly formatted matplotlib numpy arrays.

Previous Attempt

Here's an attempt to iterate through the desired array, not the full answer but a piece of the puzzle nonetheless.

If you are able to access the desired array within these arrays, perhaps you can iterate through them with something similar to this:

Disclaimer: Not sure what values for keys were suppose to be thus used integers.

arrays = [['Hello'], ['World'], ['Foobar'], ['Alice'], ['Bob'], ['Chuck']]
d = {}
n = 0

for array in arrays:
    for element in array:
        d[element] = n
        n += 1
    
print(d)

Output

{'Hello': 0, 'World': 1, 'Foobar': 2, 'Anne': 3, 'Bob': 4, 'Chuck': 5}

Upvotes: 1

Related Questions