How to save data of a file .txt directly in Netlogo? - netlogo

I have a procedure "read-file" that is often called in ask[]. From read file lines with spaces into NetLogo as lists, this procedure reads an file .txt as lists. Is it possible to save data of a file .txt directly in Netlogo to gain time because reading a file .txt for each turtle decreases the model speed ?
to read-file
let parameters split-into-n-lists 1 read-file-into-list "Parameters.txt"
end
Thanks in advance for your help.

You should probably not call read-file file inside ask. Assuming the data in the file doesn't change during the simulation, you should call it once (probably during setup) and access the resulting lists (saved as global variables) inside your ask.
And I'm not entirely sure that this is what you mean by "save data of a file .txt directly in Netlogo", but you could also initialize your lists directly in code:
globals [
a b c
]
to setup
set a [2321 2321 2321 213]
set b [23233 3223 313 321]
set c [2 2 1 1]
end

Related

Clearing a specific global variable in octave

I am trying to load the variables from a multiple .mat files using 'who' function and saving it in a variable 'A'. I am using a for loop for that. When I finish loading the first file and start loading the second file then 'A' shows variables in first .mat file as well. The problem is the function 'who' saves the variables as it is for multiple loops and I want to clear the 'who' after each loop. How can I do this. There is any way to clear a specific global variable.
for i=1:10; (10 mat files)
clear A;
clear who;
A=who; (all the variables in each mat file saved in A)
max(A(1,1); (finding max of variable A(1,1))
end
from the above code, if each .mat file has 5 variables then in the second loop the 'who' has 10 variables. the who is not cleared.
It's not entirely clear what you're trying to do because who (with no input arguments) returns a list of all variables in the current workspace not the variables within a file. For it to return a list of variables within a file you'd need to do something like:
vars = who('-file', filenames{i});
That being said, it looks like you actually want to load the variable A from all mat files that you've saved and find the maximum value of A across these files.
The better way to approach this is to specify an output to load which will load the data into a struct where each variable is stored as a separate field in the struct. You can also specify an additional input to load to specify that you'd only like to load variable A (in case there are other variables). You can then load each matfile into a separate struct and do your comparison
for k = 1:numel(filenames)
% Load variable A from this file into a struct
data(k) = load(filenames{k}, 'A');
end
% Now find the maximum value of A
maxA = max([data.A]);

Convert dataset of .mat format to .csv octave/matlab

there are datasets in .mat format in the this site: http://www.cs.nyu.edu/~roweis/data.html
I want to change the format to .csv.
Can someone tell me how to change the format to create the .csv file.
Thanks!
Suppose that the .mat files from the site are available already. In the command window in Matlab, you may write, for example:
load('C:\Users\YourUserName\Downloads\mnist_all.mat');
to load the .mat file; the result should be a set of matrices test0, test1, ..., train0, train1 ... created in your workspace, which you want saved as CSV files. Because they're different size, you need to save one CSV per variable, e.g. (also in the command window):
csvwrite('C:\Users\YourUserName\Downloads\mnist_test0.csv', test0);
Repeat the command for each variable, and do not forget to change also the name of the output file to avoid overwriting.
Did you tried the csvwrite function in Matlab?
Just load your .mat files with the load function and then write them with csvwrite!
I do not have a Matlab license so I installed GNU Octave 4.2.1 (2017) on Windows 10 (thank you to John W. Eaton and others). I was not fully successful using the csvwrite so I used the following workaround. (BTW, I am totally incompetent in the Octave world. csvwrite worked for simple data structures).
In the Command Window I used the following two commands
load myfile.mat
save("-text","myfile.txt","variablename")
When the "myfile.mat" is loaded, the variable names for the data vectors loaded are displayed in the workspace window. This is the name(s) to use in the save command. Some .mat files will load several data structures.
The "-text" option is the default, so you may not need to include this option in the command.
The output file lists the .mat file contents in text format as single column (of potentially sequential variables). It should be easy to use you text editor to massage this data into the original matrix structure for use in whatever app you are comfortable with.
Had a similar issue. Needed to convert a series of .mat files that had two columns of numerical data into standard data files (ascii text). Note that I don't really ever use csv, but everything here could be adapted by using csvwrite instead of the standard save.
Using Octave 4.2.1 ....
load myfile.mat
LI = [L, I] ## L and I are column vectors representing my data
save myfile.txt LI
Note that L and I appear to be default variable names chosen by Octave for the two columns vectors in my original data file. Ideally a script that iterated over all files with the .mat extension in my directory would be ideal, but this got the job done. It saves the data as two space separated columns of data.
*** Update
The following script works on Octave 4.2.1 for a series of data files with the .mat extension that are in the same directory. It will iterate over them and write the data out to text files with the same name but with the extension .dat . Note that this is not efficient, so if you have a lot of files or if they are large it can take a while to run. I would suggest that you run it from the command line using octave mat2dat.m so you can actually watch it go.
I make no guarantees that this will work for you, but it did for me. I also am NOT proficient in Octave or Matlab, so I'm sure a better solution exists.
# mat2dat.m
dirlist = glob("*.mat")
for i=1:length(dirlist)
filename = dirlist{i,1}
load(filename, "L", "I")
LI = [L,I]
tmpname = filename(1:length(filename)-3)
txtname = strcat(tmpname, 'dat')
save(txtname, "LI")
end

NetLogo: Is there a way to customize CSV files generated by export-plot to only include certain columns?

This might seems really basic , but using export plot feature of NetLogo what I get is something like this:
x,y,color,pen down?,x,y,color,pen down?,x,y,color,pen down?,x,y,color,pen down?
Is there a way to not to include color and pen down and Just one X using netlogo itself?
x,y,y,y,y
I can filter unwanted data in R or excel but I have many plots and having clutter free data files make my work much easier :)
In a word, no.
Someone could write an extension that provides this.
Actually, there is a workaround using the built-in csv extension (see https://ccl.northwestern.edu/netlogo/docs/csv.html). All you have to do is to build an array to programmatically gather the same information displayed in the plots and that can be written to a csv file later on as follows (I usually use a button for this purpose):
extensions [array csv]
to write-csv
let csv-delimiter ";"
let output [[ "x" "val1" "val2" ]]
set output lput [ 1 2 3 ] output
set output lput [ 4 5 6 ] output
(csv:to-file "c:/temp/filename.csv" output csv-delimiter)
end
This will result in the following csv file:
x;val1;val2
1;2;3
4;5;6
To write your model outputs to a file, you could construct the array output in your setup procedure and add each line of data in your go procedure (with your model time unit being the value for x).
Please be aware that I changed the csv delimiter to ';' instead of ',' in my example in order to be able to open the file in a German-language version of Microsoft Excel.

Load multiple .mat files for processing

In MatLab I have (after extensive code running) multiple .mat files outputted to .mat files. The actual matlab name of each .mat file is called results but I've used the save command to write them to different files. A small subset of the files looks like this:
results_test1_1.mat
results_test1_2.mat
results_test1_3.mat
results_test1_4.mat
results_test2_1.mat
results_test2_2.mat
results_test2_3.mat
results_test2_4.mat
Now I want to compare the results for each test, which means I have to load in all four .mat files and combine them in a graph. Reading in one file and making the eventual graph is no problem. But since all files have the same matlab name results, iteratively loading them is not an option (at least, not one that I know of yet) since in the end only file 4 remains since it rewrites the previous ones.
Is there a way to load all these files and store them in different variables in a structure (regarding only one test set)? Because doing all this manually is a hell of a lot of work.
I've tried to use this method: Load Multiple .mat Files to Matlab workspace but I get an Invalid field name error on loaded.(char(file)) = load(file);
You can load into a variable (preferably a cell array)
results = cell( 2, 4 ); % allocate
for testi=1:2
for resi = 1:4
filename = sprintf('results_test%d_%d.mat', testi, resi );
results{testi,resi} = load( filename );
end
end
Now you have all the results stored in results cell array and you may access the stored variables, e.g.,
results{1,3}.someVar % access variable someVar (assuming such variable was saves to the corresponding mat file

MATLAB - Stitch Together Multiple Files

I am new to MATLAB programming and some of the syntax escapes me. So I need a little help. Plus I need some complex looping ideas.
Here's the breakdown of what I have:
12 seperate .dat files, each titled something like output_1_x.dat, output_2_x.dat, etc.
each file is actually one piece of a whole that was seperated and processed
each .dat file is approx. 3.9 GB
Here's what I need to do:
create a single file containing all the data from each seperate file, i.e. I need to recreate the original file.
call this complete output file something like output_final.dat
it has to be done in MATLAB, there are no other alternatives (actually there maybe; see note below)
What is implied:
I will have to fread each 3.9 GBfile into chunks or packets, probably 100 mb at a time (using an imbedded loop?)
these packets will have to be read then written sequentially
after one file is read then written into output_final.dat, the next file is automatically read & written (the master loop).
Well, that's pretty much it. I did a search for 'merging mulitple files' and found this. That isn't exactly what I need to do...I don't need to take part of a file, or data from files, and write it to a new one. I'm simply...concatenating...? This would be simple in Java or Perl, but I only have MATLAB as a tool.
Note: I am however running KDE in OpenSUSE on a pretty powerful box. Maybe someone who is also an expert in terminal knows a command/script to do this from the kernel?
So on this site we usually would point you to whathaveyoutried.com but this question is well phrased.
I wont write the code but i will give you how I would do it. So first I am a bit confused about why you need to fread the file. Are you just appending one file onto the end of another?
You can actually use unix commands to achieve what you want:
files = dir('*.dat');
for i = 1:length(files)
string = sprintf('cat %s >> output_final.dat.temp', files(i).name);
unix(string);
end
That code should loop through all the files and pipe all of the content into output_final.dat.temp (then just rename it, we didn't want it to be included in anything);
But if you really want to use fread because you want to parse the lines in some manner then you can use the same process:
files = dir('*.dat');
fidF = fopen('output_final.dat', 'w');
for i = 1:length(files)
fid = fopen(files(i).name);
while(~feof(fid))
string = fgetl(fid) %You may choose to parse the string in some manner here
fprintf(fidF, '%s', string)
end
end
Just remember, if you are not parsing the lines this will take much much longer.
Hope this helps.
I suggest using a matlab.io.matfileclass objects on two of the files:
matObj1 = matfile('datafile1.mat')
matObj2 = matfile('datafile2.mat')
This does not load any data into memory. Then you can use the objects' methods to sequentialy save a variable from one file to another.
matObj1.varName = matObj2.varName
You can get all the variables in one file with fieldnames(mathObj1) and loop through to copy contents from one file to another. You can then clear some space by removing the copied fields. Or you can use a bit more risky procedure by directly moving the data:
matObj1.varName = rmfield(matObj2,'varName')
Just a disclaimer: haven't tried it, use at own risk.