p5.Table setNum() Method

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

Syntax:

setNum( row, column, value )

Parameters: This function accepts three parameters as mentioned above and described below:

  • row: It is a Number that denotes the row ID of the row.
  • 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.

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

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

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 < 4; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
  
  setTableData();
}
  
function modifyTableData() {
    
  givenRow = int(rowInput.value());
  givenCol = int(colInput.value());
  
  if (givenRow < table.getRowCount() && 
      givenCol < table.getColumnCount())
  
    // Set a number value to the given
    // row and column
    table.setNum(givenRow, givenCol, -999);
  
  showTable();
}
  
function setTableData() {
  for (let r = 0; r < 4; r++) {
    for (let c = 0; c < 4; c++) {
  
      // Set the value at the given
      // column and row of the table
      table.set(r, c, r + " " + c);
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The value is modified using" +
       " the setNum() method", 20, 20);
  
  // Show all the columns present
  for (let c = 0; c < 4; c++) {
    text(table.columns,
         20 + 100 * c, 120);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 4; r++) {
    for (let c = 0; c < 4; c++) {
      text(table.getString(r, c),
           20 + 100 * c,
           140 + 20 * r);
    }
  }
}

chevron_right


Output:
setNum-ex

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.Table/setNum

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.