CSV table get row in Class

edited June 2017 in Questions about Code

I am merging previous code with the 'Load save table' example in Processing. When merging and adapting to using a Class I am not getting the expected behaviour I had previously.

I am attempting to recreate the stepping through each rowcount as in the working code below.

 if (i< table.getRowCount()) {
    row = table.getRow(i);
    k=row.getInt("y");
    x=row.getInt("x");
  }

  //draw white ellipse visual
  ellipse(x*diam, y, diam, diam);

  // step next circle up
  y+=Step;

  // -- if off the set upper limit of the line,
  // swing right 


  if (frameCount%(k/Step)==0) {
    x+=Step;

    y=starty;
    // increment row counter
    i=i+1;
  }

However, when using a Class I am making a mistake somewhere parsing the i variable from the Class method move().This is stopping the i variable incrementing.

I am expecting k to print out the data in column named "y" in the csv file. I am expecting i to print out a number incrementing by 1 each time variable y reaches the value of k.

From printing out my variables i,k,x

k is successfully getting the data from csv file column named "y"

x is successfully getting the data from csv file column named "x"

y is successfully incrementing

i is not incrementing

y is not registering it has reached k.

Below is the code using a Class.

    // An Array of Dot objects

    Dot[] dots;
    // A Table object
    Table table;
    float diam=10.0;
    int i;

    void setup() {  
      size(560, 420);
      loadData();
      background(0);
      // set the animation speed
      frameRate(10);
    }

    void draw() {
      // Display all dots
      for (Dot d : dots) {
        d.display();
        d.move();
      }

    }

    void loadData() {
      // Load CSV file into a Table object
      // "header" option indicates the file has a header row
      table = loadTable("data.csv", "header");

      // The size of the array of Dot objects is determined by the total number of rows in the CSV
      dots = new Dot[table.getRowCount()]; 

      // You can access iterate over all the rows in a table
      int rowCount = 0;
      for (TableRow row : table.rows()) {
        // You can access the fields via their column name (or index)
        if (i< table.getRowCount()) {
          row = table.getRow(i);
          float x = row.getFloat("x");
          float k = row.getFloat("y");
          float d = row.getFloat("diameter");


          //String n = row.getString("name");
          // Make a fot object out of the data read
          dots[rowCount] = new Dot(i,k, x, d);

          rowCount++;
        }
      }

      //  check to see if at end of data
      if (i == table.getRowCount()) {
        i = 0;//if so, loop back to first row
      }
    }

    //
   //DOT CLASS
   //
    class Dot {
      float x =10.0;
      float y=10.0;
      float diam=10.0;
      float k;
      int step=10;
      int startx =10;
      int starty=10;

      float i=0;

      Dot(float i_,float k_, float x_,float diam_) {
        k=k_;
        x=x_;
        i=i_;
        diam=diam_;
      }




      void display() {
        noStroke();
        fill(255);
        // draw a circle
        ellipse(x*diam, y, diam, diam);
      }

      void move() {
        // move next circle to the right
        y+=step;

        // -- if off the right edge of the line,
        // swing down and all the way to the left
        if (frameCount%(k/step)==0) {
          x+=step;
          y=starty;
          // increment row counter
          i=i+1;
        }

        // if off the bottom of the page, stop
        if (x>=width) {
          fill(0);
          text("done", width/2, height/2);
          noLoop();
        }
      }
    }

I have also tried to include an animate() function as my thoughts were the loadData() function only occurs once as it is called in the void setup() so cannot be used to call variables in the move() method. However, it results in one static dot rather than dots incrementing down and right as the i variable increments.

void animate() {
  // initial location and size
  startx =10;
  starty =10;

  Step = 10;

  // Create a new row
  TableRow row = table.getRow(0);
  // Set the values of that row
  float x = row.getFloat("x");
  float k = row.getFloat("y");


  //if (frameCount%((mh+10)/Step)==0) {
  if (frameCount%(k/Step)==0) {
    x+=Step;

   int  y=starty;
    // increment row counter
    i=i+1;
  }
  //  check to see if at end of data
  if (i == table.getRowCount()) {

    i = 0;//if so, loop back to first row
  }


  // And reloading it
  loadData();
}

