1

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] ]

2
  • 1
    I'd suggest creating a sample cell and or matrix in MATLAB, save it, and the look at what loadmat produces. numpy numeric dtype arrays transfer as matrix, though possibly with a change in order. I think cell are rendered as object dtype array in numpy. But experiment. Commented Jan 4, 2022 at 21:21
  • I tested it and you are right, np array that it's dtype is object creates a matlab cell. Thanks! Commented Jan 5, 2022 at 7:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what is np in your code ? A missing import ?
@pippo1980 yes, np is a common short for numpy. added the import to the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.