Re: Indexing into Structures??? [message #32122] |
Thu, 19 September 2002 04:52 |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Randall Skelton wrote:
> Indeed there is a way, but it isn't easy to find in the IDL help. If I
> remember correctly, it is under the heading of 'advanced' structure usage.
> In any case, what you will want is the function 'n_tags()' and the syntax
> 's.(i)' where s is your structure name, and i is the index number. Note
> that the round brackets are important.
>
> IDL> s = { a:0L, b:0L, c:0L }
> IDL> for i=0, n_tags(s)-1 do s.(i) = 1
>
> IDL> help, s, /structure
> ** Structure <81b7cb4>, 3 tags, length=12, data length=12, refs=1:
> A LONG 1
> B LONG 1
> C LONG 1
>
> IDL> print, s.(1)
> 1
>
> It is worth noting that if you ever want to use pointers with a structure,
> an extra set of brackets is often required... i.e.
>
> IDL > a = ptr_new(s)
> IDL > print, (*a).(1)
> 1
Here is another way.
We have written a routine which is able to build two vectors from a
structure (and with substructures). One holds the definition of the
levels and one is a vector of pointers where each pointer is a pointer
of the corresponding tag value.
These vectors could be transformed back into a structure, mainly
we use this for add_tag, rename_tag, delete_tag and replace_tagvalue on
each tag level.
Here is a small example
n=struct2names_and_ptrs(!P,names=names,ptr_values=ptr_values )
print,transpose(names)
BACKGROUND
CHARSIZE
CHARTHICK
CLIP
COLOR
FONT
LINESTYLE
MULTI
NOCLIP
NOERASE
NSUM
POSITION
PSYM
REGION
SUBTITLE
SYMSIZE
T
T3D
THICK
TITLE
TICKLEN
CHANNEL
for i=0,n-1 do print,*ptr_values[i]
Here is a lot of output so I don't like to write it down
You may be interested how to build a structure again from these
two vectors.
It's easy
result=names_and_ptrs2struct(names,ptr_values)
help,result,/str
** Structure <82ef18c>, 22 tags, length=288, data length=288, refs=1:
BACKGROUND LONG 255
CHARSIZE FLOAT 0.00000
CHARTHICK FLOAT 0.00000
CLIP LONG Array[6]
COLOR LONG 0
and so on
You can find both routines here and many others in our library
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/struct2names_and_ptrs.tar.gz
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/names_and_ptrs2struct.tar.gz
or as idl5.5 binary
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/struct2names_and_ptrs.sav
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/names_and_ptrs2struct.sav
For further routines and licensing please have a loot at.
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
best regards
Reimar
--
Reimar Bauer
Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
|
|
|
Re: Indexing into Structures??? [message #32123 is a reply to message #32122] |
Thu, 19 September 2002 03:37  |
Randall Skelton
Messages: 169 Registered: October 2000
|
Senior Member |
|
|
Indeed there is a way, but it isn't easy to find in the IDL help. If I
remember correctly, it is under the heading of 'advanced' structure usage.
In any case, what you will want is the function 'n_tags()' and the syntax
's.(i)' where s is your structure name, and i is the index number. Note
that the round brackets are important.
IDL> s = { a:0L, b:0L, c:0L }
IDL> for i=0, n_tags(s)-1 do s.(i) = 1
IDL> help, s, /structure
** Structure <81b7cb4>, 3 tags, length=12, data length=12, refs=1:
A LONG 1
B LONG 1
C LONG 1
IDL> print, s.(1)
1
It is worth noting that if you ever want to use pointers with a structure,
an extra set of brackets is often required... i.e.
IDL > a = ptr_new(s)
IDL > print, (*a).(1)
1
Let's see you do that with MATLAB ;)
You can find more examples by searching google groups:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&a mp;oe=UTF-8&group=comp.lang.idl-pvwave
Cheers,
Randall
On Thu, 19 Sep 2002, Brian Huether wrote:
> I need to be able to access structure fields within a for loop, meaning that
> I can't reference the field by name. In MATLAB it was easy to access
> Structure fields by simply using index values. Can someone tell me if it is
> possible to do this in IDL? If it isn't, then that is really a major
> shortcoming.
>
> -brian
>
>
>
|
|
|
Re: Indexing into Structures??? [message #32124 is a reply to message #32123] |
Thu, 19 September 2002 03:25  |
Brian Huether
Messages: 12 Registered: September 2002
|
Junior Member |
|
|
Okay I figured this out - I can use the notation A=S.(i) to access a field
by index instead of by name.
"Brian Huether" <bhuether@earthlink.net> schrieb im Newsbeitrag
news:3d89930d_5@news.teranews.com...
> I need to be able to access structure fields within a for loop, meaning
that
> I can't reference the field by name. In MATLAB it was easy to access
> Structure fields by simply using index values. Can someone tell me if it
is
> possible to do this in IDL? If it isn't, then that is really a major
> shortcoming.
>
> -brian
>
>
|
|
|