Reading Matlab files – Structures

Suppose you have saved a structure called “data” in matlab:

% "x" and "y" are two fields in the structure "data"
data.x = linspace(1,100,100);
data.y = linspace(1,100,100);

% save a .mat file
save('matlabfile.mat','data')

To read “x” and “y” into python:

import scipy.io

# import file into a dictionary
f = scipi.io.loadmat('matlabfile.mat')

# read in the structure
data = f['data']

# get the fields
x = data[0,0]['x']
y = data[0,0]['y']

Reading Matlab files

For this example, first create a Matlab file:

In matlab…

x = magic(200); % This creates a matrix
save('matlabfile.mat','x') % This saves the matrix in a file named matlabfile.mat

Then in python, open file with this:

import scipy.io
f = scipy.io.loadmat('matlabfile.mat') # Imports file into a dictionary where the "key" is the matlab name of the variable (i.e. x) and the data is stored as the dictionary value

x = f["x"] # Makes the python variable "x" equal to the data associated with key "x" (in f)

For files saved with Matlab v7.3, use this:

import h5py
import numpy

f = h5py.File('matlabfile.mat')
x = f["x"]
numpy.array(x)

Dependencies: Numpy and h5py

Installing Python (Linux)

 

UPDATE: A better way… Installing Python with Anaconda

 


The approach used here to install Python is:

  1. First, we install Spyder (along with all the libraries that come with it)
  2. Then, we install additional libraries (than do not come with Spyder)

Spyder is a powerful interactive development environment (IDE) for the Python language… kinda’ a graphical (Matlab-looking) interface that allows you to easily write you Python code, run it, debug it, etc. Another nice thing… when installing Spyder, it automatically installs not only Python, but also a bunch of scientific libraries like NumPy, SciPy, Matplotlib and iPhyton.


Install Spyder in Linux (Ubuntu)

Install Spyder via Ubuntu Software Centre:

  1. Open the “Ubuntu Software Centre”
  2. Search for “spyder”
  3. Click install
  4. It should be installed under the “Scientific” menu

OR install Spyder via a terminal:

sudo apt-get install spyder


Install additional libraries

Basemap

You may be apble to search for “Basemap” in the software centre and click install… if not read below:

Instructions came from here: http://matplotlib.org/basemap/users/installing.html

  1. Download source tarballs here.
  2. Unpack, go into the directory and execute:
sudo python setup.py install

If it doesn’t work, execute the following:

cd geos-3.3.3
export GEOS_DIR=/usr/local
./configure --prefix=$GEOS_DIR
sudo make
sudo make install
cd ..
sudo python setup.py install

xlrd (read Excel files)

In “Ubuntu Software Centre”, search for “xlrd”. Click install.

Or… in terminal…

sudo apt-get install xlrd

h5py – Python interface to HDF5 file format

  1. Open “Software Centre”
  2. Search “h5py”
  3. Click install

Or… in terminal…

sudo apt-get install h5py

netcdf4-python

Instruction came from here: http://code.google.com/p/netcdf4-python/wiki/UbuntuInstall

First install HDF5

  1. Download the current HDF5 source release.
  2. Unpack, go into the directory and execute:
./configure --prefix=/usr/local --enable-shared --enable-hl
make
sudo make install

Download and install zlib library

  1. Download the current zlib.
  2. Unpack, go into the directory and execute:
./configure
make
sudo make install

Then install netCDF4

  1. Download the current netCDF4 source release.
  2. Unpack, go into the directory and execute:
LDFLAGS=-L/usr/local/lib CPPFLAGS=-I/usr/local/include ./configure --enable-netcdf-4 --enable-dap --enable-shared --prefix=/usr/local
make 
sudo make install

Finally, install netcdf4-python

  1. Download the current netcdf4-python source release
  2. Unpack, go into the directory and execute:
sudo ldconfig
sudo python setup.py install

Installing Python (Windows)

 

UPDATE: A better way… Installing Python with Anaconda

 


The approach that we use here to install Python is:

  1. First, we install Python(x,y), which comes not only with the Python  Language, but also with Spyder and LOT of other libraries.
  2. Then, we download and install other libraries that do not come with Python(x,y).

Note: Spyder is a powerful interactive development environment (IDE) for the Python language… kinda’ a graphical (Matlab-looking) interface that allows you to easily write you Python code, run it, debug it, etc.


Install Phyton(x,y)

  1. Download and install the current release from HERE

Install additional libraries

still to do

Installing Python (Mac OS)

UPDATE: A better way… Installing Python with Anaconda


 

Instructions below were contributed by Robert Branton.


Here is final synopsis of what I did to install and run spyder on Mountain Lion.

  1. install XQuartz – http://xquartz.macosforge.org/landing/
  2. update to latest python – http://www.python.org/download/releases/3.3.0/
  3. install Tcl/Tk – http://www.python.org/download/mac/tcltk/
  4. install XCODE from App Store
    • build Command Line Tools
  5. install MacPorts – http://guide.macports.org/
  6. activate Mac OS Terminal window to get $ prompt and so run MacPorts
    • $ sudo port install py-spyder
    • $ sudo port -f activate python27
    • $ sudo port install py-spyder
    • $ sudo port uninstall py27-matplotlib-basemap
    • $ sudo port install py27-matplotlib-basemap
    • $ sudo port install py27-netcdf4
  7. to run spyder form Mac OS Terminal
    • $ spyder-2.7 &
  8. to cleanup spyder workspace
    • $ rm -R .spyder2-27/