9

For the following code, I am getting the error I have put in title:

import scipy.io as sio
import numpy as np

temp = np.load('temp.npy')
sio.savemat('final.mat',temp)

Although AttributeError is a common error in python, I did not find anything useful for 'items' as mentioned in the title. How can we fix this?

2 Answers 2

17

It takes a dict as the second argument not an array:

From the docs:

mdict : dict

Dictionary from which to save matfile variables.

I am not overly familiar but I imagine you pass the name as the key and the array as the value, something like:

sio.savemat('final.mat',{"foo":temp})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Do we have to convert arrays to dictionaries in order to save them as .mat file?
@A.M., as far as I can tell you pass a string as the key and the array as the value. There are other keywords that affect how the array is stored
0

I solved this issue as follow:

from scipy.io import savemat
import numpy as np
num= np.arange(20)
mymat={'num':num}
savemat('mymat.mat', mymat)

Comments

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.