Populating 1D and 2D arrays with single CSV file

edited February 2015 in Questions about Code


I have a table stored as a CSV file:

Event1 Event2 Percent Frequency
   1      2      0.5     100
   1      5      0.48     80
   5      7      0.36     55

I'd like to be able to create a 2D array with the event columns, and 2 other 1D arrays from the percent and frequency columns. Something similar was done in another post (http://forum.processing.org/two/discussion/6668/how-to-convert-a-csv-file-into-arrays-one-1d-and-one-2d), but instead of a 2D array, a PVector array was used. Is there any way to do this by populating a 2D array?

I've been able to read the file, but I'm not sure how to populate the arrays:

int [][] events;
  int colCount = 0;
  int rowCount = 0;
float [] perc;
int [] freq;    

String file = "file.csv";

    void setup(){

      Table table = loadTable(file, "header, csv");

      for (TableRow row : table.rows()){
      int event1 = row.getInt("Event1");
      int event2 = row.getInt("Event2");
      float percent = row.getFloat("Percent");
      int frequency = row.getInt("Frequency");

Thanks in advance!


  • edited February 2015 Answer ✓

    ... but I'm not sure how to populate the arrays.

    • Well, I can't see any array assignments within your loop!
    • You haven't instantiated the arrays either! :-<
    • You merely extract the data but don't permanently store them into arrays!
    • I've adapted the code I had there in the other forum thread. Check it out: B-)

    // forum.processing.org/two/discussion/9570/
    // populating-1d-and-2d-arrays-with-single-csv-file
    // 2015-Feb-25
    final String CSV = "file.csv";
    final int A = 0, B = 1, TYPES[] = {
      Table.INT, Table.INT, Table.FLOAT, Table.INT
    byte[][] events;
    float[]  cents;
    short[]  freqs;
    Table t = loadTable(CSV, "header");
    int idx = 0, lines = t.getRowCount();
    events = new byte[lines][2];
    cents  = new float[lines];
    freqs  = new short[lines];
    for (TableRow tr : t.rows()) {
      events[idx][A] = (byte) tr.getInt("Event1");
      events[idx][B] = (byte) tr.getInt("Event2");
      cents[idx] = tr.getFloat("Percent");
      freqs[idx++] = (short) tr.getInt("Frequency");
    for (idx = 0; idx != lines; ++idx) {
      print("Row #" + nf(idx, 2) + ": \t");
      println(events[idx][A], events[idx][B], cents[idx], freqs[idx]);
  • Perfect, thanks for your help GoToLoop! I'm still quite new to Processing, so I apologise for my crappy code. I'm going to have a close look to understand this and learn from it ;)

  • Hi, one more question, as an extension to this: I have drawn a set of rectangles using a for loop.

    for (int i = 270; i < 390; i = i+31) {  //(int start point;end point;increment)
    for (int j = 15; j < 630; j = j+12) {    //total 39 rows. B=49
    rect(i, j, 30, 8); //rect(x, y, width, height)

    How can I colour these rectangles using values from the 2D array? I would assume that I'd have to read through the array to get the values, then use an if statement to colour the rectangles (e.g. if value=1, colour red). I haven't been able to find anything in my searches.

Sign In or Register to comment.