This also seems like a repeat of what should be happening in move() method. Any help or pointers in the direction I'm missing are greatly appreciated. Thanks

Answers

  • please post the first 5 lines of your csv to get help

    the working code above, the one you posted first, is wrong

    this is caused by short variable names

    This : //draw white ellipse visual ellipse(x*diam, y, diam, diam);

    instead of y it should be k imho

    The rest of the code is also unclear.

    y+=Step; // ????????????
    
    
    // ???????????????????????????
     if (frameCount%(k/Step)==0) { // ???
       x+=Step; // ????? x gets read from table...
    
       y=starty;
       // increment row counter
       i=i+1;
     }
    

    I would do it like this:

    if (i< table.getRowCount()) {
       row = table.getRow(i);
       k=row.getInt("y");
       x=row.getInt("x");
     }
    
     //draw white ellipse visual
     ellipse(x*diam, k, diam, diam);
    
    i++; 
    
    if (i >= table.getRowCount()) { 
        i=0; // reset 
    } 
    
  • Sorry for the confusion. The first set of code is a snippet from the code below. It is value k not y because y continuously steps and k is the point in which it stops. It is when merging this with the load save table example and Class structure I am doing something wrong.

    int w = 112;
    int h = 84;
    int diam = 10;
    int xStep;
    int x, y, k;
    int startx, starty;
    int Step;
    
    /********text file***********/
    String[] lines;
    int index = 0;
    Table table;
    TableRow row;
    int i = 0;
    
    
    void setup() {
      size(560, 420);
      background(0);
      noStroke();
      fill(255);
      frameRate(10);
    
      /********CSV file setup***********/
    
      table = loadTable("data.csv", "header");
      row = table.getRow(0);
      // initial location and size
      startx = x = 10;
      starty = y = 10;
    
      Step = 10;
    
    
    }
    
    void draw() {
    
      /********CSV file***********/
    
    
      if (i< table.getRowCount()) {
        row = table.getRow(i);
        k=row.getInt("y");
        x=row.getInt("x");
      }
    
      //draw white ellipse visual
      ellipse(x*diam, y, diam, diam);
    
      // step next circle up
      y+=Step;
    
    
    
      // -- if at the set upper limit of the line,
      // swing right 
    
    
      if (frameCount%(k/Step)==0) {
        x+=Step;
    
        y=starty;
        // increment along row counter
        i=i+1;
      }
    
      // if off the right of width/screen, stop and reset to beginning.
      //refresh background and start at 0
      if (x>=84) {
    
    
        background(0);
        x=0;
        index=0;
    
      }
    
      //  check to see if at end of data
      if (i == table.getRowCount()) {
        i = 0;//if so, loop back to first row
      }
    }
    

    the first six lines of the CSV are

    x, y
    1, 133

    2, 111

    3, 45

    4, 78

    5, 139

    6, 117

    thanks for your time

  • but your problem is really in the code with the class, right?

    this you don't need

     if (i< table.getRowCount()) {
           row = table.getRow(i);
    

    and this:

       //  check to see if at end of data
       if (i == table.getRowCount()) {
         i = 0;//if so, loop back to first row
       }
    
  • yes my problem is in the Class and the move method not stepping through the coded movements but instead just being one static dot as variable 'i' does not increment from 0.

    I removed the table.getRow(i) code but this hasn't resolved the move() method issue. Sorry, I don't think I am explaining it particularly well.

  • I have no idea

  • no problem thank you for trying anyway

  • what is that supposed to do :

      if (frameCount%(k/step)==0) {
    

    apart from that: what do you want to achieve?

    Display the rows one by one or what?

  • yes, display row one by one. Ellipses are drawn on the y-axis every frame until they are at a height of variable k. At which point x is stepped right 10 pixels and the ellipses start drawing down the y-axis of the canvas again.

  • Answer ✓

    I was able to run your latest full version and I didn't have any problems. You have two conditions: When x>=84 and when you reach the end of the table. They are competing without constraining each other. When your warp around your data, you don't know if you have reach the x=84 mark or not. When you reach the x=84 mark, you don't know how far you are into the data... that could be the problem. However, I will need a full copy of your csv file to try to reproduce your issue.

    Kf

Sign In or Register to comment.