Tutorial9_ExportingImportingData.
nb 1
Tutorial 9: Importing and Exporting Data into Mathematica
This tutorial tells how to import and export data into Mathematica, which is unnecessarly difficult in Mathematica. Here,
I input a file call FS_data.txt which is located on the "c" drive at c:\Temp\FS_data.txt.
ü 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
Now I can plot the data
ListPlot@data, Frame −> True,
PlotRange −> 8All, All<, PlotJoined −> TrueD;
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
Now I wish to separate the data to just plot one column
Tutorial9_ExportingImportingData.nb 2
idata = Table@data@@i, 2DD, 8i, 1, 1024<D;
ListPlot@idata, Frame −> True,
PlotRange −> 8All, All<, PlotJoined −> TrueD;
0.75
0.5
0.25
-0.25
-0.5
-0.75
0 200 400 600 800 1000
ü Write Data to File
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".
f = OpenWrite@"c:\intal.txt", FormatType −> OutputForm D
OutputStream@c:\Intal.txt, 4D
Do@
" ", FortranForm@Re@intλ@@i, 2DDDD D, 8i, 1, num<D
Write@f, FortranForm@N@Re@intλ@@i, 1DDDDD,
Close@fD
c:\Intal.txt
To get proper formatting using scientific notation, I use FortranForm for the output.
? FortranForm
FortranForm@exprD prints as a Fortran language version of expr.