I have simple code which goes like this:


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?

Thanks in advance...

  • 2
    $\begingroup$ Regarding your third point, you can use SetDirectory[NotebookDirectory[]] which sets the current working directory to wherever your parent notebook file is located. $\endgroup$
    – Hubble07
    Feb 7, 2017 at 6:17
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Feb 7, 2017 at 15:05
  • $\begingroup$ Many thanks @Hubble07 ... This was the thing that I wanted... $\endgroup$
    – sreeraj t
    Feb 8, 2017 at 4:20
  • $\begingroup$ @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. $\endgroup$
    – sreeraj t
    Feb 8, 2017 at 4:23

1 Answer 1


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 ]
  • 1
    $\begingroup$ In version 10.1 and later the definition of data1 can be simplified to data1 = ReIm /@ data $\endgroup$
    – Bob Hanlon
    Feb 7, 2017 at 5:36
  • $\begingroup$ @BobHanlon Thank you. $\endgroup$ Feb 7, 2017 at 5:45
  • $\begingroup$ 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? $\endgroup$
    – sreeraj t
    Feb 8, 2017 at 4:17
  • $\begingroup$ @sreerajt ReIm[z] gives out {Re[z], Im[z]}, where z is a complex number. $\endgroup$ Feb 8, 2017 at 4:20

Your Answer

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

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