Software tutorial/Loading data in MATLAB or Python

From Process Model Formulation and Solution: 3E4
Jump to: navigation, search
← Least squares modelling (linear regression) (previous step) Tutorial index

A number of people asked me how to load data into MATLAB and Python after our class on the poor ability of spreadsheets to do serious engineering calculations.

Here are some functions that you can use in either language.

File format MATLAB Python

Excel files

xlsread(...) and xlswrite(...)

Can read both the text and values in an Excel file. Cell containing formulas are imported as the result of the formula value.

The best Python libraries at the moment are xlrd and xlwt for reading and writing Excel files. See the xlrd and xlwt website for more details and a tutorial.

They are not built-in libraries, so you will have to easy_install them separately.

Comma-separated value (CSV) files are a standard format for many years and can be read on any system, since they are pure text files. However, being pure text they are an inefficient form for very large data sets. CSV files use a comma (,) as the delimiter (separator), but sometimes people will use spaces, tabs (\t) or other symbols.

csvread(...) and csvwrite(...) only support the comma delimiter.

If you have other delimiters, such as a space or a tab, you must use dlmread(...) and dlmwrite(...)

Python has built-in support for CSV files and allows you to specify the delimiter (it is a comma by default!)
import csv
filename = 'my_data_file.csv'
delimiter = ' '  # import a file with space delimiters
data = []
for row in csv.reader(open(filename), delimiter=delimiter):

# Various commands that manipulate the data follow
XML files (eXtensible Markup Language) are rapidly becoming a fairly standard import and export format for data, because it describes both the data and the nature of the data in the file. New versions of Excel, Word and Powerpoint are actually just XML files that as zipped up (which is why you have the x in the .docx file extension).

xmlread(...) and xmlwrite(...).

XML files, and other markup languages are very well supported in Python. You can read more in the official Python documentation.

← Least squares modelling (linear regression) (previous step) Tutorial index