(*^
::[ 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 14
Importing One Dimensional Data Files
and Formating Data
:[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. Reading in a file with ReadList[].
2. Formating with NumberForm[] and its options SignPadding, NumberSigns, and NumberPadding.
3. Formating with ScientificForm[].
;[s]
13:0,0;28,1;38,0;60,1;72,0;91,1;102,0;104,1;115,0;122,1;135,0;157,1;173,0;175,-1;
2:7,16,12,Times,0,14,0,0,0;6,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Problem
:[font = special1; inactive; preserveAspect]
a. Use a text editor or word processor to create the ASCII text file data1.dat containing the numbers
;[s]
3:0,0;71,1;80,0;105,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Helvetica,0,14,0,0,0;
:[font = special1; inactive; preserveAspect]
1.2, 0.34, 101.3, 6 , -2.6
:[font = special1; inactive; preserveAspect]
b. Read your data file into MMA assuming all the numbers are reals.
:[font = special1; inactive; preserveAspect]
c. Format the number 101.3 so it has a plus sign in front, and an extra 0 after the 3.
:[font = special1; inactive; preserveAspect]
d. Format the number 101.3 in scientific notation so it has a plus sign in front, and six significiant figures altogether, some of which are zero.
:[font = special1; inactive; preserveAspect; endGroup]
e. Format the data from your file so that it's displayed in tabular form with three places after the decimal point and with all the numbers lined up with respect to the decimal point.
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part a
:[font = subsection; inactive; preserveAspect; startGroup]
Step 1 - Use a text editor or word processor to create the desired ASCII text file. Type the numbers in on separate lines with no commas. Then save the file as data1.dat.
;[s]
3:0,0;163,1;172,0;174,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
1. There's nothing special about the .dat extension.
;[s]
3:0,0;39,1;43,0;56,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Helvetica,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part b
:[font = subsection; inactive; preserveAspect]
Step 1 - You're going to use the MMA command ReadList[] to read your data into MMA. First find out about this command by typing ?ReadList.
;[s]
5:0,0;46,1;56,0;132,1;141,0;143,-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 1
:[font = special1; inactive; preserveAspect; endGroup]
1. What kind of structure will your data have within MMA?
2. By "file" MMA means the full file specification, including directories. On the NeXT, sequence of directories are separated by the backslash / while on the Mac, folders are separated by the colon :
3. "Type" can be one of the following: Number, Real, Byte, Character, String, Expression, Record, or Word. Which type should you use in this problem?
;[s]
5:0,0;205,1;207,0;262,1;263,0;428,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 2 - Now read in your data file and name the resulting list data1.
;[s]
3:0,0;65,1;70,0;74,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 3 - The separators in your data file could have been spaces or tabs, instead of new lines. Verify that this is true by editing your data file and then reactivating the cell in Step 2.
:[font = subsection; inactive; preserveAspect]
Step 4 - Examine the options for ReadList[] and describe what you find.
;[s]
3:0,0;34,1;44,0;74,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 5 - You could use different separators, like commas, in your data file if you import the data as words. However, since words are strings and not numbers, you would then need to convert the individual elements within the MMA list to reals using the MMA function ToExpression[]. Try this out.
;[s]
3:0,0;269,1;283,0;301,-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 5
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
1. The original list produced by ReadList[] using words looks like it consists of reals. How do you tell that the entries are actually words? (Hint: try adding something to the list.)
;[s]
3:0,0;35,1;45,0;190,-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 c
:[font = subsection; inactive; preserveAspect]
Step 1 - There are a number of MMA formatting commands which allow greater flexibility in how numbers are displayed. Start by getting extended help on NumberForm[].
;[s]
3:0,0;154,1;166,0;168,-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 1
:[font = special1; inactive; preserveAspect; endGroup]
1. There are a number of options. You'll just investigate three of the options: SignPadding, NumberSigns, and NumberPadding.
;[s]
7:0,0;83,1;94,0;97,1;108,0;116,1;129,0;131,-1;
2:4,16,12,Times,0,14,0,0,0;3,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 2 - Name the number 101.3 myNum and apply NumberForm[] to myNum, with n going from 2 to 5. What do you find?
;[s]
13:0,0;34,1;39,0;52,1;64,0;70,1;75,0;84,1;85,0;99,1;100,0;106,1;107,0;128,-1;
2:7,16,12,Times,1,14,0,0,0;6,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 2
:[font = special1; inactive; preserveAspect; endGroup]
1. You should find that n controls the number of significant figures, provided you don't claim more significant figures than occur.
2. If n is too small, an error message occurs. Does this message make sense?
;[s]
5:0,0;26,1;27,0;143,1;144,0;216,-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 3 - Try out the NumberPadding option, with 0 as the padding symbol. What do you find? What happens when n changes?
;[s]
7:0,0;23,1;36,0;52,1;53,0;117,1;118,0;129,-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]
1. If n is too small, a warning appears indicating that you may loose significant figures.
2. An extra zero appears in front. Why? It holds the place of the sign.
;[s]
3:0,0;9,1;10,0;173,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 4 - Suppose n equals 6, NumberPadding is in effect as in Step 3, and the number you're displaying is -myNum. What do you find? Why? Remove the problem by setting an appropriate option to True.
;[s]
10:0,0;18,1;19,0;30,1;31,0;34,1;47,0;112,1;120,0;201,1;207,-1;
2:5,16,12,Times,1,14,0,0,0;5,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 5 - Suppose you want to display myNum with a plus sign in front and no leading zeros. What option would you set? Do it.
;[s]
3:0,0;37,1;43,0;129,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; endGroup]
Step 6 - Now suppose you want to pad an extra 0 after the 3 to signify that there are really five significant figures in myNum. Eventhough it's not docummented in help, NumberForm[] has a list argument (that replaces n) of the form {n, f}, where n is now the total number of digit to be printed and f is the number of digits to the right of the decimal point. Use this information to print myNum to five significant figures, with a plus sign in front. You'll need to use both NumberSigns and NumberPadding as options.
;[s]
23:0,0;45,1;47,0;60,1;61,0;125,1;130,0;175,1;187,0;224,1;225,0;240,1;246,0;256,1;257,0;309,1;311,0;405,1;410,0;494,1;505,0;512,1;525,0;539,-1;
2:12,16,12,Times,1,14,0,0,0;11,15,11,Courier,1,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part d
:[font = subsection; inactive; preserveAspect; endGroup]
Step 1 - The MMA command for formating numbers in scientific notation is ScientificForm[]. Use this function to format myNum in scientific notation so it has a plus sign in front and is quoted to six significant figures.
;[s]
5:0,0;75,1;91,0;123,1;128,0;226,-1;
2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution - Part e
:[font = subsection; inactive; preserveAspect]
Step 1 - Format the list data1 so that it's displayed in tabular form with three places after the decimal point, with appropriate plus or minus signs before the numbers, and with all the numbers lined up with respect to the decimal point (i.e., the numbers are right justified). Note that MMA requires that you form the table before you apply the formating commands.
;[s]
3:0,0;27,1;32,0;373,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; endGroup]
Step 2 - Repeat Step 1, but now left justified the numbers so that all the plus or minus signs line up.
:[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 myEx14.
;[s]
4:0,0;57,1;64,0;106,1;114,-1;
2:2,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;
^*)