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


and then imports it back by using


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
    $\begingroup$ 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) $\endgroup$ – george2079 Sep 17 '16 at 14:07

Use Put and Get.


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

one exports using


and imports next with


This works for arbitrary depth lists.

  • $\begingroup$ @Feyre: where do you see temp files here? $\endgroup$ – Albert Retey Sep 8 '16 at 16:14
  • $\begingroup$ @AlbertRetey I misread the documentation. I was surprised Put[] works so much faster. I don't think Export[] is better than Put[] in this case, in fact Put[] seems to be faster. I have concordantly upvoted this answer. $\endgroup$ – Feyre Sep 8 '16 at 17:48
  • $\begingroup$ Marvelous & remarkable! $\endgroup$ – Αλέξανδρος Ζεγγ Sep 10 '16 at 6:13
  • $\begingroup$ Another question. Can we set where to put things? $\endgroup$ – Αλέξανδρος Ζεγγ Sep 17 '16 at 13:46
  • $\begingroup$ @Alexander, look at SetDirectory[]. $\endgroup$ – J. M.'s torpor Oct 8 '16 at 13:31
dat = RandomReal[10, {3, 3, 3}];
Export["test.dat", dat, "List"];
dat2 = ToExpression[Import["test.dat", "List"]];
Simplify[dat == dat2]


dat // MatrixForm
dat2 // MatrixForm

enter image description here

  • $\begingroup$ It seems that "ToExpression" is indispensable? $\endgroup$ – Αλέξανδρος Ζεγγ Sep 8 '16 at 9:18
  • $\begingroup$ @AlexanderZeng Unless you want to work with temp files, yes. $\endgroup$ – Feyre Sep 8 '16 at 9:33
  • $\begingroup$ Can you explain why you think this is better than just using Put and Get? $\endgroup$ – Albert Retey Sep 8 '16 at 16:15

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

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)


I have been using

Export["file.txt", Compress[whatever]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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