We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have the following program and try to find a way to read the values of variables from a TXT file.
static final int ribbon_length = 512, H = 300;
void setup() {
size(ribbon_length, H);
}
void draw() {
int k = 0;
int l = 255;
float p = 0.5;
int up_y = 10;
int widthh = 4;
int height = 180;
float a = pow (ribbon_length, 1-p);
float colour = 0;
for (int step = 0; step <= 255; step++) {
colour = a * pow (step, p);
fill(colour);
rect(widthh*step, up_y, widthh, height);
noStroke();
//save("cross20.png");
}
}
I read that thread, but I'm not sure how I can customize the code.
http://forum.processing.org/one/topic/using-strings-from-text-file-as-input-for-float-variable.html
The latest part of the code in the example mention is the code below that I have to input to my code, is that right? and of course I need to do a loop to repeat the code for each different set of values.
void reloadData() {
String[] data = loadStrings(fileName);
for ( int i=0; i!=numVars; vars[i] = float(data[i++]) );
}
Any suggestions?
Answers
There are two possibilities on the way your data is saved. I'll cover them both.
I'm also assuming that the text file holds the same type of data, for the same variable.
Hopefully, each different value is saved on a new line. If that's the case, then you just have to load the text file into an array like shown at the bottom of your post, then parse the data accordingly.
However, if your data is separated not by a new line, but rather a comma (or any form of separation, like a colon or semi-colon), then you'll have to use a bit more complicated method involving the split() method. This is what I mean by "separated by a comma":
-- MenteCode
I try that one and didn't work
With error: cannot find a class or type named "FileReader"
It's easier to just use loadStrings():
http://processing.org/reference/loadStrings_.html
Something like this? :)
this works half way
the colour bit must be of type color, not float (line 17). Also the formula didn't work for me (see println output in line 19)
also the window was too small at first (line 4)
I check the values in line 14
I use for input.txt