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

Problem

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.

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

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

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

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

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

Comment 3

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