views:

2751

answers:

6

I want to use some Python libraries to replace MATLAB. I'd like to know 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. If you have any tutorial I'd be glad to read it!

Thanks a lot!

+3  A: 

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.

Hank Gay
SAGE (mentionend in the tutorial, see http://sagemath.org/) is a pretty cool stack based on Python.
stephan
Thanx a lot. I'm going to try SAGE.
clowny
+7  A: 

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

EDIT: for matlab up to version 7.1, it is possible to directly load .mat files from python with the scipy.io.matlab.mio module

ThibThib
+2  A: 

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'>)
wr
+2  A: 

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 elect to try mlabwrap and have any luck with it, please post your experiences, I'd like to hear about them.

John Pirie
+1  A: 

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
A: 

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()

jpi

JuanPi