p5.Table set() Method

The set() method of p5.Table in p5.js is used to store the given 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 name.

Syntax:

set( 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 String or Number that specifies the value that has to be stored.

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

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  text("Click on the button to set the" +
       " values of the table", 20, 20);
  
  setBtn = createButton("Add Table Values");
  setBtn.position(30, 40);
  setBtn.mouseClicked(setTableData);
  
  // 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();
  }
}
  
function setTableData() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
  
      // Set the value at the given
      // column and row of the table
      table.set(r, c, r + " " + c);
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
    text("Click on the button to set the" +
       " values of the table", 20, 20);
  
  // Show all the columns present
  for (let c = 0; c < 5; c++) {
    text(table.columns, 20 + 80 * c, 80);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      text(table.getString(r, c),
           20 + 80 * c,
           100 + 20 * r);
    }
  }
}

chevron_right


Output:
set-ex1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  changeBtn =
    createButton("Modify Table");
  changeBtn.position(30, 40);
  changeBtn.mouseClicked(modifyTable);
  
  // 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();
  }
  
  setTableData();
  
  // Display the table
  showTable();
}
  
function setTableData() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
  
      // Set the value at the given
      // column and row of the table
      table.set(r, c, r + " " + c);
    }
  }
  
  showTable();
}
  
function modifyTable() {
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
  
      // If the index of the table is
      // a diagonal, set it to "XX"
      if (r == c) table.set(r, c, "XX");
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
   text("Click on the button to set the" +
       " values of the table", 20, 20);
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      text(table.getString(r, c),
           20 + 80 * c,
           80 + 20 * r);
    }
  }
}

chevron_right


Output:
set-ex2

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/set

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.