2013-05-16 65 views
0

我必須使用來自兩個IDL結構的數據創建一個適合文件。這不是基本問題。創建一個變量,在for循環中更改大小

我的問題是,首先我必須創建一個包含兩個結構的變量。 爲了創建這個,我使用了一個for循環,它會在每一步寫入我的變量的新行。 問題是,我不能在下一步添加新行,它覆蓋它,所以最後我的適合文件,而不是,我不知道,10000行,它只有一行。

這是我也試過

for jj=0,h[1]-1 do begin 

    test[*,jj] = [sme.wave[jj], sme.smod[jj]] 
    print,test 
endfor 

*通配符搞亂了一切,因爲現在裏面test我有對應jj數,sme.wavesme.smod不是值。

我希望有人能夠理解我問了什麼,這可以幫助我! 預先感謝您!

Chiara的

回答

0

假設你「sme.wave」和「sme.smod」結構域包含具有相同數量的元素1-d數組作爲有在「測試」行,那麼您的代碼應該工作。例如,我想這一點,得到了以下的輸出:

IDL> test = intarr(2, 10) ; all zeros 
IDL> sme = {wave:indgen(10), smod:indgen(10)*2} 
IDL> for jj=0, 9 do test[*,jj] = [sme.wave[jj], sme.smod[jj]] 
IDL> print, test 
     0  0 
     1  2 
     2  4 
     3  6 
     4  8 
     5  10 
     6  12 
     7  14 
     8  16 
     9  18 

然而,爲了更好的速度優化,你應該做的,而不是IDL的多線程陣列以下的操作和利用。循環通常比類似如下的要慢得多:

IDL> test = intarr(2, 10) ; all zeros 
IDL> sme = {wave:indgen(10), smod:indgen(10)*2} 
IDL> test[0,*] = sme.wave 
IDL> test[1,*] = sme.smod 
IDL> print, test 
    0  0 
    1  2 
    2  4 
    3  6 
    4  8 
    5  10 
    6  12 
    7  14 
    8  16 
    9  18 

此外,如果你不知道什麼是「測試」的大小是時間提前,並且要附加的變量,即加排,那麼你可以這樣做:

IDL> test = [] 
IDL> sme = {wave:Indgen(10), smod:Indgen(10)*2} 
IDL> for jj=0, 9 do test = [[test], [sme.wave[jj], sme.smod[jj]]] 
IDL> Print, test 
    0  0 
    1  2 
    2  4 
    3  6 
    4  8 
    5  10 
    6  12 
    7  14 
    8  16 
    9  18