# Formatting exported data

I have simple code which goes like this:

ClearAll["Global*"]

et = 1 + 34/(k^2 3);
pt = (2 (1 + (\[Omega] 3)/(Sqrt[2] k)))/k^2 + (
1 + 2/(\[Omega] Sqrt[2] k))/k^2 + (1 + \[Omega]^2/(Sqrt[2] k))/k^2;
equation = NSolve[et + pt == 0, \[Omega]];
dattta = Table[equation, {k, 0, 2}]
Export["nice.dat", dattta]


I have some questions in this regard.

1) How can I format the output? As you cn see from the o/p dat file that all are kind of mixed. What if I want real part in the first column, then imaginary part in the next column?

2) How can I put heading in the output file? For example, I want to write at top of first column "k" and at top of second column I want to write "$\omega$"?

3) If you take example of any other programming language like for eg. MATLAB, the output file by default goes into the folder where the parent program is (if you want to change the path then you have to give the path of the folder). But that is not the case with Mathematica. Is there to get around of this issue?

• Regarding your third point, you can use SetDirectory[NotebookDirectory[]] which sets the current working directory to wherever your parent notebook file is located. Feb 7 '17 at 6:17
• The default directory setting is system dependent. Ultimately manually setting using NotebookDirectory[] is the way to go but if you want to discuss further you should say what system. Feb 7 '17 at 15:05
• Many thanks @Hubble07 ... This was the thing that I wanted... Feb 8 '17 at 4:20
• @george2079 , as it turns out that NotebookDirectory[] gives you the current directory and to put the programme in the directory or the folder you have to use SetDirectory[NotebookDirectory[]] as Hubble07 pointed out. Feb 8 '17 at 4:23

Get the \[Omega] values first by using ReplaceAll (/.) and then Flatten your data. Then, use Map (/@) to map ReIm function onto the elements of data.

data = \[Omega] /. Table[equation, {k, 0, 2}] // Flatten;
data1 = ReIm & /@ data
(*{{0.0272794, -0.574095}, {-6.05456, 2.22045*10^-16}, {0.0272794,
0.574095}, {-0.0885931, 0}, {-2.9557,
3.72007}, {-2.9557, -3.72007}, {-0.0367215, 0}, {-2.98164,
6.75083}, {-2.98164, -6.75083}}*)


To put a heading, use Join

dataToBeExported = Join[{{"k", "\[Omega]"}}, data1]


Now you can export it.

Export["nice.dat", dataToBeExported ]

• In version 10.1 and later the definition of data1 can be simplified to data1 = ReIm /@ data Feb 7 '17 at 5:36
• @BobHanlon Thank you. Feb 7 '17 at 5:45
• Thanks @AnjanKumar for the timely help, especially in revelaing to me the command Join and its usage. But could you please explain to me what is ReIm? Feb 8 '17 at 4:17
• @sreerajt ReIm[z] gives out {Re[z], Im[z]}`, where z is a complex number. Feb 8 '17 at 4:20