Exporting-Importing-and-Referencing-Data.nb

Getting data into and out of a Mathematica notebook

Vince Cronin, March 31, 2021

How do I create an export input line so data from my Mathematica notebook can fill an external file on my computer?

The following method is, admittedly, a little quirky.  There is almost certainly a better way that smarter people use, but at least this method works.

1.  Select the folder/directory in your computer where you want the export file to land.  If there are no files in the folder, make a file called something like “temp” and put it in the folder.

2.  Choose a useful name that you will use for the file you will export to your computer. within the Mathematica notebook.  For this example, let’s use the variable name Results.csv  for a comma-separated-values file that can be read by a text editor or spreadsheet application.

2.  Somewhere below the point in your code where the data that you want to export will be generated, type the following as an input line:  Export[];  
You can make sure it is an input line by clicking on that line’s bracket on the far right edge of this window, going to the Format menu, selecting the Style submenu, and then choosing Input

3. Put your cursor between the square brackets in the Export[] statement and click to establish the insertion point we will need in the next step.  The Export[] function requires two arguments:  the path to the destination file on your computer, and the variable name of the dataset within the Mathematica notebook.  These two arguments are separated by a comma.
   (a)  Between the square brackets in the Export[] statement, type a comma and the variable name of the dataset from the Mathematica notebook.  
   (b) Move the insertion point to just before the comma within the square brackets of the Export function.
   (c) Go to the Insert menu, select File Path, navigate to a file (perhaps the “temp” file) in the folder where you want to export the file and choose it.  The path to that file will be inserted at the cursor.  Then you need to replace that file name (the last part of the path) with the filename you have chosen for the export files -- in this case, Results.csv.  The path will be different for every different file, and on every different computer.

The following is an example of how the export line might look as we try to move a dataset called outfile1 onto my computer’s desktop.
Export[“/Users/vince/Desktop/Results.csv”, outfile1]

Example:

outfile1={{10, 11},{12,13},{14,15},{16,17},{18,19}};

Export["/Users/vince/Desktop/Results.csv", outfile1];

A new CSV file called Results.csv will be created on my computer’s desktop whose contents look like this (as viewed in a generic text editor).

Files can be exported in a variety of formats, including the ever-mysterious “DAT” format (dat), table formats (csv, tsv, xls), text (txt), graphics formats (e.g., eps, pdf, svg, pict, wfm, tiff, gif, jpeg, png, bmp, pcx, xbm, pbm, ppm, pgm, pnm, dicom, avi), and other specialized data formats (e.g., dif, fits, hdf5, mps, sdts, et cetera) .

For more information about how the Export[] function works, go the the Help menu in Mathematica and select Find Selected Function.  You can then type the function name into the search bar at the top of the window.

Your turn ...

In the yellow input cell below, complete the Export line so that the file outfile1 that was created above is exported to your computer .  Use the example above as your guide.  Then execute the code .

Export[,outfile1];

How do I create an import input line so data from an external file can load into my Mathematica notebook?

1.  Start by choosing a useful name that you will use for the datafile within the Mathematica notebook.  For this example, let’s use the variable name importData.

2.  Having decided on a name for the datafile, type the following as an input line:  importData=Import[];  You can make sure it is an input line by clicking on that line’s bracket on the far right edge of this window, going to the Format menu, selecting the Style submenu, and then choosing Input

3. Put your cursor between the square brackets in the Import[] statement and click to establish the insertion point we will need in the next step.

4. Go to the Insert menu, select File Path, navigate to the correct input data file and choose it.  The correct file path will be inserted at the cursor.  In this example, a CSV file called “Results.csv” is located somewhere on your computer, and so Mathematica will insert the file path between the brackets.  The file path will be different for every different file, and on every different computer.

Example:

importData = Import["/Users/vince/Desktop/Results.csv"];

The variable importData will be defined as the contents of the CSV file called Results.csv and will look like this:

importData

{{10, 11}, {12, 13}, {14, 15}, {16, 17}, {18, 19}}

Your turn ...

In the yellow input cell below, complete the Import line so that we can grab the file Results.csv from your computer .  Use the example above as your guide.  Then execute the code .

