Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

How to export data from listplot?

share|improve this question
1  
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 6:03
    
Related: (7030) –  Mr.Wizard Aug 26 '13 at 6:05
    
But why the output have curly brackets? {-1.2, -0.9848413481084042} -0.9823585198403731 -0.9795411388515629 –  MMM Aug 26 '13 at 6:08
    
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 6:13
1  
suppose u2 = y^2+ϕ+1 where y varies from 0 to 2 and ϕ=0, 0.1, 0.2 –  MMM Aug 26 '13 at 6:15

2 Answers 2

up vote 2 down vote accepted

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]
share|improve this answer
    
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 6: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 6:26
    
Thx Mate, this exactly what I was looking for. –  MMM Aug 26 '13 at 6: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 6: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 6:33

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}];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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