scipy.io.savemat How to save global variables?
Question: I'm trying to use Python like one would do in Matlab. Basically I have some
Python code for which I have run and it has generated some global variables.
Say, a = 5 b = 3

I would like to save these to a .mat file , that will be openable by Matlab.
The goal is to be able to see the global variables in Matlab, just as one
would when saving to a .mat file in Matlab.

I've seen examples where savemat is used to save dictionaries/arrays, but not
where it saves the global variables.

How may I do so? Is this something that scipy just cannot do?

Thanks.


Answer: You most probably don't want to save globals, but locals (though the variables
are local to the interpreter). You can access and update them through the
`locals()` and `globals()` functions.

From there you can use your preferred method of storage, such as
[pickle](https://docs.python.org/3/library/pickle.html),
[marshal](https://docs.python.org/3/library/marshal.html),
[json](https://docs.python.org/3/library/json.html) or others, depending on
which level of security you want and which object types you have at hand.
However, be wary that messing with those functions may get you into some
trouble, since they also report functions and some internal variables.

However, if you intend to (like Matlab) just load and store variables through
the GUI, you may want to check the **[Spyder
IDE](http://pythonhosted.org/spyder/)** ([GitHub
project](https://github.com/spyder-ide/spyder)). It is a very nice IDE, geared
toward scientific usage and very similar (in appearance) to Matlab's old
interface. Most importantly, it offers a tab/pane named "**Variable explorer**
" which tracks what you want to store, and have both a "**Save data as...** "
and "**Import data** " buttons and the "Save data as..." dialog supports
saving to Spyder data files (_.spydata), Matlab files (_.mat) and HDF5 (*.h5).

If you want to do this programatically, you may check how Spyder does it.

