Netlogo Export/Tableau issues

守給你的承諾、 提交于 2019-12-05 20:08:14
Charles

This question was asked and answered over on NetLogo Users. James Steiner's answer is copied below, with a few typos in the code corrected. It's really quite elegant.


You can print the headers to your results-file during setup!

You might want to make a subroutine to handle all writing to the file, so you don't have to repeat code:

to write-csv [ #filename #items ]
  ;; #items is a list of the data (or headers!) to write.
  if is-list? #items and not empty? #items
  [ file-open #filename
  ;; quote non-numeric items
  set #items map quote #items
  ;; print the items
  ;; if only one item, print it.
  ifelse length #items = 1 [ file-print first #items ]
  [file-print reduce [ (word ?1 "," ?2) ] #items]
  ;; close-up
  file-close
  ]
end

to-report quote [ #thing ]
  ifelse is-number? #thing
  [ report #thing ]
  [ report (word "\"" #thing "\"") ]
end

You would call it with

write-csv "myfilename.csv" ["label1" "label2" "label3"]

to write the column headers in your setup routine, and then

write-csv "myfilename.csv" [10.0 "sometext" 20.3]

to write a row of data - in this case a number, a string, and another number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!