If I create a table of this kind:

(m1 = Table[{RandomInteger[], RandomInteger[{1, 5}, {3}]}, {i, 1, 
 5}]) // MatrixForm

which looks like

enter image description here

I know how to pick the first column of the second column:

m1[[All, 2, 1]] // MatrixForm

which looks like

enter image description here

But if I first export and then again import the matrix m1

Export["data.dat", m1, "Data"]
m2 = Import["data.dat", "Data"]

I do not get the same result as previously

m2[[All, 2, 1]] // MatrixForm

which looks like

enter image description here

What is wrong?

  • 1
    $\begingroup$ If you just want to save/retrive the data use "m" or "mx" format, eq Export["data.m",m1] these are "native" mathematica formats and will safely preserve whatever expression you export. If you really want to use the "Data" format for some reason that will take a bit of work to read it back in (clarify if that's what you need.. ) $\endgroup$ – george2079 Dec 3 '14 at 17:09
  • $\begingroup$ @george2079 Seems I somehow misinterpreted the docs ("Data": give data in a generic form (list, string, etc.)), I thought this was the one I had to add. I did not want to export as "Data" format, so actually your comment contains the solution to my problem. Could you please put it here as answer? I will mark it then. $\endgroup$ – wondering Dec 3 '14 at 21:42
  • $\begingroup$ Ok, anyway, +1. $\endgroup$ – wondering Dec 3 '14 at 22:28

When you export a Table, then the array cannot be arbitrarily nested. What you should do is to export your data (which is not a matrix!) in a different format. Try:

Export["data.dat", m1, "Package"]
Import["data.dat", "Package"][[All, 2, 1]] // MatrixForm


Export["data.dat", m1, "MX"]
Import["data.dat", "MX"][[All, 2, 1]] // MatrixForm

and read the documentation of these formats.

  • $\begingroup$ Thank you, this works, too +1, but the solution suggested by george2079 is sufficient for me (it's shorter). $\endgroup$ – wondering Dec 3 '14 at 21:44

Because you are exporting as text, and when you import back into Mathematica, it will see extra commas. To get the desired default, either export as csv

Export["data.csv", m1]

or WDF

Export["data.wdx", m1, "WDX"]
  • $\begingroup$ 'Export["data.wdf", m1, "WDF"]' gives in the case of my matrix m1 'Export::infer: Cannot infer format of file data.wdf. >>' $\endgroup$ – wondering Dec 3 '14 at 21:46
  • $\begingroup$ My bad, I meant WDX. It's Wolfram's proprietary file format. Try 'Export["data.wdx", m1, "WDX"]'. Works for me. $\endgroup$ – nqduy Dec 4 '14 at 1:51
  • $\begingroup$ Yes, it works, not the 'Export["data.csv", m1]', however. $\endgroup$ – wondering Dec 4 '14 at 6:38
  • 1
    $\begingroup$ Ah, I see what the problem is. At a glance, it looks correct, but Mathematica actually exported the inner list as a string (without added commas, so it's not too bad). You can still use this format if you do just one extra process after import: m2 = Import["data.csv", "Data"]; m2[[1, 2]] // FullForm m2[[All, 2]] = Map[ToExpression, m2[[All, 2]]] m1 == m2 m2[[1, 2]] // FullForm $\endgroup$ – nqduy Dec 4 '14 at 16:24
  • $\begingroup$ Ok, thank you for the effort! $\endgroup$ – wondering Dec 4 '14 at 17:34

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.