Mathematica Asked on October 22, 2021
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 another version which I think is showing some useful tricks specific to Mathematica:
data = {
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
}
Export[
FileNameJoin[{$HomeDirectory,"Desktop","data.txt"}],
Join[
{{"index", "x", "y", "z"}},
Table[{i, x[i], y[i], z[i]}, {i, 3}] /. data
],
"Table"
]
Some explanations:
Directory[]
.FileNameJoin
and the $*Directory
variables are essential tools to work with filenames that I think every Mathematica user should know about.File
to specify the filename to export to here, but I think for such relatively simple use cases it is an unnecessary abstraction.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...Answered by Albert Retey on October 22, 2021
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.,
ds[[1]]
Answered by Alan on October 22, 2021
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[]
---x---
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.
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.
Answered by PaulCommentary on October 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP