I have the following output obtained after optimizing the positions of geometry using Minimize command

{x[1] -> 1011.07, y[1] -> 1127.56, z[1] -> 420.922, x[2] -> 940.095, 
 y[2] -> 1041.71, z[2] -> 414.793, x[3] -> 754.727, y[3] -> 1088.95, 
 z[3] -> 467.422}

I have converted this to input form and saved in another variable,

coord = {x[1] -> 1011.07, y[1] -> 1127.56, z[1] -> 420.922, x[2] -> 940.095, 
 y[2] -> 1041.71, z[2] -> 414.793, x[3] -> 754.727, y[3] -> 1088.95, 
 z[3] -> 467.422}

I would like to save this output as three columns corresponding to x,y,z coordinates of a geometry in a text file.

I think I should be using the export option, I tried

`Export["output.txt", coord]`

and this gives

x[1] -> 1018.4781995039851
y[1] -> 1136.9092785077921
z[1] -> 422.85069778505175
x[2] -> 949.2538801819732
y[2] -> 1051.3795795311353
z[2] -> 404.52636548576334
x[3] -> 763.3574381125803
y[3] -> 1053.2894203505348
z[3] -> 473.80190652330657

I would like to ask for suggestions on how to save this in the following way

index  x                    y                   z
1     1018.4781995039851 1136.9092785077921     422.85069778505175
2     949.2538801819732   :
3     763.3574381125803   :

Here is an approach that address the core of your request

First strip the values from the Association and group them with Partition.

z1 = Values[coord] // Partition[#, 3] & ;

Now create a file variable

theFile = File["exportFile.txt"];

Export as follows (using "Table" argument to Export

Export[theFile, z1, "Table"];

This file will end up in the directory referred to by Directory[]


Now a little more complication

Add a columns of row indices to the left of your table with (I think it is convoluted syntax, but it works....)

z2 = Insert[z1 // Transpose, Range[3], 1] // Transpose;

The next picture shows z1 and z2.

enter image description here Add a row of labels to the top of your table

z3 = Prepend[z2, {"index", "x", "y", "z"}];

And then Export the variable z3 instead of z1 as above.

The only problem is the labels aren't aligned properly; this could be a problem or not; depending on how you will use the exported file.

enter image description here

| improve this answer | |
  • $\begingroup$ Thank you. I could export the data without the index. While trying to evaluate z2, I get Transpose::nmtx: The first two levels of <<1>> cannot be transposed. $\endgroup$ – Natasha Jul 18 at 2:30
  • $\begingroup$ I added a picture to the answer which will hopefully help us get to the bottom of this. $\endgroup$ – PaulCommentary Jul 18 at 14:46

If this is really the structure, maybe you could use a dataset instead?

pdata = Partition[coord/. h_[i_Integer] -> h, 3]
ds = Dataset[Association @@@ pdata]

Indexes are supplied automatically. E.g.,

| improve this answer | |
  • $\begingroup$ Thanks. May I know how to export ds? $\endgroup$ – Natasha Jul 18 at 2:30
  • 1
    $\begingroup$ See the answer here. $\endgroup$ – Alan Jul 19 at 16:51

Here is another version which I think is showing some useful tricks specific to Mathematica:

 data = {
        x[3] -> 754.727,y[3]->1088.95,z[3]-> 467.422

     {{"index", "x", "y", "z"}},
     Table[{i, x[i], y[i], z[i]}, {i, 3}] /. data

Some explanations:

  • I find it more convenient and less error-prone to work with full path-names when working with files than to rely on the current value of Directory[].
  • FileNameJoin and the $*Directory variables are essential tools to work with filenames that I think every Mathematica user should know about.
  • There is nothing wrong with using File to specify the filename to export to here, but I think for such relatively simple use cases it is an unnecessary abstraction.
  • As you have your data as a list of rules it seems easiest to take advantage of that fact when constructing the tabular data you are after.
  • I could have used Prepend instead of Join which would have saved me the extra pair of curly brackets, but I find it more clear to write the header before the data in such cases...
| improve this answer | |

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.