1
$\begingroup$

For example, say I wanted to plot Sin(x) like this:

Plot[Sin[x],{x,0,2 Pi}]

But instead of plotting to a graph, I want to now tabulate the values of Sin[x] to a data file. How do I achieve this?

EDIT: Sorry I wasn't clear enough, I mean plot the x values in one column and the y values in the second column

$\endgroup$
3
  • $\begingroup$ Well, that depends: do you want equal spacing of points, or do you want to make use of mathematica's adaptive sampling? $\endgroup$
    – Ajasja
    Jun 19, 2012 at 10:23
  • $\begingroup$ Just equal spacing for now $\endgroup$
    – Eddy
    Jun 19, 2012 at 10:32
  • $\begingroup$ See the update to my question for { x, Sin[x] } values together as per your question update. $\endgroup$ Jun 19, 2012 at 10:58

3 Answers 3

7
$\begingroup$

First create your points, running from 0 to 2 Pi at intervals of 0.01.

points = Table[{x,Sin[x]}, {x, Range[0, 2 \[Pi], .01]}];

You can plot them with:

ListLinePlot[points]

Mathematica graphics

And export them to a csv file with:

Export["points.csv", points]
$\endgroup$
2
  • $\begingroup$ I think he need something like this:Table[{x,Sin[x]},{x,xmin,xman,dx}]//Transpose $\endgroup$
    – yulinlinyu
    Jun 19, 2012 at 16:56
  • $\begingroup$ @yulinlinyu Perhaps, I interpreted the OPs use of "column" to mean that the output file should contain 2 columns. If they export the transposed data it will give them two rows and many columns. $\endgroup$ Jun 19, 2012 at 17:12
8
$\begingroup$

You can select the points from the plot and write it to file with Export:

pl = Plot[Sin[x], {x, 0, 2 Pi}];

The Points are arguments to a Line directive in the Plot function:

Position[pl, Line]

output is: {{1, 1, 3, 2, 0}} the zero is the Head of the Line directive, the points are the argument:

points = pl[[1, 1, 3, 2, 1]];

Alternatively you can also produce the {x,y} pairs directly:

points = Table[Sin[x], {x, 0, 2 Pi, 0.1}];

Now I use export to write it all to a file

Export[< output.file >, points, "Table"]

Please note, that there is a typo in your code. You should use square brackets to give function argumens: Sin[x] instead of Sin(x)

Edit:

To get {x,y} pairs from a list of x-positions you can for instance use:

xypairs = {#, Sin[#]} & /@ points

/@ is a shorthand notation for Map. To calculate it directly in the Table command replace Sin[x] by {x,Sin[x]}.

Here is another method to get the points directly from the plot using EvaluationMonitor:

{pl, points} = 
  Reap[Plot[Sin[x], {x, 0, 2 Pi}, 
    EvaluationMonitor :> Sow[{x, Sin[x]}]]];
$\endgroup$
2
  • $\begingroup$ How do I get the x values in one column and the y values in another column? All I get are the y values $\endgroup$
    – Eddy
    Jun 19, 2012 at 10:52
  • $\begingroup$ @Eddy I updated the answer for the {x,y} pairs. $\endgroup$ Jun 20, 2012 at 8:09
2
$\begingroup$

This is to show some of Mathematica's other features.

create a list of x values:

x = Range[0, 2 Pi, 0.1];

create a list of y values:

y = Sin @ x;

(Sin is Listable; if it were not you would use Map: Sin /@ x. Also see Function.)

Put them together:

data = {x, y}\[Transpose];

The Transpose symbol is entered EsctrEsc.

Export data in whatever format you desire, e.g.:

ExportString["datafile.txt", data, "Table"]
$\endgroup$
1
  • $\begingroup$ Just a little comment that there's not need to use the transpose superscript symbol there--although it does look nice in a notebook. A straightforward Transpose[{x,y}] will produce the same result. $\endgroup$
    – murray
    Jun 20, 2012 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.