Category Archives: Mathematica

Mathematica Memo – How to import multiple data files into a single variable.

So, you’ve measured something many times and need a simple way to import all the data into a Mathematica notebook. The method below works under the following assumptions:

  1. All data files are of the same format
  2. All data files have the same number of datum, arranged in the same manner (i.e. all containing the same number of rows and columns)
  3. You wish to import all files present in the folder, though this example can easily be edited to cherry pick files.

Set location of folder containing data

SetDirectory["/.../data/"];

Get the names of the files contained in the target folder

filenames = FileNames[]

Import multiple files into a nested list

alldata = Import /@ filenames;

Note: /@ is shorthand for Map[] which performs element wise operations.

For files containing a single column you get an n x m matrix (or nested list), for files containing multiple columns you get an n x m x p 3d matrix.

Errors will result if the number of datum in each file is not consistent, though they may not appear until you try to manipulate or act on the variable alldata (see mathematica mini-memo: element wise operations).