importData=Import[];

Check the data after it is imported to see if it is in the right form for you to use

I usually check the imported datafile in one of two ways after it has been imported.  

Way 1.  List the output by typing the name of the datafile and omit the semicolon that would suppress the listing.  When you evaluate the notebook again, the whole datafile will be printed.  If the datafile is very large, Mathematica will try to give you a sample of it and ask you what you want to do.

Way 2.  Find out the dimensions of the datafile using the built-in Mathematica function Dimensions[].  In this case, we would add an input line with the following content:
Dimensions[importLocData]
When the notebook is evaluated, it will provide one or more numbers in a set, where the first number indicates how many elements are in the top level of a nested list of data.  
(a) If there is only one number returned by the Dimensions[] function, that means the datafile is one single list like {1,2,3,4,5,6}.  The top level of the datafile is the only level, and so this is not a nested list of data.
(b) If there are two numbers, the dataset has a list that contains one sort of sublist, like {{1,2},{3,4},{5,6}}

Example

datafile1={1,2,3,4,5,6,7,8,9,10};

Dimensions[datafile1]

Explanation.  The output of function Dimensions[] tells us that datafile1 has 5 elements in a single list.

datafile2={{1,2},{3,4},{5,6},{7,8},{9,10}};

Dimensions[datafile2]

Explanation.  The output of function Dimensions[] tells us that datafile2 is structured with 2 levels (i.e., there are 2 elements in the list returned by the function Dimensions[]). The top level has 5 lists, and each of these lists has 2 elements.  This would be a good structure if the input datafile contained latitude and longitude data for several points.  The geographic coordinates for each point would be a 2-element list at the lower level of the imported datafile, contained within curly brackets, and the higher level of the datafile would be a list of lists.

datafile3={{{1,2},{3,4},{5,6},{7,8},{9,10}}};

Dimensions[datafile3]

Explanation.  The output of function Dimensions[] tells us that datafile3 is structured with 3 levels (i.e., there are 3 elements in the list returned by the function Dimensions[]). The top level has 1 list, the second level has 5 lists, and each of these lists has 2 elements.  This datafile seems to have one too many dimensions.

What do I do if my imported dataset has too many dimensions

The built-in Mathematica function Flatten[] can be used to remove unwanted dimensions in your imported dataset.

datafile4={{{1,2},{3,4},{5,6},{7,8},{9,10}}};

Dimensions[datafile4]

The following example changes datafile4 into a single 1-dimensional (1-level) list of values

datafile5=Flatten[datafile4]

Dimensions[datafile5]

The following example strips off the top level of datafile4 to form a 2-dimensional (2-level) datafile

datafile6=Flatten[datafile4,1]

Dimensions[datafile6]

For more information about how the Flatten[] function works, go the the Help menu in Mathematica and select Find Selected Function.  You can then type the function name into the search bar at the top of the window.

How do I reference an individual element of a nested table like datafile4?

datafile4

Dimensions[datafile4]

The file datafile4 has 3 levels (or layers or dimensions).  For example, the “6” is the second value of the third list in the second level.  The “coordinates” in the double square brackets are listed from the highest to the lowest levels.  There is only one list in the highest level, five lists in the second level, and the contents of each of those five lists constitute the third level.  The target is in the (only element of the) first layer, the third element of the second layer, and the second element of the third layer.

datafile4[[1,3,2]]

How do I reference an individual element of a nested table like datafile5?

datafile5

Dimensions[datafile5]

The file datafile5 has 1 level (or layer or dimension).  For example, the “6” is the sixth value of the first (and only) level.

datafile5[[6]]

How do I reference an individual element of a nested table like datafile6?

datafile6

Dimensions[datafile6]

The file datafile6 has 2 levels (or layers or dimensions).  For example, the “6” is the second value of the third list in the first level.  The “coordinates” in the double square brackets are listed from the highest to the lowest levels.  There is only one list in the highest level, five lists in the second level, and the contents of each of those five lists constitute the third level.  The target is in the (only element of the) first layer, the third element of the second layer, and the second element of the third layer.

datafile6[[3,2]]