How do I duplicate the class for each row in a .csv?

edited November 2015 in Questions about Code

I am trying to create a sine wave for every row of data in the .csv but no matter what I try it doesn't seem to work. Do I use arrays? Can someone please tell me or show me how?

Wave wave;
Table neutral;
//A = Amplitude
//C = Horizontal Displacement
//B = Period
void setup() {
  size(500, 500);

  neutral = loadTable("Test.csv", "header");
  println(neutral.getRowCount() + " total rows in table");


  for (TableRow row : neutral.rows ()) {
    //float A = row.getFloat("A");
    //float B = row.getFloat("B");
    //float C = row.getFloat("C");
    //println(A + " : " + B + " : " + C);
    for (int i = 0; i < neutral.getRowCount (); i++) {
      float Ai = neutral.getFloat(i, 4);
      float Bi = neutral.getFloat(i, 5);
      float Ci = neutral.getFloat(i, 6);


      println(Ai + " : " + Bi + " : " + Ci);
      wave = new Wave(0, Ai, Bi/100, 100);
    }
  }
}

void draw() {
  background(255);
  translate(width/2, height/2);
  wave.update();
}

Answers

  • Answer ✓

    Yes you should use an array if you have more than just a few waves. Define one with a size equal to the number of rows in your table:

    Wave[] waveArray = new waveArray[neutral.getRowCount()];

    The two nested loops are strange, you don't need to iterate over the rows twice. Just delete the outer loop, then you can access the current row like this:

    for (int i=0; i< neutral.getRowCount(); i++) {
      TableRow row = neutral.getRow(i);
    
      ...
      ...
    

    To get the value of a column you can use row.getFloat() but it only has one parameter (as far as i know). So this would assign the value of the 5th column to the variable Ai:

    float Ai = row.getFloat(4);

    Then you can create a Wave-Object and store it inside of your array:

      waveArray = new Wave(0, Ai, Bi/100, 100);
    

    I didn't test the code, since i don't have your csv-file, i hope it doesn't have errors.

  • Thank you :)

  • Answer ✓

    Uups.. i'm sorry, made a mistake the last code-snippet should be

    waveArray[i] = new Wave(0, Ai, Bi/100, 100);

  • edited November 2015

    That's ok but now I seem to be getting a null pointer exception in the 2nd row of the code. Do you know why?

    Table neutral;
    Wave[] waveCount = new Wave[neutral.getRowCount()];
    
    int width, height;
    color fillColor;
    float diameter = 6.0;
    
    void setup() {
      // DEFINE THE STAGE
      width = 500;
      height = 550;
      size(500, 550);
      // THE SAME FILL IS USED FOR ALL waveCount
      noFill();
      stroke(0);
      // CREATE A COLLECTION OF waveCount
      neutral = loadTable("Test.csv", "header");
      for (TableRow row : neutral.rows ()) {
        for (int i = 0; i < neutral.getRowCount (); i++) {
          float Ai = neutral.getFloat(i, 4);
          float Bi = neutral.getFloat(i, 5);
          float Ci = neutral.getFloat(i, 6);
          Wave wave = new Wave(0, Ai, Bi/100, 100);
          waveCount[i] = wave;
        }
      }
    }
    
    void draw() {
      translate(width/2, height/2);
      background(255);
    
      for (int i = 0; i < neutral.getRowCount (); i++) {
        waveCount[i].update();
      }
    }
    
  • Answer ✓

    That is because "neutral" doesn't exist there. Just declare the array at the top:

    Wave[] waveCount;

    and after you load the table in line 17 you can initialize it:

    waveCount = new Wave[neutral.getRowCount()];

  • Thank you :)

Sign In or Register to comment.