5

Consider the following list in mathematica:

a = {
   {
    {0, 0, 0}, {1, 0, 0}, {1, 1, 0}
    },
   {
    {0, 0, 1}, {1, 0, 1}, {1, 1, 1}
    }
   };

Now, invoke:

Export["test.dat", a]

and then

b = Import["test.dat"]

You will see that at the end a doesn't equal b. Should I consider this as a feature or a bug?

Furthermore, I would like to import a list having the following format: {P1,P2,P3...,Pn} where Pi={v1,v2,v3,...,vm} and each vi={x,y,z} where x,y,z are numbers representing the coordinates of the vertex vi. This should be a list of polygons.

How should I set my .dat file so I can read it with Mathematica, and how should I read it? I tried to imitate the output of Export["test.dat",a] above, but then I discovered the other issue. I found this question, but couldn't make the answer work for me...

Any ideas? Thanks in advance!

Community
  • 1
  • 1
Dror
  • 12,174
  • 21
  • 90
  • 160

4 Answers4

5

You should specify the exact format that you need to import/export in, otherwise Mathematica might not be able to guess the correct format.

So your question boils down to what textual format is suitable for storing 3D arrays?

If you work with Mathematica, probably the easiest thing to do is exporting the expression using Mathematica's expression syntax, i.e. Export["data.m", a, "Package"]. This format is relatively easy to write from other languages (but it's not as easy to parse). Your other option would be to make up some new easy to parse textual format for your 3D data, and write your own input/output functions for it in both Mathematica and the other languages you need to work with.

Since the format of the data you are working with is fixed (you always have coordinate triplets), the easiest solution may be to just flatten out your list before exporting, and partition it after importing, like this:

Export["test.txt", Join @@@ a, "Table"]
b = Import["text.txt", "Table"]
Partition[#, 3]& /@ a
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • By imitating the output of `Export["data.m", a, "Package"]` I managed to do what I wanted. Thanks! – Dror Jun 03 '11 at 09:30
  • @Dror, I thought you need to read your datafiles with other software too. If this is not the case, and you just need to store/save the data temporarily, the "WDX" format might be much faster to import/export than "Package". But it is binary, so you won't be able to inspect it with a text editor or import it into other programs. "WDX" can also store any kind of Mathematica expression (I think). – Szabolcs Jun 03 '11 at 10:17
2

For storing MMA expression I'd suggest DumpSave (binary, system dependent),Save or Put, but if you want to use Export I'd convert a to a string , and export that as text. (I use ImportString and ExpertString below, so that I don't need a file, but it works the same for Import and Export). IMO this is solid as a rock.

a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
b = ToExpression@ImportString[ExportString[a // ToString, "Text"], "Text"]

(*  ==>
{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

a == b

(* ==> True *)

Reading your polygon list should work the same:

b = ToExpression@ImportString["test.dat", "Text"]
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • how is this different from exporting to a "Package"? Actually I just discovered the possibility to export to a "Package" format (.m file), but it seems to do about the same. – Szabolcs Jun 03 '11 at 20:13
  • 1
    @Szabolcs I didn't have the faintest idea as I was unaware of the export to Package option either. I've now studied this capability and I feel "Package" is the way to go (if you don't mind the Wolfram comment at the beginning of the file). For instance, if you export reals, the "Text" options exports the 6 digit format whereas "Package" uses the full accuracy. – Sjoerd C. de Vries Jun 03 '11 at 20:59
1

I also go this Problem. My solution is the following:

IN[]: a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
      Export["test.dat", a, "List"];
      b = ToExpression@Import["test.dat", "List"];
      a == b

Out[]: True

Hope this helps. Best regards.

BennyS83
  • 11
  • 1
1

You may also do for example:

a={{{0,0,0},{1,0,0},{1,1,0}},{{0,0,1},{1,0,1},{1,1,1}}};

Export["c:\\test.dat",a,"MathML"];
b=ToExpression@Import["c:\\test.dat","MathML"]

(*
->{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

The additional benefit is that this method does not require parsing the Import output

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • while this *works* for this specific example, it looks really hackish and fragile to me. What this does is basically build a MathML expression that, when rendered, *looks* more or less like the Mathematica expression, then re-import it. One can only hope that the re-imported version will be the same ... If we take this approach it's much better to use a non-fragile format, like just using the InputForm of the expression (which is what exporting to the "Package" format does). As far as I can tell, "MathML" has no advantages over this, only disadvantages. – Szabolcs Jun 03 '11 at 14:17
  • @Szabolcs As the question scope is `Import data from txt to Mathematica`, I think this way is appropriate and working. The OP is not working with `expressions`, but only `data` structured as Lists. – Dr. belisarius Jun 03 '11 at 14:41
  • 1
    however, MathML is designed for (both) exchanging and formatting mathematical expressions, not lists or arrays. It could be considered an accident that this works without issues ... why do you think that MathML is a better choice for representing arrays than all the other human readable possibilities such as InputForm ("Package"), ExpressionML, etc.? – Szabolcs Jun 03 '11 at 15:04
  • 1
    @Szabolcs I did not say _better_. It just works. But it is (IMHO) better than joining before Export and partitioning after Import. – Dr. belisarius Jun 03 '11 at 15:18