Example 14
Importing One Dimensional Data Files
and Formating Data

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

Topics and Skills

1.  Reading in a file with  ReadList[].
2.  Formating with  NumberForm[]  and its options  SignPadding, NumberSigns, and  NumberPadding.
3.  Formating with  ScientificForm[].

Problem

a.  Use a text editor or word processor to create the ASCII text file  data1.dat  containing the numbers

1.2,   0.34,   101.3,   6 ,   -2.6

b.  Read your data file into MMA assuming all the numbers are reals.

c.  Format the number  101.3  so it has a plus sign in front, and an extra  0  after the  3.

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.

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.

Solution - Part a

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.

1.  There's nothing special about the  .dat  extension.

Solution - Part b

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.

Comment 1

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?

Step 2 - Now read in your data file and name the resulting list  data1.

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.

Step 4 - Examine the options for  ReadList[]  and describe what you find.

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.

Comment 5

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.)

Solution - Part c

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[].

Comment 1

1.  There are a number of options.  You'll just investigate three of the options:  SignPadding,  NumberSigns,  and  NumberPadding.

Step 2 -  Name the number  101.3  myNum  and apply  NumberForm[]  to  myNum,  with  n  going from  2  to  5.  What do you find?

Comment 2

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?

Step 3 -  Try out the  NumberPadding  option, with  0  as the padding symbol.  What do you find?  What happens when  n  changes?

Comment 3

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.

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.

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.

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.

Solution - Part d

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.

Solution - Part e

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.

Step 2 - Repeat Step 1, but now left justified the numbers so that all the plus or minus signs line up.

Save Your Notebook

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