LoginSignup
0
0

More than 1 year has passed since last update.

IDLでテキストファイルを読み込み、配列に格納する方法

Posted at

テキストファイルから読み取り、配列に格納する。
https://www.l3harrisgeospatial.com/docs/read.html
上記のサイトを参考に進める。

OPENR

まず、ファイルのパスを取得する。

file=dialog_pickfile(filter='*.txt')

上記を実行するとダイアログが表示され、選択したファイルのパスをfileに格納する。
OPENR,UNIT,FILE

  • UNIT:識別子のようなもので自由に設定してよい
  • FILE:ファイルのパス
    また下記のように記述するとファイルを選択した状態を維持できる。
OPENR,UNIT,FILE,/GETLUN

また終了する際は次のように記述する。

FREE_LUN,UNIT

READF

s

READF,UNIT,var
  • UNIT:OPENRで指定した識別子
  • var:格納する変数

テキストファイルを行単位で配列に格納してみる

pro art_sum_0614,probe,times
; Select a text file and open for reading
  file = DIALOG_PICKFILE(FILTER='*.txt')
  OPENR, lun, file, /GET_LUN
  ; Read one line at a time, saving the result into array
  array = ''
  line = ''
  WHILE NOT EOF(lun) DO BEGIN & $
    READF, lun, line & $
    array = [array, line] & $
  ENDWHILE
  ; Close the file and free the file unit
  FREE_LUN, lun
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0