Skip to content Skip to sidebar Skip to footer

Convert Multiple Python Dictionaries To Matlab Structure Array With Scipy.io Savemat

A simple question, but one I can't seem to be able to wrap my head around. I'm using the scipy.io library to save Python dictionaries as a Matlab structs. Now, the documentation of

Solution 1:

Here's a solution:

In Python:

>>>a_dict = {'field1': 0.5, 'field2': 'a string'}>>>b_dict = {'field1': 1, 'field2': 'another string'}>>>sio.savemat('saved_struct.mat', {'dict_array':[a_dict,b_dict]})

In MATLAB:

s = load('saved_struct.mat');struct_array = [s.dict_array{:}];

You will end up with a structure array in MATLAB as desired.

struct_array = 

  1×2struct array with fields:

    field1
    field2

Solution 2:

@UnbearableLightness has the simplest solution, but to clarify the structured array suggestion, I'll give an example.

Define a structured array:

In [192]: arr = np.array([(0.5,'one'),(0.6,'two'),(0.8,'three')], dtype=[('field1',float),('field2','U10')])                                                                                        

and a list of dictionaries with the same fields and data:

In [194]: dicts = [{'field1':0.5, 'field2':'one'},{'field1':0.6, 'field2':'two'},{'field1':0.8,'field2':'three'}]

In [195]: arr                                                                                          
Out[195]: 
array([(0.5, 'one'), (0.6, 'two'), (0.8, 'three')],
      dtype=[('field1', '<f8'), ('field2', '<U10')])

In [196]: dicts                                                                                        
Out[196]: 
[{'field1': 0.5, 'field2': 'one'},
 {'field1': 0.6, 'field2': 'two'},
 {'field1': 0.8, 'field2': 'three'}]

save and load:

In [197]: io.savemat('ones.mat', {'arr':arr, 'dicts':dicts})                                           
In [198]: io.loadmat('ones.mat')                                                                       
Out[198]: 
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri May  1 09:06:19 2020',
 '__version__': '1.0',
 '__globals__': [],
 'arr': array([[(array([[0.5]]), array(['one'], dtype='<U3')),
         (array([[0.6]]), array(['two'], dtype='<U3')),
         (array([[0.8]]), array(['three'], dtype='<U5'))]],
       dtype=[('field1', 'O'), ('field2', 'O')]),
 'dicts': array([[array([[(array([[0.5]]), array(['one'], dtype='<U3'))]],
       dtype=[('field1', 'O'), ('field2', 'O')]),
         array([[(array([[0.6]]), array(['two'], dtype='<U3'))]],
       dtype=[('field1', 'O'), ('field2', 'O')]),
         array([[(array([[0.8]]), array(['three'], dtype='<U5'))]],
       dtype=[('field1', 'O'), ('field2', 'O')])]], dtype=object)}

savemat has created some object dtype arrays (and fields) and 2d MATLAB like arrays.

In an Octave session:

>> load ones.mat

The arr is a struct array with 2 fields:

>> arr
arr =

  1x3 struct array containing the fields:

    field1
    field2

>> arr.field1
ans =  0.50000
ans =  0.60000
ans =  0.80000
>> arr.field2
ans = one
ans = two
ans = three

dicts is a cell with scalar structures:

>> dicts
dicts =
{
  [1,1] =

    scalar structure containing the fields:
      field1 =  0.50000
      field2 = one

  [1,2] =

    scalar structure containing the fields:
      field1 =  0.60000
      field2 = two

  [1,3] =

    scalar structure containing the fields:
      field1 =  0.80000
      field2 = three

}

which can be converted to the same struct array as @Unbearable showed:

>> [dicts{:}]
ans =

  1x3 struct array containing the fields:

    field1
    field2

>> _.field1
error:'_' undefined near line 1 column 1>> [dicts{:}].field1
ans =  0.50000
ans =  0.60000
ans =  0.80000>> [dicts{:}].field2
ans = one
ans = two
ans = three

Post a Comment for "Convert Multiple Python Dictionaries To Matlab Structure Array With Scipy.io Savemat"