Mathematica Commands for Importing and Exporting Files

This article describes several commands related to importing and exporting data taking care of specific file structures. Various manipulations of the data are also discussed.

Lets begin with clearing all variable definitions of the current Mathematica session.

Clear["Global`*"]

Importing Data File with Header and Footer Text

Our first goal is to import data from a file which contains, lets say, 3 columns; first column is the X-data whereas 2nd and 3rd columns contain Y-data. There is also some Header and Footer text which we need to skip while importing. An example file "DataFile.txt" is shown below.

---------------------------------------------------

Test data

Xdata Ydata1 Ydata2

1 1 1.000

2 4 1.414

3 9 1.732

4 16 4.000

Created on Nov 23, 2016

---------------------------------------------------

HeaderLineNum = 2;
FooterLineNum = 1;
TotalColNum = 3;
file = OpenRead["C:\\Users\\TANAY\\Desktop\\DataFile.txt"];
Skip[file, String, HeaderLineNum];
data = ReadList[file, Table[Number, {i, 1, TotalColNum}]][[;; -(FooterLineNum + 1)]];
Close[file];
Output: {{1, 1, 1.}, {2, 4, 1.414}, {3, 9, 1.732}, {4, 16, 4}}

Select Different Columns from Imported Data

To select individual columns use the following commands:

xdata = data[[All, 1]];
ydata1 = data[[All, 2]];
ydata2 = data[[All, 3]];

To select individual rows use the following commands:

row1 = data[[1, All]];
row2 = data[[2, All]];

To select two columns from the imported data use the following commands:

data12 = data[[All, {1, 2}]]; (* Column 1 and 2 *)
data13 = data[[All, {1, 3}]]; (* Column 1 and 3 *)

To select all rows of columns 1 through 3 use the following commands:

data1to3 = data[[All, 1 ;; 3]] (* Column 1 to 3 *)

One can use these reconstructed arrays to make a plot:

ListLinePlot[{data12, data13}]; (* Output: two line; not shown *)

Export Data to A File

In order to write xdata and ydata as two rows in the file "OutputFile.txt" use the following command:

Export["C:\\Users\\TANAY\\Desktop\\OutputFile.txt", {xdata, ydata1}, "Table"]

OutputFile:

1 2 3 4
1 4 9 16

In order to write xdata and ydata as two columns in the file "OutputFile.txt" use the following command:

Export["C:\\Users\\TANAY\\Desktop\\OutputFile.txt", Transpose[{xdata, ydata1}], "Table"]

OutputFile:

1 1
2 4
3 9
4 16

Important Note: the "Table" option is important to get rid of the extra curly brackets. Other useful option is "CSV".

Generate and Export Multi-column Data to a *.xlsx File

Lets first generate a 2D array and store it into variable "arr2d":

arr2d = {};
For[i = 0, i <= 3, arr2d = Join[arr2d, {{i, i^2, N[Sqrt[i]]}} ]; i = i + 1;]
Print[arr2d]
Output: {{0,0,0.},{1,1,1.},{2,4,1.41421},{3,9,1.73205}}

To save the data into a .xlsx (or .xls) format use the following command:

Export["C:\\Users\\TANAY\\Desktop\\OutputFile.xlsx", arr2d];

OutputFile:

Setting Working Directories and Searching Files

To know the current working directory use:

Directory[]
Output: C:\Users\TANAY\Documents

To set current working directory to a different location use:

SetDirectory["C:\\Users\\TANAY\\Desktop"]
Output: C:\Users\TANAY\Desktop

To set current working directory to the folder where the Mathematica notebook resides:

SetDirectory[NotebookDirectory[]];

In order to find all files (including directories) in the working directory use:

FileNames[]

In order to find all txt files that start with letters "C" or "D" in the working directory use:

files = FileNames[{"C*.txt", "D*.txt"}];

In order to find all directories that contain the letter "C" in the working directory use:

folders = Select[FileNames["*C*"], DirectoryQ];

Other Useful Commands

To know the kernel time used use:

TimeUsed[]

To create a constant array of length 11 with all elements being 0 use:

ConstantArray[0, 11]

To rotate an array by 2 units use:

RotateRight[array,2]
RotateLeft[array,2]