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!
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
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"]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With