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
|