Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptp5.Table setNum() Method

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:




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);
    }
  }
}


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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments