Re: how to link the strname with the same name of variables [message #67271] |
Tue, 14 July 2009 07:35  |
Tony
Messages: 6 Registered: March 2009
|
Junior Member |
|
|
On Jul 14, 9:58 am, David Fanning <n...@dfanning.com> wrote:
> Jean H. writes:
>> Otherwise, have a look at SCOPE_VARFETCH It will allow you to retrieve
>> the content of your variable.
>
> Oh, my gosh. SCOPE_VARFETCH!? EXECUTE!? This is a person
> who is just learning how to write a program! How about
> letting him in on arrays, so he doesn't have to give
> a unique name to every single variable in his program?
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Of course David is right, as long as all the variables are the same
size. The 'execute' trick comes in handy if you have variables which
are of different lengths and would not neatly fit into an array.
Tony
|
|
|
|
Re: how to link the strname with the same name of variables [message #67273 is a reply to message #67272] |
Tue, 14 July 2009 06:23   |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
mengran wrote:
> Hi,everyone.
>
> I want to output a batch of variables to different files, and the
> variable names like c1, c2,c3,,,,. I use the arrstring to save the
> variable names and get the real variable data from different bands of
> a image. The problem confused me is that it is not possible to output
> the variable in a for loop, although the elements of strarr have the
> same name as the true variable names. There must be some skills that I
> do not know, could you tell them to me please?Thanks a lot!
> best wishes,
> mengran
Hi,
ok, you have 2 solutions. The first one is to create an array holding
your c's values... so you have c1 = fltarr(x,y), c2 = fltarr(x,y) etc,
you would end up with c = fltarr(x,y,nbBands). Then you can use C in
your loop (writeu, lun, c[*,*,i]) . Alternatively, you can use an array
of pointers (if you don't have enough memory).
Otherwise, have a look at SCOPE_VARFETCH It will allow you to retrieve
the content of your variable.
Jean
|
|
|
Re: how to link the strname with the same name of variables [message #67275 is a reply to message #67273] |
Tue, 14 July 2009 05:23   |
Tony
Messages: 6 Registered: March 2009
|
Junior Member |
|
|
On Jul 13, 8:35 pm, mengran <mengra...@gmail.com> wrote:
> Hi,everyone.
>
> I want to output a batch of variables to different files, and the
> variable names like c1, c2,c3,,,,. I use the arrstring to save the
> variable names and get the real variable data from different bands of
> a image. The problem confused me is that it is not possible to output
> the variable in a for loop, although the elements of strarr have the
> same name as the true variable names. There must be some skills that I
> do not know, could you tell them to me please?Thanks a lot!
> best wishes,
> mengran
> pro sub_test
> Path_08 = 'D:\mengr_2\spot_vege\2008_byte\project_pof'
> Path_99 = 'D:\mengr_2\spot_vege\1999_byte\project_pof'
> out_path='D:\mengr_2\sub'
> outFl = strarr(5) & ref = strarr(5)
> for i=1,5 do begin
> outFl[i-1] = strjoin([out_path, strtrim(string(i), 2), '.img']) ;the
> full path of output files
> ; outh = strjoin([out_path, strtrim(string(i), 2)])
> ref[i-1] = strjoin(['c', strtrim(string(i), 2)]) ; the output
> variable names
> endfor
> ;get the data to variables
> envi_open_file,Path_08,r_fid = fid1
> envi_file_query,fid1,nb = nb1, ns = ns1, nl = nl1, dims = dims1
> a1 = ENVI_GET_DATA(fid = fid1,pos = [0], dims = dims1)
> a2 = ENVI_GET_DATA(fid = fid1,pos = [1], dims = dims1)
> a3 = ENVI_GET_DATA(fid = fid1,pos = [3], dims = dims1)
> a4 = ENVI_GET_DATA(fid = fid1,pos = [5], dims = dims1)
> a5 = ENVI_GET_DATA(fid = fid1,pos = [7], dims = dims1)
> map_info = envi_get_map_info(fid = fid1)
> proj = ENVI_GET_PROJECTION(FID = fid1)
> envi_open_file,Path_99,r_fid = fid2
> envi_file_query,fid2,nb = nb2, ns = ns2, nl = nl2, dims = dims2,
> INTERLEAVE = INTERLEAVE
> b1 = ENVI_GET_DATA(fid = fid2,pos = [0], dims = dims2)
> b2 = ENVI_GET_DATA(fid = fid2,pos = [1], dims = dims2)
> b3 = ENVI_GET_DATA(fid = fid2,pos = [3], dims = dims2)
> b4 = ENVI_GET_DATA(fid = fid2,pos = [5], dims = dims2)
> b5 = ENVI_GET_DATA(fid = fid2,pos = [7], dims = dims2)
> c1 = fltarr(ns1,nl1) & c2 = fltarr(ns1,nl1)& c3 = fltarr(ns1,nl1)& c4
> = fltarr(ns1,nl1)& c5 = fltarr(ns1,nl1)
> for k = 0,ns1-1 do begin
> for j = 0,nl1-1 do begin
> do something
> endfor
> endfor
>
> for i=0,4 do begin
> openw,unit,outFl[i],/get_lun
> writeu,unit,ref[i] ;only can output the string but not the real
> data
> free_lun,unit
> ENVI_SETUP_HEAD, map_info=map_info, data_type = 4, nb=1,ns = ns1,nl
> = nl1,offset = 0,INTERLEAVE = INTERLEAVE,FNAME=outFl[i]
> endfor
> ;it works like this way, ~~~~(>_<)~~~~
> ;openw,unit,outFl[0],/get_lun
> ;writeu,unit,c1
> ;free_lun,unit
> ;openw,unit,outFl[1],/get_lun
> ;writeu,unit,c2
> ;free_lun,unit
> ;openw,unit,outFl[2],/get_lun
> ;writeu,unit,c3
> ;free_lun,unit
> ;openw,unit,outFl[3],/get_lun
> ;writeu,unit,c4
> ;free_lun,unit
> ;openw,unit,outFl[4],/get_lun
> ;writeu,unit,c5
> ;free_lun,unit
>
> end
My first thought (let me know if I didn't read your code well
enough :) is the 'execute' command. You could do something like this:
> for i=0,4 do begin
> openw,unit,outFl[i],/get_lun
diditwork=execute('writeu,'+string(unit)+','+ref[i]) ;only can
output the string but not the real
I didn't actually try that code, but you get the idea :) Check out
'execute' in the IDL help
Tony
|
|
|
Re: how to link the strname with the same name of variables [message #67278 is a reply to message #67275] |
Tue, 14 July 2009 00:37   |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
mengran schrieb:
> Hi,everyone.
>
> I want to output a batch of variables to different files, and the
> variable names like c1, c2,c3,,,,. I use the arrstring to save the
> variable names and get the real variable data from different bands of
> a image. The problem confused me is that it is not possible to output
> the variable in a for loop, although the elements of strarr have the
> same name as the true variable names. There must be some skills that I
> do not know, could you tell them to me please?Thanks a lot!
> best wishes,
>
you should use a structure read about by entering ?create_struct at the
idl prompt in the online help and also you may want to write a function
which returns your structure.
cheers
Reimar
|
|
|
|
Re: how to link the strname with the same name of variables [message #67368 is a reply to message #67271] |
Tue, 14 July 2009 10:29  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Tony schrieb:
> On Jul 14, 9:58 am, David Fanning <n...@dfanning.com> wrote:
>> Jean H. writes:
>>> Otherwise, have a look at SCOPE_VARFETCH It will allow you to retrieve
>>> the content of your variable.
>> Oh, my gosh. SCOPE_VARFETCH!? EXECUTE!? This is a person
>> who is just learning how to write a program! How about
>> letting him in on arrays, so he doesn't have to give
>> a unique name to every single variable in his program?
>>
>> Cheers,
>>
>> David
>>
>> --
>> David Fanning, Ph.D.
>> Fanning Software Consulting, Inc.
>> Coyote's Guide to IDL Programming:http://www.dfanning.com/
>> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
>
> Of course David is right, as long as all the variables are the same
> size. The 'execute' trick comes in handy if you have variables which
> are of different lengths and would not neatly fit into an array.
>
> Tony
/me hates evil EXECUTE
|
|
|