1

I have data produced by the following code:

u2 := y^2 + ϕ + 1

ϕ = 0.1;                 
aa1 = Table[{y,u2}, {y, -1.2, 1.2, 0.05}]

ϕ = 0.15;                                        
aa2 = Table[u2, {y, -1.2, 1.2, 0.05}]

ϕ = 0.20;                                          
aa3 = Table[u2, {y, -1.2, 1.2, 0.05}]

I want to save this data in a single file like this

y      u2 (output form aa1)        u2 (output form aa2)        u2 (output form aa3)
-1.2      2                           3                           4
-1        3                           4                           3
 0        2                           2                           2

The full code can be found here

https://mathematica.stackexchange.com/questions/31027/how-to-export-data-from-listplot

zhk
  • 11,939
  • 1
  • 22
  • 38
  • 2
    I'm sure this question is a duplicate but there are so many questions about exporting data that I can't find it yet. Anyway, you would use something like this: data = Transpose[{aa1,aa2,aa3}]; Export["myfile.txt", data, "Table"] – Mr.Wizard Aug 26 '13 at 06:03
  • But why the output have curly brackets?

    {-1.2, -0.9848413481084042} -0.9823585198403731 -0.9795411388515629

    – zhk Aug 26 '13 at 06:08
  • 1
    Please provide a short, self-contained example. I do not wish to load or read through whatever u2 is from the other question. I edited your question to make it more readable, hopefully while preserving the intent. Please use the same formatting in your own edits. – Mr.Wizard Aug 26 '13 at 06:13
  • 1
    suppose u2 = y^2+ϕ+1 where y varies from 0 to 2 and ϕ=0, 0.1, 0.2 – zhk Aug 26 '13 at 06:15
  • Alright, I'll add that to the question. – Mr.Wizard Aug 26 '13 at 06:16

2 Answers2

4

Based on your clarified question I believe you want this:

dat1 = Flatten /@ Transpose[{aa1, aa2, aa3}];

Export["myfile.txt", dat1, "Table"]

Just for fun you could also use:

dat1 = ArrayFlatten[{{aa1, List /@ aa2, List /@ aa3}}, 2]

If you choose to use a different method to generate the data as m_goldberg suggested I propose a different formulation:

u[y_, phi_] := y^2 + phi + 1

yVals = Range[-1.2, 1.2, 0.05];

phi   = Range[.1, .2, .05];

uVals = Outer[u, yVals, phi];

data = Join[{yVals}\[Transpose], uVals, 2]

In this particular case u is a naturally Listable function, therefore we have even faster alternatives such as I used for How to write values of function to file?

uVals = u[yVals, #] & /@ phi

data = Join[{yVals}, uVals]\[Transpose]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • This question is answered in this "question". But maybe this can be renamed to "Exporting a table" for future visitors, since I haven't found and real duplicate and in documentation center there is no direct answer on top. – Kuba Aug 26 '13 at 06:23
  • @Kuba This may be answered there (I don't know; I didn't read it all) but it seems a lot more complicated than this simple question; I don't what to close this as a duplicate of that. – Mr.Wizard Aug 26 '13 at 06:26
  • Thx Mate, this exactly what I was looking for. – zhk Aug 26 '13 at 06:27
  • I don't want to close it too because it may be simple but it is not easily found in documentation or here. That's why I suggest edit the title to make it easily found here for future visitors who want to export the table :) – Kuba Aug 26 '13 at 06:31
  • @Kuba I'm pretty sure I've seen this question before; I just need to find the original. – Mr.Wizard Aug 26 '13 at 06:33
2

Here is one way to build your output table that works for any number of y-values and phi-values.

u[y_, phi_] := y^2 + phi + 1
yVals = Table[y, {y, -1.2, 1.2, 0.05}];
uVals = Table[u[y, phi], {y, -1.2, 1.2, 0.05}, {phi, .1, .2, .05}];
data = MapThread[{#1, Sequence @@ #2} &, {yVals, uVals}];

Here is the first five lines of the output table:

data[[;; 5]] // TableForm

table.png

If the last step using MapThread and Sequence seems obscure, here is a perhaps clearer expression using Table and Flatten that also works.

data = Table[Flatten@{yVals[[i]], uVals[[i]]}, {i, Length@yVals}];
m_goldberg
  • 107,779
  • 16
  • 103
  • 257