Example 15
Importing and Manipulating
 Multi-Dimensional Data Files as Lists

Copyright „ 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

1.  Using  !!  to read a file without storing it.
2.  List functions:  Partition[],  Transpose[],  and  Length[]
3.  Extended argument for  ReadList[].
4.  Round[]  to convert a real to an integer.
5.  ScatterPlot3D[]  in the package  Graphics`Graphics3D`.

Problem

a.  Use a text editor or a word processor to create an ASCII data file containing the following set of nine  (t, v, x)  points.

0.0   1.0   0.0
	0.8   0.7   0.7
	1.6   0.0   1.0
	2.4  -0.7   0.7
	3.1  -1.0   0.0
	3.9  -0.7  -0.7
	4.7   0.0  -1.0
	5.5   0.7  -0.7
	6.3   1.0   0.0

b.  Read this data file into MMA and store it as a list of three dimensional points.

c.  Produce a list of just the  {t, x}  values and plot the points on a two dimenisonal graph.

d.  Produce a list of just the  {t, v}  values and plot the points on a two dimensional graph.  Have your plot show both the points and the lines connecting the points.

Solution - Part a

Step 1 - Use a text editor or word processor to create an ASCII text file containing the desired three dimensional data.  Arrange the data, using tabs and/or spaces, into three columns and nine rows.  Save the data to the file  data2.dat.

Step 2 - Verify that your data file can be read by MMA by using the  !!  operator followed by the full path name of your file.  Do not enclose the file name in double quotes.

Comment 2

1.  The columns will line-up in MMA only if you use the same number of tabs in each row to separate the column items.  But then the columns may not line up in the document as read by your text editor or word processor. 
2.  The  !!file  command only shows the data in the file.  It doesn't store the results in MMA; in particular, you can't assign a name to the output associated with this command.  Try it and see what happens.

Solution - Part b

Step 1 -  Use  ReadList[]  to load  data2.dat  into MMA  as a set of reals.  However, only use one read type in  ReadList[]'s  argument.  Name the list  dataVer1.  What do you notice about the form of the final list produced by MMA?

Comment 1

1.  The list isn't partitioned into 3D points.
2.  MMA doesn't distinguish when reading in numbers between spaces, tabs, and new lines. 
3.  If the data had been stored with different separators, like commas, it would have been necessary to use the Word type in  ReadList[]'s argument and then the function  ToExpression[]  to convert the data into reals.

Step 2 -  One way to produce the desired list of 3D points is to partition the list  dataVer1.  Locate the MMA command that will do this and create the desired list, naming it  dataVer2.

Step 3 - Another way to accomplish the task in Step 2 is to produce the desired list when you first read in the file.  The second argument in  ReadList[]  can be an expression or function of one or more of the basic "types", for example, {Real, Real, Real}.  Use this technique to obtain the desired list  dataVer2.

Comment 3

1.  Which of the two methods described above do you like best?  Why?

Solution - Part c

Step 1 - You'll now create a list consisting of the points  {t, x}.  To do so, you'll use the MMA function  Transpose[].  Find out about this function and then active the cell below and look closely at the code and the output.

Clear[tempList]
tempList = Transpose[dataVer2]
tempList[[1]]
tempList[[3]]
Transpose[{tempList[[1]], tempList[[3]]}]

Step 2 - Create the list  dataTX  consisting of the points  {t, x}  and plot the results on a 2D graph.

Comment 2

1.  Large lists take up lots of memory and should be cleared once you're finished using them.  Under certain circumstances, MMA may crash if too much memory has been used up during a MMA session.

Solution - Part d

Step 1 - Create the list  dataTV  consisting of the points  {t, v}  and plot the results on a 2D graph.  Have your plot show both the points and lines connecting the points. (Hint:  produce two plots and then superimpose them using  Show[].)

Comment 1

1.  Did you use the option  DisplayFunction  to suppress the two separate plots before combining them with  Show[]?  If not, do so.

Solution - Part e

Step 1 - The three dimensional phase plot can be created in two ways.  The most direct way is to use the function  ScatterPlot3D[]  found in the package  Graphics`Graphics3D`.  Load this package, find out about the function  ScatterPlot3D[]  and apply it to the list dataVer2.

Step 2 - The code in the following cell uses  ParametricPlot3D[]  to create the desired 3D phase plot.  It also uses the commands  Length[]  and  Round[].  Find out about the new commands, look over the code, and then activate the cell.

Clear[tempList]
tempList = Transpose[dataVer2];

ParametricPlot3D[
  {tempList[[1, Round[s]]],
   tempList[[2, Round[s]]],
   tempList[[3, Round[s]]]},
  {s, 1, Length[dataVer2]},
  DefaultColor -> RGBColor[0, 0, 1],
  AxesLabel -> {"t", "x", "v"},
  PlotLabel -> "Three Dimensional Phase Plot",
  FaceGrids -> All];

Comment 2

1.  This technique using  ParametricPlot[]  doesn't allow individual points to be plotted.  How might you do this?
2.  The parameter  s  must be real in order for the command to work.  But  s  also needs to be an integer for indexing the list; thus the use of  Round[s]  in indexing  tempList.

Save Your Notebook

Remove any output cells from this notebook and then use  Save As  to store your notebook under the name   myEx15.