5

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!

More info: the code for M is:

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).

g3kk0
  • 3,318
  • 17
  • 37
Susan
  • 93
  • 7

2 Answers2

4

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"
g3kk0
  • 3,318
  • 17
  • 37
4

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]} *)
Yves Klett
  • 15,383
  • 5
  • 57
  • 124
  • 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
  • I know that this is an old question but I just had the same issue. The answer of @g3kk0 is perfect using the mx trick, however, sometimes we need the data in a dat format for processing. Therefore, I think that by exporting the data as a real numbers and then importing and converting to complex also may be usefull. – Pierre Pantaleon Nov 26 '23 at 11:10