p5.js 2-dimensional float array
in
Processing with Other Languages
•
6 months ago
I'm trying to port my Processing scatterplot to Processing.js
My datapoints are stored in a txt file in this format:
- "V1" "V2"
- "monitor/noun/BL_19990106_01/31625" -0.12131594619236 -0.0714908515733397
- "monitor/noun/BL_19990120_01/56648" -0.322780246647713 0.108432169208681
- "monitor/noun/BL_19990126_01/43890" -0.161315099543655 0.0210510446479526
When I run my code with p5.js, my array gets filled with NaN instead of the floats.
Any suggestions?
The MWE:
- // first line of the file should be the column headers
- // first column should be the row titles
- // all other values are expected to be floats
- // getFloat(0, 0) returns the first data value in the upper lefthand corner
- // files should be saved as "text, tab-delimited"
- // empty rows are ignored
- // extra whitespace is ignored
- class FloatTable {
- int rowCount;
- int columnCount;
- float[][] data;
- string[] rowNames;
- string[] columnNames;
- FloatTable(string filename) {
- string[] rows = loadStrings(filename);
- string[] columns = split(rows[0], TAB);
- columnNames = subset(columns, 1); // upper-left corner ignored
- columnCount = columnNames.length;
- rowNames = new string[rows.length-1];
- data = new float[rows.length-1][];
- // start reading at row 1, because the first row was only the column headers
- for (int i = 1; i < rows.length; i++) {
- // split the row on the tabs
- string[] pieces = split(rows[i], TAB);
- // copy row title
- rowNames[rowCount] = pieces[0];
- // copy data into the table starting at pieces[1]
- data[rowCount] = float(subset(pieces, 1));
- // increment the number of valid rows found so far
- rowCount++;
- }
- // resize the 'data' array as necessary
- data = (float[][]) subset(data, 0, rowCount);
- }
- int getRowCount() {
- return rowCount;
- }
- string getRowName(int rowIndex) {
- if(rowIndex >= 0)
- return rowNames[rowIndex];
- else
- return null;
- }
- string[] getRowNames() {
- return rowNames;
- }
- // Find a row by its name, returns -1 if no row found.
- // This will return the index of the first row with this name.
- // A more efficient version of this function would put row names
- // into a Hashtable (or HashMap) that would map to an integer for the row.
- int getRowIndex(string name) {
- for (int i = 0; i < rowCount; i++) {
- if (rowNames[i].equals(name)) {
- return i;
- }
- }
- //println("No row named '" + name + "' was found");
- return -1;
- }
- // technically, this only returns the number of columns
- // in the very first row (which will be most accurate)
- int getColumnCount() {
- return columnCount;
- }
- string getColumnName(int colIndex) {
- return columnNames[colIndex];
- }
- string[] getColumnNames() {
- return columnNames;
- }
- float getFloat(int rowIndex, int col) {
- return data[rowIndex][col];
- }
- }
1