TableRow returning NaN values

Hello – I’m working on a project that uses CSV data to drive the graphics; I created a Show Control class that reads all of the csv files in a folder, and then grabs the next available row from the current file to provide values for the graphics. However, I’m running into a strange error where every /other/ row returns NaN for the cell values, and then the other rows return valid values.

The class is here:

class ShowControl {
  ArrayList<Table> mShowList = new ArrayList<Table>(10);
  int mShowIndex = 0;
  int mCurrentShowLocation = 0;
  Table mCurrentShow = null;
  
  ShowControl() {
    String[] fileList;
    File file = new File(dataPath(""));
    if (file.isDirectory()) {
      fileList = file.list();
    } else {
      fileList = null;
    }
    
    printArray(fileList);
    
    if(fileList != null) {
      for(String filename : fileList) {
        Table t = loadTable(filename, "header");
        mShowList.add(t);
      }
    }
    
    if(mShowList.size() > 0) {
      mCurrentShow = mShowList.get(mShowIndex);
    }
  }
  
  void update() {
    if(mCurrentShowLocation < mCurrentShow.getRowCount() - 1) {
      mCurrentShowLocation++;
    }
    else {
      mShowIndex++;
      mShowIndex = mShowIndex % mShowList.size();
      println("Current Show Index: " + mShowIndex);
      mCurrentShow = mShowList.get(mShowIndex);
      mCurrentShowLocation = 0;
    }
  }
 
  void restartShow() {
    mCurrentShowLocation = 0;
  }
  
  float getValue(String fieldName) {
    println("Show Location: " + mCurrentShowLocation);
    println("Field Name: " + fieldName);
    println(mCurrentShow.getFloat(mCurrentShowLocation, fieldName));
    return mCurrentShow.getFloat(mCurrentShowLocation, fieldName);
  }
  
}

and the code that I’m calling in draw() is this:

  mShowControl.update();
  float pos_x = mShowControl.getValue("Gallery X");
  float pos_y = mShowControl.getValue("Gallery Y");
  float pos_z = mShowControl.getValue("Gallery Z");

which prints data like this:

Show Location: 155
Field Name: Gallery X
69.0
Show Location: 155
Field Name: Gallery Y
69.0
Show Location: 155
Field Name: Gallery Z
2.37
Show Location: 156
Field Name: Gallery X
NaN
Show Location: 156
Field Name: Gallery Y
NaN
Show Location: 156
Field Name: Gallery Z
NaN

The csv file looks fine in every way, so I’m very confused how even rows provide incorrect data but odd rows work fine!

Does anyone have any thoughts?

1 Like

first i did not get into your code,
but want start differently:

sadly

  • array
  • arraylist
  • table

have very different style ( and functionalities )
but what i not understand is your idea of ?copy?
a table to a arraylist
example see
https://processing.org/reference/TableRow.html
so as you might want to build a screen with the content of a table row
( one by one by like [NEXT] button… ?)
you just load a record ( line ) ONE_ROW
and ( that part i also not see ) show its columns/fields ? by

text(ONE_ROW.getString(“thatColumn”),posx,posy);

but if you want someone here to dig deeper into your code
post a running minimal code example
and also a table_file.csv example;

a easy way:

// https://discourse.processing.org/t/tablerow-returning-nan-values/7955

Table mytable;
TableRow one_row;
String scol, myfile = "data/table.csv";     // with column headers
int xpos, ypos, dx, line=0, crow, ccol;

void setup() {
  size(200, 200);  
  mytable = loadTable(myfile, "header");
  crow = mytable.getRowCount();  
  ccol = mytable.getColumnCount();
  println("file: "+myfile+" rows: "+crow+" cols: "+ccol);
  println("use: key [n] for next");
}

void draw() {
  background(200, 200, 0);
  draw_row(line);
}

void draw_row(int line) {
  one_row = mytable.getRow(line);
  scol = "Gallery ";
  xpos = 10; ypos =20; dx = 60;
  fill(0,0,200);
  text(scol+'X',xpos,ypos); xpos += dx;
  text(scol+'Y',xpos,ypos); xpos += dx;
  text(scol+'Z',xpos,ypos); 
  xpos = 10; ypos += ypos;
  text(nf(one_row.getFloat(scol+'X'),1,1),xpos,ypos); xpos += dx;
  text(nf(one_row.getFloat(scol+'Y'),1,1),xpos,ypos); xpos += dx;
  text(nf(one_row.getFloat(scol+'Z'),1,1),xpos,ypos); 
}


