From: Axel Kohlmeyer (
Date: Tue Dec 05 2006 - 16:37:00 CST

On Tue, 5 Dec 2006, Myunggi Yi wrote:

MY> Dear VMD users,
MY> How can I extract the x, y, and z coordinate in the script?
MY> I did something like the following
MY> >Main< (memb-amt) 50 % set sel [atomselect 0 "name C6"]
MY> atomselect0
MY> >Main< (memb-amt) 51 % $sel num
MY> 1
MY> >Main< (memb-amt) 52 % $sel get {x y z}
MY> {6.65500020981 24.7140007019 51.9319992065}
MY> >Main< (memb-amt) 53 % set crd [$sel get {x y z}]
MY> {6.65500020981 24.7140007019 51.9319992065}
MY> >Main< (memb-amt) 54 % lindex $crd 0
MY> 6.65500020981 24.7140007019 51.9319992065
MY> How can I extract x coord. or y coord. independently?

this is a major confusion with VMD. you _always_ get
a list of lists (this is a good thing, since it makes
it consistent which is required for generic script
writing), even if your selection contains only one
atom. so you have to use something like:

set x [lindex [lindex $crd 0] 0]
set y [lindex [lindex $crd 0] 1]
set z [lindex [lindex $crd 0] 2]

to access the individual coordinates.

however this is rarely needed since most scripts use constructs like

foreach c $crd {

where you then can use $c as you intended it for $crd and the
construct would work for any number of atoms in the selection
(even none).


MY> Have a nice day.

Axel Kohlmeyer
   Center for Molecular Modeling   --   University of Pennsylvania
Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
tel: 1-215-898-1582,  fax: 1-215-573-6233,  office-tel: 1-215-898-5425
If you make something idiot-proof, the universe creates a better idiot.