2008年3月11日 星期二

Import data from Excel files into Mathematica


If your dataset named test.dat with space delimited has the following contents (Iris dataset).

1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa

You can read this into Mathematica with only one command:

mydata = ReadList["~/iris.txt", {Number, Real, Real, Real, Real, String}] ;

Show the dataset

mydata // TableForm

Read the 2-3 column from the dataset.

iris1 = mydata[[Table[i, {i, 1, 50}], {2, 3}]]; iris2 = mydata[[Table[i, {i, 51, 100}], {2, 3}]]; iris3 = mydata[[Table[i, {i, 101, 150}], {2, 3}]];

Plot the Scatter Plot

Step 1.

Needs["Graphics`MultipleListPlot`"];

Step 2.

MultipleListPlot[{iris1, iris2, iris3}, SymbolShape -> Point, SymbolStyle -> {{PointSize[0.015], Blue}, {PointSize[0.015], Green}, {PointSize[0.015], Red}}, PlotLegend -> {"setosa", "versicolor", "virginica"}, LegendPosition -> {1.07, .3}, LegendSize -> {.3, .3}, PlotRange -> {{4, 8}, {2, 4.5}} ]



沒有留言: