user3856486
user3856486

Reputation: 547

Saving data in a .mat file using a for loop using python

I am trying to store data in a .mat file using python. The data, an array is updated at each iteration of the for loop. I have trouble achieving this. The code and the error are as attached.

    from array import *
    import scipy.io

    a={}
    b=[9,8,7,6]
    i=0
    for i in range(10):
        a['m']=b.append(i)
        scipy.io.savemat('test.mat',a)
        i+=1

The error I get:

    Traceback (most recent call last):
    File "C:/Python27/tryin", line 15, in <module>
         scipy.io.savemat('test.mat',a)
    File "C:\Users\rlangari\Portable Python 2.7.6.1\App\lib\site-packages\scipy\io\matlab\mio.py", line 204, in savemat
    MW.put_variables(mdict)
    File "C:\Users\rlangari\Portable Python 2.7.6.1\App\lib\site-packages\scipy\io\matlab\mio5.py", line 872, in put_variables
    self._matrix_writer.write_top(var, asbytes(name), is_global)
    File "C:\Users\rlangari\Portable Python 2.7.6.1\App\lib\site-packages\scipy\io\matlab\mio5.py", line 622, in write_top
    self.write(arr)
    File "C:\Users\rlangari\Portable Python 2.7.6.1\App\lib\site-packages\scipy\io\matlab\mio5.py", line 643, in write
    % (arr, type(arr)))
    TypeError: Could not convert None (type <type 'NoneType'>) to array

Upvotes: 0

Views: 2044

Answers (1)

Pankaj Sharma
Pankaj Sharma

Reputation: 679

b.append(i) 

returns nothing. therefore each time you're getting an exception.

Upvotes: 1

Related Questions