Using python to create a .mat file with a cell array in it

729 Views Asked by At

I'm trying to create a mat file with a cell it it. I tried to apply some solutions I found on the internet but non of them worked.

The code I'm wishing to have at the end is something like that:

import scipy.io as sio

l = [ [ [1,2,3], [4,5,6] ], [ [10, 20, 30] , [40, 50, 60] ] ]

c = # Some code to make it appear as a cell array in matlab

sio.savemat('f.mat', { cell : c })

so the cell array at the end would look like: [ [1,2,3], [4,5,6] ] [ [10, 20, 30] , [40, 50, 60] ]

1

There are 1 best solutions below

2
MeatBALL On

To create a cell in a .mat file, the corresponding array in python should be a np.arrat whose dtype is np.object.

should look something like this:

import numpy as np
import scipy.io as sio
c = np.array( array, dtype=np.object)
sio.savemat('file.mat', '{'cell' : c})

credit to @hpaulj that commented this suggestion here, I checked it and it seems to work.