Tutorial 9: Importing and Exporting Data Into Mathematica
Tutorial 9: Importing and Exporting Data Into Mathematica
nb 1
ü Importing Data
In order to inport the data, you need to properly format the text file with the correct end-of-line character and delimiter.
Below is one method to properly format the text file, I use here a space delimiter with an end-of-line command after each
row. There are other ways to do this properly.
1) In origin, export your data with comma, space option. Export as *.dat file
2) In MsWord, find the comma and replace them with 1 space.
3) Save a *.dat file in Word using "Save .txt with line". This provide the correct format for the import into Mathematica
The data format is two columns by n rows. I use the function OpenRead to import the data.
f = OpenRead@"c:\Temp\FS_data.txt"D
data = ReadList@f, 8Number, Number<D;
Close@fD
InputStream@c:\Temp\FS_data.txt, 7D
c:\Temp\FS_data.txt
1
0.75
0.5
0.25
0
-0.25
-0.5
-0.75
-0.01 -0.005 0 0.005 0.01 0.015
ü Separate data
0.75
0.5
0.25
-0.25
-0.5
-0.75
One can also write arrays in Mathematica to text files as well. Here I will write ordered pairs to a file called "intal.tx" at
c:\. The first commman opens the file at path "f", the second writes the data to the path, and the third command closes the
path "f".
OutputStream@c:\Intal.txt, 4D
Do@
Close@fD
c:\Intal.txt
To get proper formatting using scientific notation, I use FortranForm for the output.
? FortranForm