2

I have a series of numpy arrays that I want to save in a .mat file so that I can plot the data later. (I don't want to use Pickle because my actual program is much more complicated and has more than 2 arrays.) My MWE is:

import numpy as np
import mat4py as m4p

x = np.array([1,20,0.4,0.5,9,8.8])
y = np.array([0.3,0.6,1,1,0.01,0.7])

data = {'x': x,
        'y': y}
m4p.savemat('datafile.mat', data)

but I get an error ValueError: Only dicts, two dimensional numeric, and char arrays are currently supported.

What does this mean and how can I fix this?

2
  • Try scipy's savemat. from scipy import io; io.savemat(...)`. But it will save your arrays as 2d since that is what matlab expects. Commented Nov 27, 2016 at 14:44
  • mat4py works with lists not numpy arrays. scipy is built on numpy. Commented Nov 27, 2016 at 14:54

3 Answers 3

6
In [853]: from scipy import io
In [854]: x = np.array([1,20,0.4,0.5,9,8.8])
     ...: y = np.array([0.3,0.6,1,1,0.01,0.7])
     ...: 
In [855]: data={'x':x, 'y':y}
In [856]: io.savemat('test.mat',data)
In [857]: io.loadmat('test.mat')
Out[857]: 
{'__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sun Nov 27 09:30:35 2016',
 '__version__': '1.0',
 'x': array([[  1. ,  20. ,   0.4,   0.5,   9. ,   8.8]]),
 'y': array([[ 0.3 ,  0.6 ,  1.  ,  1.  ,  0.01,  0.7 ]])}

For MATLAB compatibility the arrays have been turned into 2d orderF arrays.

h5py is another option. Newer Matlab versions use the HDF5 format, providing greater compatibility with other languages.

np.savez can save a dictionary of arrays without modifying them:

In [881]: data={'x':x, 'y':y,'xy':np.array((x,y))}
In [882]: np.savez('test',**data)
In [883]: D=np.load('test.npz')
In [884]: D.keys()
Out[884]: ['y', 'x', 'xy']
In [885]: D['xy']
Out[885]: 
array([[  1.00000000e+00,   2.00000000e+01,   4.00000000e-01,
          5.00000000e-01,   9.00000000e+00,   8.80000000e+00],
       [  3.00000000e-01,   6.00000000e-01,   1.00000000e+00,
          1.00000000e+00,   1.00000000e-02,   7.00000000e-01]])

D is an NPZFile object, which is a lazy loader. So it does not dump all the arrays directly into memory. You access them by key name.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm interested in the h5py option to write to an already existing .mat file created with MATLAB. Is this possible? I can't quite see how to structure the arrays from this answer.
@villaa, it should be straight forward to load the arrays as shown above. Writing arrays to a HDF5 file is documented in the h5py docs. The only nuance is that order F files will be saved as order C. I'd suggest you read up on h5py, try some simple cases, and ask a new question if you have problems.
4

You have to convert numpy arrays into python lists.

You can create x and y like:

x = [1,20,0.4,0.5,9,8.8]
y = [0.3,0.6,1,1,0.01,0.7]

Now data will contain python lists and m4p.savemat('datafile.mat', data) will work.

Edit:

If you need to work with numpy arrays, you can convert them on-the-fly when creating data as follows:

data = {'x' : x.tolist(), 'y' : y.tolist()}

Comments

1

Unless you need to interface to Matlab, there really is no reason to use mat4py. You can use the build-in numpy.save() for single arrays, or numpy.savez() for multiple arrays. For large arrays use numpy.savez_compressed().

4 Comments

That looks nice. I'm currently using Spyder - is there a shortcut for saving everything on my workspace so that I won't have to specify each variable, array, etc in savez_compressed?
I cant follow you. Spyder can save all your .py files. Here, we're talking about saving one or several arrays into a file during runtime of the script in python.
@ImportanceOfBeingEarnest I'll eventually be running this code on HPC clusters where I can't save variables using the Spyder GUI. What I meant was, is there a way to save the workspace using savez_compressed without specifying each variable?
@MedullaOblongata I am not aware of any such option. It wouldn't make much sense in my opinion anyways, since you normally do not want all your workspace saved but only the result of your calculations. Keeping track of then saved variables also makes sense, to later be able to recover them.

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.