Re: Is there any way to change the dimension of a field in a structure? [message #58148] |
Tue, 22 January 2008 06:45 |
Hongkai
Messages: 6 Registered: September 2006
|
Junior Member |
|
|
Thanks, David and Maarten! Really nice solutions! While i was waiting
for your answers, I wrote a dirtier code to solve this problem using
the EXECUTE function :)
;Modify a single tag in a structure when we need to change the data
type or dimension of this tag
pro Structure_Modify, Structure, TagName, newValue
NAMES = TAG_NAMES(Structure)
cmdline = 'S1={'
for i_tag = 0,N_TAGS(Structure)-1,1 do begin
if strcmp( STRUPCASE(TagName), NAMES(i_tag) ) ne 1 then begin
cmdline = cmdline + NAMES(i_tag) + ': Structure.
('+strtrim(string(i_tag),2)+')'
endif else begin
cmdline = cmdline + NAMES(i_tag) + ': newValue'
endelse
if i_tag lt N_TAGS(Structure)-1 then begin
cmdline = cmdline + ', '
endif else begin
cmdline = cmdline + ' }'
endelse
endfor
result = EXECUTE(cmdline);
Structure = S1
end
|
|
|
Re: Is there any way to change the dimension of a field in a structure? [message #58151 is a reply to message #58148] |
Tue, 22 January 2008 05:47  |
Maarten[1]
Messages: 176 Registered: November 2005
|
Senior Member |
|
|
On Jan 22, 6:11 am, Hongkai <wang.hong...@gmail.com> wrote:
> This is reasonable because IDL dose not allow to change the dimensions
> of the fields in the structure.
>
> I can solve this problem by redefining another structure S1 with the
> same tag names of S:
> S1 = {Dog:lindgen(3,118), Cat:'cc', Horse:[1,2,4]}
> S = S1
>
> However, the question is, if I only know the tag name of the field
> Dog, and I don't know the tag names of other fields in S, how can I
> change the dimension of Dog?
>
> I've tried this:
> aa = tag_names(S1)
> S = {aa(0):lindgen(3,118), aa(1):S.(1),a a(2):S.(2)}
Although David pointed to pointers as a solution, that does require
extensive changes to the rest of your code. The following dirty code
may be of help.
tnames = tag_names(S1)
tmp = create_struct(tnames[0], lindgen(3,118))
for ii=1,n_elements(tnames)-1 do tmp = create_struct(tmp, tnames[ii],
S.(ii))
S = tmp
Maarten
|
|
|