How to work with multiple datasets in p5.js?

Member

by jerad , in category: Javascript , 2 months ago

How to work with multiple datasets in p5.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 2 months ago

@jerad 

To work with multiple datasets in p5.js, you can load them into your sketch using the preload() function or by manually loading them with the loadTable() or loadJSON() functions. Once you have loaded your datasets, you can store them in variables and manipulate them as needed.


Here are some steps to work with multiple datasets in p5.js:

  1. Load your datasets using the preload() function:
1
2
3
4
5
let dataset1;

function preload() {
  dataset1 = loadTable('data1.csv', 'csv', 'header');
}


  1. Or load your datasets manually using loadTable() or loadJSON():
1
2
3
4
5
6
7
let dataset1;
let dataset2;

function setup() {
  dataset1 = loadTable('data1.csv', 'csv', 'header');
  dataset2 = loadJSON('data2.json');
}


  1. Once your datasets are loaded, you can access and manipulate them in your sketch:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function draw() {
  // Accessing data from dataset1
  for (let i = 0; i < dataset1.getRowCount(); i++) {
    let value = dataset1.getNum(i, 'value');
    // Do something with the data
  }

  // Accessing data from dataset2
  for (let i = 0; i < dataset2.length; i++) {
    let value = dataset2[i].value;
    // Do something with the data
  }
}


By following these steps, you can easily work with multiple datasets in p5.js and create visualizations or interactive applications that use data from different sources.