void keyPressed() {
  if (key == 'n') line++;
  if (line >= crow ) line =0;
}

/*
data/table.csv content:

Gallery X,Gallery Y,Gallery Z,
11.0,12.0,13.0,
21.0,22.0,23.0,

*/


//UPDATE_____________________________
i did try your code ( with what i needed to test it ) and it worked.

ShowControl mShowControl;

class ShowControl {
  ArrayList<Table> mShowList = new ArrayList<Table>(10);
  int mShowIndex = 0;
  int mCurrentShowLocation = 0;
  Table mCurrentShow = null;

  ShowControl() {
    String[] fileList;
    File file = new File(dataPath(""));
    if (file.isDirectory()) {
      fileList = file.list();
    } else {
      fileList = null;
    }

    printArray(fileList);

    if (fileList != null) {
      for (String filename : fileList) {
        Table t = loadTable(filename, "header");
        mShowList.add(t);
      }
    }

    if (mShowList.size() > 0) {
      mCurrentShow = mShowList.get(mShowIndex);
    }
  }

  void update() {
    if (mCurrentShowLocation < mCurrentShow.getRowCount() - 1) {
      mCurrentShowLocation++;
    } else {
      mShowIndex++;
      mShowIndex = mShowIndex % mShowList.size();
      println("Current Show Index: " + mShowIndex);
      mCurrentShow = mShowList.get(mShowIndex);
      mCurrentShowLocation = 0;
    }
  }

  void restartShow() {
    mCurrentShowLocation = 0;
  }

  float getValue(String fieldName) {
    println("loc: " + mCurrentShowLocation+" field: " + fieldName+" val:"+mCurrentShow.getFloat(mCurrentShowLocation, fieldName));
    return mCurrentShow.getFloat(mCurrentShowLocation, fieldName);
  }
}

void setup() {
  size(200,200);
  mShowControl = new ShowControl();
}

void draw() {
 background(200,200,0);
  mShowControl.update();
  float pos_x = mShowControl.getValue("Gallery X");
  float pos_y = mShowControl.getValue("Gallery Y");
  float pos_z = mShowControl.getValue("Gallery Z");
}


/*
data/table.csv

Gallery X,Gallery Y,Gallery Z,
11.0,12.0,13.0,
21.0,22.0,23.0,

data/table2.csv

Gallery X,Gallery Y,Gallery Z,
211.0,212.0,213.0,
221.0,222.0,223.0,

*/
// show
/*
Current Show Index: 0
loc: 0 field: Gallery X val:11.0
loc: 0 field: Gallery Y val:12.0
loc: 0 field: Gallery Z val:13.0
loc: 1 field: Gallery X val:21.0
loc: 1 field: Gallery Y val:22.0
loc: 1 field: Gallery Z val:23.0
Current Show Index: 1
loc: 0 field: Gallery X val:211.0
loc: 0 field: Gallery Y val:212.0
loc: 0 field: Gallery Z val:213.0
loc: 1 field: Gallery X val:221.0
loc: 1 field: Gallery Y val:222.0
loc: 1 field: Gallery Z val:223.0
*/


so ?? one of your test files is bad??

1 Like

Do you experience the same problem if you use another csv file? Can you share a small portion of the file here to see the structure? It will also help to reproduce the problem.

Kf

1 Like

Some useful Table methods: :sunglasses:

  1. setColumnTitles(): Forum.Processing.org/two/discussions/tagged?Tag=setcolumntypes()

  2. setMissingFloat(): Forum.Processing.org/two/discussion/17738/test-a-variable-type

  3. Table docs: Processing.GitHub.io/processing-javadocs/core/processing/data/Table.html

1 Like

Thanks for your help everyone – I went in to copy/paste a subsection of my CSV file, and realized that the software I was using to generate it was adding extra carriage returns that weren’t showing up in my text editor. This resulted in the every-other-line being null data, and now that I fixed it, everything is working!

1 Like