Skip to content
Related Articles

Related Articles

Improve Article

p5.TableRow setNum() Method

  • Last Updated : 18 Sep, 2020

The setNum() method of p5.TableRow in p5.js is used to store the given Float value to the given column of the table. The column can be specified by its column ID or column name.

Syntax:

setNum( column, value )

Parameters: This method accepts two parameters as mentioned above and described below: 

  • column: It is a String or Number that denotes the column name or ID of the column.
  • value: It is a number that specifies the value that has to be stored.

The example below illustrates the setNum() method in p5.js:

Example:



javascript




function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  text("Enter the index of the table " +
       "to be modified", 20, 20);
  
  rowInput = createInput();
  rowInput.size(30);
  rowInput.position(30, 40);
  
  colInput = createInput();
  colInput.size(30);
  colInput.position(80, 40);
  
  setBtn =
    createButton("Modify Given Row and Column");
  setBtn.position(30, 80);
  setBtn.mouseClicked(modifyTableData);
  
  // Create the table
  table = new p5.Table();
  
  // Add 5 columns and rows to the table
  for (let i = 0; i < 5; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
  
  for (let r = 0; r < 5; r++) {
  
    // Get the table row from the table
    let tableRow = table.rows[r];
  
    for (let c = 0; c < 5; c++) {
  
      // Set the value at the given
      // column of the table row
      tableRow.set(c, r * 100 + c);
    }
  }
  
  showTable();
}
  
function modifyTableData() {
    
  givenRow = int(rowInput.value());
  givenCol = int(colInput.value());
  
  if (givenRow < table.getRowCount() && 
      givenCol < table.getColumnCount()) {
  
        // Get the specified table row
        let tableRow = table.rows[givenRow];
  
        // Set a number value to the given
        // column of the table row
        tableRow.setNum(givenCol, -99999);
      }
      
  
  showTable();
}
  
function showTable() {
  clear();
  text("The value is modified using " +
       "the setNum() method", 20, 20);
    
  let colCount = table.getColumnCount();
  let rowCount = table.getRowCount();
  
  // Show all the columns present
  for (let c = 0; c < colCount; c++) {
    text(table.columns, 20 + 100 * c, 120);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < rowCount; r++) {
    for (let c = 0; c < colCount; c++) {
      text(table.getString(r, c),
           20 + 100 * c, 150 + 20 * r);
    }
  }
}

Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.TableRow/setNum




My Personal Notes arrow_drop_up
Recommended Articles
Page :