Can anyone explain mdict in python (scipy.io), for example in scipy.io.savemat ()?

I am working on loading some files in python and then once the files are loaded I want to export them to a .mat file and do the rest of the processing in MATLAB. I understand that I can do this with:

    import scipy.io as sio
    # load some files, assign loaded data to variables
    # ...
    sio.savemat(filename,mdict)

      

I understand what's going on here and have seen the syntax for this something like:

    alist = [5,3,6]
    sio.savemat('small_list.mat',mdict={'alist':alist})

      

Can someone please explain what I am doing in the second part of the argument for the argument sio.savemat()

i.e. mdict = {'alist':alist}

? I may just be confused about which is more pythonic (I am relatively new to python and from a background working mostly with C ++), but I am confused as to what the term mdict does in terms of which parts of the syntax do what (is the quoted part the name of my variable in MATLAB and unadjusted my variable in python?)

Also, what would it look like if I wanted to save (and then load into MATLAB) multiple separate variables in one file .mat

?

+3


source to share


1 answer


According to the documents, it savemat

is defined as

io.savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as='row')

      

So the 2nd argument is required and can be supplied with mdict=...

or without part .

The reason it expects it to be a dictionary is because it needs to know the name (s) under which the variable (data) is stored. If it accepts a variable, it will have to compose a name. In Python syntax, these 2 expressions pass the same value to foo

. The name "alist" is not passed to the foo

.

alist = np.arange(10)
foo('test.mat', alist)

foo('test.mat', np.arange(10))

      

loadmat

also returns a dictionary like:

{'__version__': '1.0',
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Mon May 25 09:19:36 2015',
 '__globals__': [],
 'alist': array([[1, 2, 3]])}

      

And when I am load text.mat

in octave

, a variable appears in the workspace alist

.

The functional form load

gives astructure



octave:5> r=load('test.mat')
r =
  scalar structure containing the fields:
    alist =
      1  2  3

      

The corresponding functional form octave

save

is

save ("-option1", ..., "file", "v1", ...)

      

where "v1"

is the name of the variable you want to store.

octave/MATLAB

retrieves the values ​​of these variables from the global namespace. This is (more strongly) deprecated in Python.

save('-7','test1.mat',"alist")

      

in is octave

loaded as

In [1256]: io.loadmat('test1.mat')
Out[1256]: 
{'__version__': '1.0',
 '__header__': b'MATLAB 5.0 MAT-file, written by Octave 3.8.1, 2015-05-25 17:02:15 UTC',
 '__globals__': [],
 'alist': array([[1, 2, 3]])}

      

Except for the header content, they are the same thing.

+4


source







All Articles