Add a Field to an Array of Structures
QUESTION: I want to add a new field to an array of structures I have already created. Is there any way to do this efficiently in IDL? Can I do this without creating a new structure array and copying each field separately?
ANSWER: As Wayne Landsman points out, it is not possible to avoid creating a new structure in IDL, since part of a structure definition is a defined size that can't be changed, but copying the contents of one structure to another is made much easier with the Struct_Assign command in IDL.
Consider this structure.
IDL> struct = { A:3.0, B:'coyote' } IDL> array = Replicate(struct, 10)
We wish to add a new field or tag to this structure. We start by creating a new structure with the new field.
IDL> newStruct = { A:5.0, B:'pig', C:10.0 } IDL> newArray = Replicate(newStruct, 10)
To copy the contents of the first structure array to the new structure array, you can do this.
IDL> Struct_Assign, array, newArray IDL> Print, newArray[0] {3.00000 coyote 0.000000}
You see that the new field has been "zeroed out", or set to its null value state. This is because Struct_Assign is using relaxed structure assignment in which missing fields are simply ignored and set to their null state.
Version of IDL used to prepare this article: IDL 7.1.2
Copyright © 2010 David W. Fanning
Last Updated 7 March 2010