Creating Tables

Hi everyone. So I'm now entering the world of creating, fetching and parsing data. Very very early stages at the moment and I'm already sweating. My task was to create a table that would save mouseX and mouseY data onto a table of max 10 lines. Pretty simple, pretty straight forward, but it doesn't work. I spent quite a good deal of time trying to understand why, iterating but I keep on getting the same error: "Array index out of range:0" ... Help? Here's the code. Thanks!

Table table;
int counter=0;

void setup() {
  size(); 
  table= new Table();
  table.addColumn("id", Table.INT);
  table.addColumn("valueX", Table.INT);
  table.addColumn("valueY", Table.INT);
  saveTable(table, "data/new.csv");
}


void draw() {
  TableRow row=table.addRow();
  row.setInt("id", counter);
  row.setInt("valueX", mouseX);
  row.setInt("valueY", mouseY);

  counter++;



  //if (table.getRow()>10) {
  //  table.removeRow(0);


  //}
}

void mousePressed() {
  exit();
}

Answers

  • _vk_vk
    edited February 2016

    I think you need to create new rows, actually Tablerow to add your data. See this example

    https://processing.org/reference/Table.html

    Sorry I misread your post.

  • /**
     * CSV Mouse Coords (v2.0)
     * GoToLoop (2016-Feb-04)
     * forum.Processing.org/two/discussion/14756/creating-tables
     */
    
    static final String FILENAME = "MouseCoords.csv";
    static final boolean ONLINE = 1/2 == 1/2.;
    static final int COORDS = 10, FPS = 5;
    
    final int[] coords = new int[COORDS];
    
    void setup() {
      size(600, 400);
      noLoop();
      frameRate(FPS);
    }
    
    void draw() {
      background((color) random(#000000));
      if (!ONLINE)  frame.setTitle(str(frameCount - 1));
    }
    
    void mouseMoved() {
      final int fc = frameCount - 1, mx = mouseX, my = mouseY;
      println(fc, mx, my);
      coords[fc] = my<<020 | mx;
    
      if (frameCount == COORDS) {
        saveCSV();
        exit();
      }
    
      redraw();
    }
    
    void saveCSV() {
      final String[] csv = new String[COORDS];
      final String path = ONLINE? FILENAME : dataPath(FILENAME);
    
      for (int i = 0; i != COORDS; ++i) {
        int pair = coords[i];
        int mx = pair & 0xFFFF;
        int my = pair >>> 020;
        csv[i] = i+1 + "," + mx + "," + my;
      }
    
      saveStrings(path, csv); // localStorage['MouseCoords.csv'];
    }
    
Sign In or Register to comment.