How to export .ASCII file in NetLogo 6.2? - netlogo

I would like to know how I can transform a .csv file into raster (ASCII) within NetLogo 6.2?
Well, I can export in .csv to do the conversion later in .ASCII in another program such as R. However, I think it should be possible to export the output in .ASCII by NetLogo itself. At the same time, I would like to know if in terms of processing it would be better to export the output in .csv by NetLogo or in .ASCII? obs.: my world on NetLogo is relatively big
Thanks in advance
extensions [ gis ]
globals [ file output-filename ]
to setup
clear-all
reset-ticks
set output-filename "output-data.csv"
set-default-shape turtles "person"
create-turtles 10 [
setxy random-pxcor random-pycor
]
initialize-data-file
end
to initialize-data-file
file-close-all
if file-exists? output-filename [
file-delete output-filename
]
file-open output-filename
file-print ( word "xcor, ycor" )
end
to go
ask turtles [
wander-about
if ticks mod 50 = 0 [
write-output-data who xcor ycor
]
]
tick
end
to write-output-data [ #turtle-id #xpos #ypos ]
file-open output-filename
file-print ( word #xpos ", " #ypos )
file-flush
; set file gis:patch-dataset xcor ycor
; gis:store-dataset file "test.asc"
end
to wander-about
rt random 40
lt random 40
if not can-move? 1 [ rt 180 ]
fd 1
end

You're on the right track using the GIS extension to export directly to ASCII. Whether you actually need to do this depends entirely on your application- as you say, it may be possible to do what you need simply by exporting a list of turtle coordinates, or of patch values, etc., depending on your needs. Whether you should use ASCII or csv is going to depend on your application, but: for all of my larger models where I actually need to export the whole world for analysis (and, more importantly, to go the other way and import my spatial data into NetLogo), I always use the GIS extension because it seems to be much faster. If I were only exporting turtle characteristics I would probably export to csv.
To answer your specific question here about how to export directly to ASC- here is one way. You need to set up some spatial information for the GIS extension to work with your data, but once you've done that you can create a raster dataset from patch values and export that to a .ASC file that will consist of the .ASC header in addition to a value for each patch in your world. With this example:
extensions [ gis ]
patches-own [ turtle-here ]
to setup
ca
resize-world 0 14 0 14
; Pull in some coordinate system
gis:load-coordinate-system "utm_11.prj"
; Set some envelope. Unless you are actually spatially
; referencing your model, this is just a placeholder.
; If you are using actual spatial coordinates, set these
; appropriately or use gis:envelope-of
gis:set-world-envelope [ 55000 50000 55000 50000 ]
crt 10
reset-ticks
end
to go
ask turtles [
rt random 80 - 40
ifelse not can-move? 1
[ rt 180 ]
[ fd 1 ]
]
if ticks = 50 [
export-ascii
stop
]
tick
end
to export-ascii
ask patches with [ any? turtles-here ] [
set turtle-here 1
]
let ras_out gis:patch-dataset turtle-here
gis:store-dataset ras_out "any_turtle_here.asc"
end
This exports an ascii file that stores the patches-own variable called turtle-here that simply indicates if a turtle is present on the patch at the time of export:
NCOLS 15
NROWS 15
XLLCORNER 50000
YLLCORNER 50000
CELLSIZE 333.333333
NODATA_VALUE NaN
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

Related

Convert a double array into binary using series

I have a series as [ 7 5 4 3 2 1] . I want to convert a number into binary sequence by considering the place value of the above numbers. So each number have many combinations like..
3 is represented as [ 0 0 0 1 0 0 ] again 3 is represented as [ 0 0 0 0 1 1]
4 is represented as [ 0 0 1 0 0 0 ] again 4 is represented as [ 0 0 0 1 0 1]
5 is represented as [ 0 1 0 0 0 0 ] again 5 is represented as [ 0 0 0 1 1 0]
5 is represented as [ 0 0 1 0 0 1]
I want to store all values in an array. Can anybody help me for writing Matlab Code for it?

matlab running a video, .txt and .mat

I want to run a video in matlab but I got some problems while doing so. I tried to run the video while using 'load' and then 'implay'. But I got this problem. My movie file is a textfile which constains mostly of zeros, arranged diagonally.
When I load this file in matlab I get different values. Here a snippet:
0 0 0 0 0 0 0 0 0 0 0
0 0 -5 0 0 -5.00097609500000 0 0 0 0 0
0 0 -10 0 0 -10.0019423800000 0 0 0 0 0
0 0 -15 0 0 -15.0028988550000 0 0 0 0 0
0 0 -20 0 0 -20.0038455200000 0 0 0 0 0
0 0 -25 0 0 -25.0047823750000 0 0 0 0 0
0 0 -30 0 0 -30.0057094200000 0 0 0 0 0
0 0 -35 0 0 -35.0066266550000 0 0 0 0 0
0 0 -40 0 0 -40.0075340800000 0 0 0 0 0
0 0 -45 0 0 -45.0084316950000 0 0 0 0 0
I dont know why I get a mat.file with different values and why my movie does not run. I wanted to upload the original textfile but this not possible.
When I try to run I receive following message: There is not enough memory to open 'filename' in the Editor.
I hope you guys can help me!

Is there a way to change the digits shown in the variables browser?

the matlab variables browser in the IDE GUI per default only shows 10 digits. longer variables have their dimension shown.
[0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 1 1 0 0]
<1x11 double>
[1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
<1x11 double>
[0 0 1 0 0 0 0 0 0 0]
[0 0 0 1 1 1 0 0 0]
[1 0 0 1 0 0 0 1 1 0]
is there a way to modify this behavior in matlab 8.1 ?
As you may already have guessed from the comments:
No, there is no setting that can be changed via regular means to modify this behavior.
Of course everything is possible, but the 'easiest' way would probably require a java hack, like the one for changing the workspace preferences.

Check the surrounding neighbors of a matrix element in Matlab

I have a 480-by-640 matrix A. For each pixel, I want to check its neighbors. The neighbors of the pixel are determined by a value N. For example, this is a part of matrix A where all the zeros are the neighbours of pixel X when N=3:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
As shown, because N=3, all these zeros are pixel X's neighbors. The problem is if X is located before the index N=3. Here the neighbors will be pixels with one values:
X 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Could anyone advise on how to handle this?
The simplest way to proceed is just to pad your array with with values that do not return true for whatever you are checking (say, if you're looking for nonzeros, pad with zeros, or if you're looking for finite values, pad with NaN.) The padarray function can do this for you, but requires the Image Processing Toolbox*. Otherwise, you can pad arrays yourself. For example, an unoptimized way to proceed might be
A = rand(m,n);
Apadded = [zeros(N,2*N+n); [zeros(m,N), A, zeros(m,N)]; zeros(N,2*N+n)];
for i = N+1:N+m+1
for j = N+1:N+n+1
% Process neighborhood of A(i,j)
end
end
*Also note that these sorts of "sliding neighborhood" operations, being common in image processing, are implemented for you in the Image Processing Toolbox.

every other matching element in matrix MATLAB

I have a matrix of 2 values, for example:
[ 1 1 1 1 1 0 0 0 1 1 0 0 0
0 0 0 0 1 1 1 1 0 0 0 1 1 ]
I want to to change every other element in a row that is a "1" to a 0,
so the output would be:
[ 0 1 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 ]
if your matrix is m then you can do:
ind=find(m(:)==1);
m(ind(1:2:end))=0;