Help to export a result in .csv

397 views
Skip to first unread message

Rafaela Lorena

unread,
Jun 18, 2021, 7:42:45 PM6/18/21
to netlogo-users
Hi all,


I'm having some difficulty exporting a result in .csv from NetLogo.

I would like to extract information from the path taken by various turtles in the world. That is, extract its coordinates (as if it were the path that is marked with the pen-down command) but it is the coordinates that I throw in another program and reproduce the path made by each turtle. The idea is to have all the coordinates of the path taken by each turtle. I'm doing the following code below, but, the following error keeps appearing: this code cannot be executed by a patch, only a turtle error while patch -16 16 running FILE-PRINT called by (anonymous command: [ t -> ask t [ file-print word self ": pxcor: " pxcor " pycor: " pycor " turtle-id: " who ] ]) called by procedure OUTPUT called by procedure GO called by 'go' button

Is it possible for me to extract to a .csv file all the coordinates of a path made by every turtle in the world?

Thanks in advance


    to setup
      ca
      reset-ticks
      crt 10
      ask turtles [
        setxy random-xcor random-ycor
        pen-down
      ]
      let pcolors [ ]
      set pcolors [ 1 10 ]
      ask patches [ set pcolor item (random 2) pcolors ]
    end
   
    to go
      move
      let n count turtles
      if n = 0 and output? = true [output] ;; ouput? is an switch on interface
      if n = 0 [ stop ]
      tick
    end
   
    to move
      ask turtles [
        rt random 360  
        fd 1
        if ticks >= 5
      [ die ]
    ]
    end
   
   
    to output
      file-open "test.csv"
      file-print ( word "---------- Tick Number: " ticks "-----------" )
      foreach sort patches
      [
        t ->
        ask t
        [
          file-print ( word self ": pxcor: " pxcor " pycor: " pycor " turtle-id: " who )
        ]
      ]
      file-print ""  ;; blank line
      file-close
    end

Wade Schuette

unread,
Jun 18, 2021, 10:05:37 PM6/18/21
to Rafaela Lorena, netlogo-users
don't ask me why.
Seems to be strange interaction with files and patches.
anyway this sort of thing works.  define some globals to get info out of the ask context.
you are asking for who for a turtle already dead!  need to save it.

globals [px py ss tid]
patches-own [ turtlewho ]
[...]
   
    to output
       file-open "temp.txt"

   
        foreach sort patches
      [
        t ->
         
          ;file-print ( word self ": pxcor: " pxcor " pycor: " pycor " turtle-id: " who )
          ask t [
          set px  pxcor  
          set py  pycor  
          set ss   self  
          set tid  turtlewho
            ]
           if (tid > 0 ) [file-print ( word px " " py " " ss " " tid)  ]
      ]
      file-close
       end

wade

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CAHs8kB9VQ3KuqDQQ-23L1zfweM1%2Bay0jzdfCidDwsRBGTx5q9g%40mail.gmail.com.

wade.s...@gmail.com

unread,
Jun 19, 2021, 7:22:25 AM6/19/21
to netlogo-users
oh, i left out defining the turtle-id, which I think should go just before "die".
Here's the full working code.  I set the patch label to the turtle-id who died there to
visually confirm it's doing what you want it to,

globals [px py ss tid]
patches-own [ turtlewho ]
 to setup
      ca
      reset-ticks
      crt 10
      ask turtles [
        setxy random-xcor random-ycor
        pen-down
      ]
      let pcolors [ ]
      set pcolors [ 1 10 ]
      ask patches [ set pcolor item (random 2) pcolors ]
    end
   
    to go
      move
      let n count turtles
      if n = 0 and output? = true [output] ;; ouput? is an switch on interface
      if n = 0 [ stop ]
      tick
    end
   
    to move
      ask turtles [
        rt random 360  
        fd 1
        if ticks >= 5
           [ set turtlewho who
             set plabel who
             die
           ]
    ]
    end
    

    to output

       file-open "temp.txt"

        foreach sort patches
      [
        t ->

          ;file-print ( word self ": pxcor: " pxcor " pycor: " pycor " turtle-id: " who )
          ask t [
          set px  pxcor
          set py  pycor
          set ss   self
          set tid  turtlewho
            ]
           if (tid > 0 ) [  ;; not sure if you want to record them all or just the ones where turtles died
                  print ( word px " " py " " ss " " tid) ;; if you want to see what's going on
                  file-print ( word px " " py " " ss " " tid)  ;; this works now
               ]
      ]
      file-close
       end

wade.s...@gmail.com

unread,
Jun 19, 2021, 7:28:02 AM6/19/21
to netlogo-users
Sigh. Above code misses the first turtle with turtle who = 0.
This fixes that.

globals [px py ss tid]
patches-own [ turtlewho ]
 to setup
      ca
      reset-ticks
      crt 10
      ask turtles [
        setxy random-xcor random-ycor
        pen-down
      ]
      let pcolors [ ]
      set pcolors [ 1 10 ]
      ask patches [ set pcolor item (random 2) pcolors ]
      ask patches [ set turtlewho -1 ]   ;;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
           if (tid >= 0 ) [  ;; <<<<<< include zero to get the first turtle!, Be sure to set all to -1 in setup.

                  print ( word px " " py " " ss " " tid) ;; if you want to see what's going on
                  file-print ( word px " " py " " ss " " tid)  ;; this works now
               ]
      ]
      file-close
       end
Reply all
Reply to author
Forward
0 new messages