2
votes

I'm trying to save a dictionary whose values are numpy arrays of different length to a mat file so that collaborators can load the dictionary as a cell array in MATLAB.

An example of input:

 data={traces: {0: array([], dtype=float64), 1: array([], dtype=float64), 
 2: array([], dtype=float64), 3: array([], dtype=float64), 
 4: array([], dtype=float64), 5: array([], dtype=float64), 
 6: array([], dtype=float64), 7: array([], dtype=float64), 
 8: array([], dtype=float64), 9: array([], dtype=float64)}}

savemat('test.mat', mdict=data)

However, I get the error:

 TypeError: Could not convert {0: array([], dtype=float64), 1: array([], dtype=float64), 
 2:array([], dtype=float64), 3: array([], dtype=float64), 4: array([], dtype=float64), 
 5:  array([], dtype=float64), 6: array([], dtype=float64), 7: array([], dtype=float64), 
 8: array([], dtype=float64), 9: array([], dtype=float64)} (type <type 'dict'>) to array

How can I save this dictionary to a mat file?

1

1 Answers

3
votes

You are defining a dictionary with key 'traces' and a value of a dictionary. Remove 'traces', replace key values with strings instead of int (0) or numeric strings ('0') and save in Python normally.

from scipy.io import savemat
from numpy import *

data={'t0': array([], dtype=float64), 't1': array([], dtype=float64), 
 't2': array([], dtype=float64), 't3': array([], dtype=float64), 
 't4': array([], dtype=float64), 't5': array([], dtype=float64), 
 't6': array([], dtype=float64), 't7': array([], dtype=float64), 
 't8': array([], dtype=float64), 't9': array([], dtype=float64)}

# print data
savemat('test.mat', data, oned_as='row')

You can load in MATLAB by

traces = load('test.mat');