Question

I want to use some Python libraries to replace MATLAB. How could I import Excel data in Python (for example using NumPy) to use them?

I don't know if Python is a credible alternative to MATLAB, but I want to try it. Is there a a tutorial?

Was it helpful?

Solution

Depending on what kind of computations you are doing with MATLAB (and on which toolboxes you are using), Python could be a good alternative to MATLAB.

Python + NumPy + SciPy + Matplotlib are the right combination to start.

For the data, you can, for example, save your data directly in text file (assuming that you are not directly concerned by floating-point precision issues) and read it in Python.

If your data are Excel data, where each value is separated by a ";", you can for example read the file line by line, and use the split() method (with ";" as argument) to get each value.

For MATLAB up to version 7.1, it is possible to directly load .mat files from Python with the scipy.io.matlab.mio module.

OTHER TIPS

There's Matplotlib for plots and the csv module for reading Excel data (assuming you can dump to CSV).

Here's a tutorial about replacing MATLAB with Python.

If you come from the MATLAB world, Pylab will ease your transition. Once you have converted your data to ASCII, pylab.load() will do the rest:

pylab.load(fname, comments='#', delimiter=None, converters=None, 
           skiprows=0, usecols=None, unpack=False, 
           dtype=<type 'numpy.float64'>)

There are probably hundreds of ways you could import text data into Python.

But since you want to replace MATLAB, you're going be using NumPy and probably SciPy.

Keep things simple: use NumPy's standard text-loading:

import numpy
imported_array = numpy.loadtxt('file.txt',delimiter='\t')  # Assuming tab-delimiter
print imported_array.shape

Pandas is a Python data analysis library that can import/export from Excel pretty easily. Here's how to do it:

http://pandas.pydata.org/pandas-docs/stable/10min.html#excel

Crash course:

import pandas as pd
data = pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])

I had a look at mlabwrap as a step to easing some MATLAB developers into using Python more.

But I have been unable to cleanly build it, and I don't run production installs here, so I'm dead in the water.

If you saved you data in MATLAB format, use:

from scipy.io import loadmat

datafile = "yourfile.mat"
data = loadmat(datafile, matlab_compatible=True)
var1 = data['nameOfYourVariable'].squeeze()
var2 = data['nameOfYourOtherVariable'].squeeze()

"I don't know if Python is a credible alternative to MATLAB"

For me (experimental physics) Python is not only a full replacement for MATLAB (when including SciPy and Matplotlib as mentioned above), but it is useful for many things other than data crunching and visualisation (such are general programming needs).

"I'm going to try SAGE."

It is worth noting that there are a couple of servers running Sage which offer the notebook environmet (check Try Sage online in http://www.sagemath.org/). This is pretty neat given the fact that all you need it is an Internet browser and access (no installation required).

As for the question as interpreted by Kevin Buchs (in another answer), reading proprietary Excel to Python can be done in several methods, some are platform (OS) dependent:

  1. A nice resource (platform independent) - http://www.python-excel.org/
  2. An example using xlrd which I once found useful (this is what I used when I needed it): http://code.activestate.com/recipes/483742/ for an example based on xlrd (platform independent)
  3. pyexcelerator is another option.

I hope this helps. If not, I can try to arrange some example code myself (though the ones I have are over six years old...). I personally prefer, as was proposed in the other answers, to use the CSV or ASCII format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top