Search code examples
pythonmatrixnumpyscipymat-file

Creating a Three Dimensional Matrix structure and writing in mat file in Python


I am very new to Python. I need to have a 3 dimensional matrix, in order to save 8 by 8 matrix in some length. Let's call 530. The problem is that I used np.array since matrix cannot have more than 2 dimensions as numpy argues.
R = zeros([8,8,530],float)
I calculated my 8 by 8 matrix as a np.matrix
R[:,:,ii] = smallR
And, then I try to save it in mat file as scipy claims to do so.
sio.savemat('R.mat',R)
However, error says 'numpy.ndarray' object has no attribute 'items'

/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio.py:266: FutureWarning: Using oned_as default value ('column') This will change to 'row' in future versions oned_as=oned_as)
Traceback (most recent call last):
File "ClassName.py", line 83, in <module> print (buildR()[1])
File "ClassName.py", line 81, in buildR sio.savemat('R.mat',R)
File "/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 269, in savemat MW.put_variables(mdict)
File "/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio5.py", line 827, in put_variables
for name, var in mdict.items(): AttributeError: 'numpy.ndarray' object has no attribute 'items'


Solution

  • If you type help(sio.savemat), you see:

    savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as=None)
        Save a dictionary of names and arrays into a MATLAB-style .mat file.
    [...]
        mdict : dict
            Dictionary from which to save matfile variables.
    

    and so even if you don't recognize .items() as a dictionary method, it's clear we're going to need to use a dictionary (a set of key, value pairs; google "python dictionary tutorial" if necessary).

    In this case:

    >>> from numpy import zeros
    >>> from scipy import io as sio
    >>> 
    >>> R = zeros([8,8,530],float)
    >>> R += 12.3
    >>> 
    >>> sio.savemat('R.mat', {'R': R})
    >>> 
    >>> S = sio.loadmat('R.mat')
    >>> S
    {'R': array([[[ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            ..., 
    
            ..., 
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3]]]), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file Platform: posix, Created on: Sat Feb 25 18:16:02 2012', '__globals__': []}
    >>> S['R']
    array([[[ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            ..., 
    
            ..., 
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
            [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3]]])
    

    Basically, a dictionary is used so that the arrays can be named, as you can store multiple objects in one .mat file.