user5224720
user5224720

Reputation:

Save multiple ndarray into each individual content of mat file - scipy

How can you save multiple ndarray into one mat file, using scipy function savemat? I wonder if I have two matrices call A and B, can I save them both to one result.mat like followed:

sio.savemat('result.mat', {'A':A})
sio.savemat('result.mat', {'B':B})

I did that and then open result.mat in MATLAB to only find the matrix B... A got overwrite. Any helps?

Upvotes: 0

Views: 1436

Answers (1)

hpaulj
hpaulj

Reputation: 231385

In [436]: with open('test.mat','wb') as f:  # need 'wb' in Python3
    savemat(f, {'A':np.arange(10)})
    savemat(f, {'B':np.ones((3,3))})
   .....:     

In [437]: loadmat('test.mat')
Out[437]: 
{'A': array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
 '__version__': '1.0',
 'B': array([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri May 13 16:38:04 2016'}

Upvotes: 1

Related Questions