# How to export a table of complex numbers?

I'm having problems exporting a table of complex numbers. I have:

tablevalues=Table[M[a,b],{b,1,101},{a,-500+5*(b-1),500+5*(b-1),5}];
Export["trial.dat",tablevalues,"Table"];


But then when I try to import the data using:

Data=Import["trial.dat","Table"];


I find that the Table has changed (later commands in my code no longer work, despite the fact that they do work if I use tablevalues directly).

Any help would be really appreciated, thanks!

M[a_, b_] := l1[a, b] + l2[a, b] -l3[a, b];


where l1, l2 and l3 are calculated for different a and b (the full expressions are quite long).

-
Could you also provide the code vor the symbol M? –  g3kk0 Jul 31 '13 at 9:57
I cannot find .dat in the list of possible export formats: http://reference.wolfram.com/mathematica/ref/\$ExportFormats.html. It works fine when using Mathematica .mx for example. –  g3kk0 Jul 31 '13 at 10:04
@g3kk0 I've tried with .mx but it still doesn't work? –  Susan Jul 31 '13 at 10:14
@g3kk0 ah, leaving out the "Table" with .mx does seem to work. Thanks! –  Susan Jul 31 '13 at 10:28
For a readable version of your data you can also use something like Export["trial.dat",tablevalues,"Package"] or simply Export["trial.m",tablevalues]. Importing is then easy, especially with Get. Seems slower than .mx though, but uses less space. –  Yves Klett Jul 31 '13 at 11:21
show 1 more comment

If you want to use the .dat format you should have a look at the tutorial http://reference.wolfram.com/mathematica/tutorial/ImportingAndExportingData.html.

Workaround 1

You can use Mathematica's .mx format to export and then import your data. You can have a look at the format description at http://reference.wolfram.com/mathematica/ref/format/MX.html.

M[a_, b_] := l1[a, b] + l2[a, b] - l3[a, b];
tablevalues =
Table[M[a, b], {b, 1, 101}, {a, -500 + 5*(b - 1), 500 + 5*(b - 1),
5}]; Export["trial.mx", tablevalues];

data = Import["trial.mx"];
data[[1, 1 ;; 5]]
(*{l1[-500, 1] + l2[-500, 1] - l3[-500, 1],
l1[-495, 1] + l2[-495, 1] - l3[-495, 1],
l1[-490, 1] + l2[-490, 1] - l3[-490, 1],
l1[-485, 1] + l2[-485, 1] - l3[-485, 1],
l1[-480, 1] + l2[-480, 1] - l3[-480, 1]}*)


Workaround 2

As Yves Klett pointed out, another nice workaround is to use:

Export["trial.m",tablevalues]


to store your data in a Mathematica package. Importing is then easy, especially with the Get function:

<<"trial.m"

-

After playing around a bit, I am surprised to find that this seems to be faster and uses far less diskspace than the .mx approach:

Export["trial.mx", tablevalues] // AbsoluteTiming
FileByteCount[%[[2]]]

(* {3.347335, "trial.mx"} *)
(* 7970752 *)

Export["trial.m.gz", tablevalues] // AbsoluteTiming
FileByteCount[%[[2]]]

(* {0.401040, "trial.m.gz"} *)
(* 140508 *)

data = Import["trial.m.gz"];
data[[1, 1 ;; 5]]

(* {l1[-500, 1] + l2[-500, 1] - l3[-500, 1],
l1[-495, 1] + l2[-495, 1] - l3[-495, 1],
l1[-490, 1] + l2[-490, 1] - l3[-490, 1],
l1[-485, 1] + l2[-485, 1] - l3[-485, 1],
l1[-480, 1] + l2[-480, 1] - l3[-480, 1]} *)

-
FWIW I mentioned this format here but I did not realize you could use the extension .m.gz directly without specifying the format. +1! –  Mr.Wizard Jul 31 '13 at 13:53
@Mr.Wizard interesting how the timings differ depending on content. Would you consider the question a duplicate, then? –  Yves Klett Jul 31 '13 at 15:43
No, I think it's reasonable to have a more focused question in this case. That post made no specific mention of complex numbers. –  Mr.Wizard Jul 31 '13 at 15:53
@Mr.Wizard Another thing is the timing - the .m takes forever and you can watch it being written incrementally on disk while the .gz really saves the day. Was not aware of that - might come in handy. –  Yves Klett Jul 31 '13 at 16:15