I'm trying to create a Matlab cell array in python and save it as a .mat file, but am running into problems when all the cells contain 2 values:
import scipy.io as sio
twoValues = {'a': array([[array([[2, 2]]), array([[3, 3]])]])}
sio.savemat('test.mat',twoValues)
In Matlab:
load('test.mat')
>>> a
a(:,:,1,1) =
2 3
a(:,:,1,2) =
2 3
>>> class(a)
ans =
int32
Back in python:
threeValues = {'a': array([[array([[2, 2, 2]]), array([[3, 3]])]])}
sio.savemat('test.mat',threeValues)
In Matlab:
>>> a
a =
[3x1 int32] [2x1 int32]
>>> class(a)
ans =
cell
What's the reason for this?
array()is trying to pack your values in the most efficient way possible, because you're not specifyingdtype, and it has to guess at how to store it. When your dimensions match, a plain matrix is evidently the preference. I am so far unsuccessful at coercing it into a cell array when the dimensions match up.dtype=object.