How to read the values ​​of variables from a TXT file

edited February 2014 in How To...

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?

Tagged:

Answers

  • edited February 2014

    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":

    45, 76, 24, 90, 24, etc...
    

    -- MenteCode

  • edited February 2014

    I try that one and didn't work

    BufferedReader br;
    String line;
    static final int ribbon_length = 255, H = 200;
    
    void setup() {
      size(ribbon_length, H);
      //reader = createReader("positions.txt");
      BufferedReader br = new BufferedReader(new FileReader("values.txt"));  
    }
    
    void draw() {
      int k = 0;
      int l = 255;
      float p = Float.parseInt(br.readLine());  
      int up_y = Integer.parseInt(br.readLine()); 
      int widthh = Integer.parseInt(br.readLine());  
      int height = Integer.parseInt(br.readLine());  
      float a = pow (ribbon_length, 1-p); 
      float colour = 0; 
      for (int step = k; step <= l; step++) { 
          colour = a * pow (step, p);          
          fill(colour,0,0);     
          rect(widthh*step, up_y, widthh, height); 
          noStroke(); 
       }
    }
    

    With error: cannot find a class or type named "FileReader"

  • Something like this? :)

    static final int ribbon_length = 255, H = 200; 
    
    void setup() {
      size(ribbon_length, H);
    }
    
    void draw() {
      String[] lines = loadStrings("input.txt");
      float p = float(split(lines[0], "=")[1]);
      int up_y = int(split(lines[1], "=")[1]);
      int wide = int(split(lines[2], "=")[1]);
      int high = int(split(lines[3], "=")[1]);
      float a = pow (ribbon_length, 1-p);
      float colour = 0;
      for (int step = 0; step <= 255; step++) { 
          colour = a * pow (step, p);
          fill(colour,0,0); 
          rect(wide*step, up_y, wide, high);
          noStroke();
       }
    }
    
  • this works half way

    static final int ribbon_length = 255, H = 200; 
    
    void setup() {
      size(ribbon_length, H);
      //size(700, 700);
    }
    
    void draw() {
      String[] lines = loadStrings("input.txt");
      float p = float(split(lines[0], "=")[1]);
      int up_y = int(split(lines[1], "=")[1]);
      int wide = int(split(lines[2], "=")[1]);
      int high = int(split(lines[3], "=")[1]);
      println (p+","+up_y+","+wide+","+high);
      float a = pow (ribbon_length, 1-p);
    
      color colour = 0;
      for (int step = 0; step <= 255; step++) {
        println(a * pow (step, p)); 
        colour = color( a * pow (step, p), 2, 2 );
        // colour=color(255, 2, 2);
        fill(colour); 
        // noFill();
        //  stroke(colour);
        noStroke();
        rect(wide*step, up_y, wide, high);
        noStroke();
      }
    }
    
  • edited February 2014

    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

    p=213
    u=113
    wide=17
    high=33
    
Sign In or Register to comment.