1
$\begingroup$

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

$\endgroup$
6
  • 2
    $\begingroup$ 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"] $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 6:03
  • $\begingroup$ Related: (7030) $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 6:05
  • $\begingroup$ But why the output have curly brackets? {-1.2, -0.9848413481084042} -0.9823585198403731 -0.9795411388515629 $\endgroup$
    – zhk
    Aug 26, 2013 at 6:08
  • 1
    $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 6:13
  • 1
    $\begingroup$ suppose u2 = y^2+ϕ+1 where y varies from 0 to 2 and ϕ=0, 0.1, 0.2 $\endgroup$
    – zhk
    Aug 26, 2013 at 6:15

2 Answers 2

4
$\begingroup$

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]
$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Aug 26, 2013 at 6:23
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 6:26
  • $\begingroup$ Thx Mate, this exactly what I was looking for. $\endgroup$
    – zhk
    Aug 26, 2013 at 6:27
  • $\begingroup$ 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 :) $\endgroup$
    – Kuba
    Aug 26, 2013 at 6:31
  • $\begingroup$ @Kuba I'm pretty sure I've seen this question before; I just need to find the original. $\endgroup$
    – Mr.Wizard
    Aug 26, 2013 at 6:33
2
$\begingroup$

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}];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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