(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";
MacintoshStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8, 24, "Times";
fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6, 18, "Times";
fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times";
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20, 18, "Times";
fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times";
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times";
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times";
fontset = input, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, 12, "Courier";
fontset = output, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-4, 12, "Courier";
fontset = message, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = print, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = info, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, 12, "Courier";
fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Times";
fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = leftheader, inactive, L2, 12, "Times";
fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, 12, "";
fontset = leftfooter, inactive, L2, 12, "Times";
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times";
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Courier";
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 14, "Times";
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
paletteColors = 128; automaticGrouping; currentKernel;
]
:[font = title; inactive; preserveAspect; startGroup]
Example 15
Importing and Manipulating
Multi-Dimensional Data Files as Lists
:[font = subsubtitle; inactive; preserveAspect]
Copyright ã 1993 by Bill Titus, Carleton College,
Department of Physics and Astronomy, Northfield, MN 55057-4025
September 6, 1993
;[s]
3:0,0;10,1;11,0;133,-1;
2:2,16,12,Times,2,14,0,0,0;1,16,12,Symbol,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Topics and Skills
:[font = special1; inactive; preserveAspect; endGroup]
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`.
;[s]
17:0,0;11,1;13,0;71,1;82,0;85,1;96,0;104,1;112,0;140,1;150,0;156,1;163,0;202,1;217,0;235,1;255,0;257,-1;
2:9,16,12,Times,0,14,0,0,0;8,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Problem
:[font = special1; inactive; preserveAspect]
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.
:[font = text; inactive; preserveAspect]
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
;[s]
3:0,0;1,1;152,0;153,-1;
2:2,16,12,Chicago,0,12,0,0,0;1,13,10,Courier,0,12,0,0,0;
:[font = special1; inactive; preserveAspect]
b. Read this data file into MMA and store it as a list of three dimensional points.
:[font = special1; inactive; preserveAspect]
c. Produce a list of just the {t, x} values and plot the points on a two dimenisonal graph.
;[s]
3:0,0;32,1;38,0;95,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
3:0,0;32,1;38,0;169,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part a
:[font = subsection; inactive; preserveAspect]
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.
;[s]
3:0,0;228,1;237,0;239,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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.
;[s]
3:0,0;69,1;71,0;176,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 2
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
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.
;[s]
3:0,0;229,1;235,0;429,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part b
:[font = subsection; inactive; preserveAspect]
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?
;[s]
9:0,0;15,1;25,0;36,2;45,0;113,1;123,0;153,1;161,0;233,-1;
3:5,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 1
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
5:0,0;263,1;273,0;308,1;322,0;356,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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.
;[s]
5:0,0;85,1;93,0;177,1;185,0;187,-1;
2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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.
;[s]
7:0,0;143,1;153,0;238,1;256,0;306,1;314,0;316,-1;
2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 3
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
1. Which of the two methods described above do you like best? Why?
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part c
:[font = subsection; inactive; preserveAspect; startGroup]
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.
;[s]
5:0,0;60,1;66,0;108,1;119,0;227,-1;
2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = input; preserveAspect; endGroup]
Clear[tempList]
tempList = Transpose[dataVer2]
tempList[[1]]
tempList[[3]]
Transpose[{tempList[[1]], tempList[[3]]}]
:[font = subsection; inactive; preserveAspect]
Step 2 - Create the list dataTX consisting of the points {t, x} and plot the results on a 2D graph.
;[s]
5:0,0;26,1;32,0;60,1;66,0;104,-1;
2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 2
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
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.
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part d
:[font = subsection; inactive; preserveAspect]
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[].)
;[s]
7:0,0;26,1;32,0;60,1;66,0;233,1;239,0;242,-1;
2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 1
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
1. Did you use the option DisplayFunction to suppress the two separate plots before combining them with Show[]? If not, do so.
;[s]
5:0,0;28,1;43,0;108,1;114,0;132,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part e
:[font = subsection; inactive; preserveAspect]
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.
;[s]
9:0,0;115,1;130,0;154,1;174,0;225,1;240,0;266,1;275,0;277,-1;
2:5,16,12,Times,1,14,0,0,0;4,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
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.
;[s]
7:0,0;46,1;64,0;131,1;139,0;146,1;153,0;237,-1;
2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0;
:[font = input; preserveAspect; endGroup]
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];
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 2
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
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.
;[s]
11:0,0;26,1;42,0;134,1;135,0;190,1;192,0;261,1;269,0;284,1;292,0;294,-1;
2:6,16,12,Times,0,14,0,0,0;5,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Save Your Notebook
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
Remove any output cells from this notebook and then use Save As to store your notebook under the name myEx15.
;[s]
5:0,0;57,1;64,0;106,1;112,0;114,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;
^*)