5

The typical process goes as first one exports a numerical matrix mat

Export["test.dat",mat,"Table"]

and then imports it back by using

Import["test.dat","Table"]

This is necessary, especially when the evaluation of mat is relatively time-consuming and the value of mat is expected to be used many times.

When mat is a 1-dimensional vector or 2-d matrix, one gets what s/he wants (what is imported is what exactly has been exported). However, to one's surprise, the procedure fails when mat is a 3-d matrix (of Depth 4) --- what imported becomes many strings.

It has been found that *.mat file may do the job. But, is there any way to make *.dat to do the job as well?

  • 2
    worth a note, the "dat" extension has no bearing on what you did there (or any of the answers here). The last argument to Import/Export is a directive to ignore the extension. (the dat extension is so generic it is meaningless anyway) – george2079 Sep 17 '16 at 14:07

4 Answers4

7

Use Put and Get.

Having

l1 = {{a1, {a, b, c, d, e}}, {a2, {f, g, h, i}}}

one exports using

Put[l1,"out.dat"]

and imports next with

Get["out.dat"]

This works for arbitrary depth lists.

corey979
  • 23,947
  • 7
  • 58
  • 101
5
dat = RandomReal[10, {3, 3, 3}];
Export["test.dat", dat, "List"];
dat2 = ToExpression[Import["test.dat", "List"]];
Simplify[dat == dat2]

True

dat // MatrixForm
dat2 // MatrixForm

enter image description here

Feyre
  • 8,597
  • 2
  • 27
  • 46
4

If you just need to store an expression to file to later reload it to Mathematica the by far most efficient way is this:

Export["mat.mx",mat]
mat = Import["mat.mx"]

it will work for every Mathematica expression containing whatever you want and that of course includes arrays of arbitrary depth. The drawback is of course that these files can only be reloaded with Mathematica and no other program. And AFAIK there is no guarantee that they are compatible between different (major) Mathematica versions.

(note that for some newer data structs like graphs, datasets, neural networks, mesh regions... there might be bugs for some versions, so you better check whether what you want to safe can be reloaded, but the same is true for any other file format of course)

Albert Retey
  • 23,585
  • 60
  • 104
2

I have been using

Export["file.txt", Compress[whatever]]
Uncompress[Import["file.txt"]]
Coolwater
  • 20,257
  • 3
  • 35
  • 64