Data from files

This is part 3 of an N part series detailing how I make my animations.

Prev Next

Now we are going to read data from the CSV file.

Replace the contents of setup() with loadData(); You will notice that it’ll be underlined in red squiggles and a red bar at the bottom

So let’s make loadData() exist. Copy the following and paste it at the end of the code on a line past the closing curly bracket of draw()

void loadData()
{

}

All the nasty red should have gone away.

Now in loadData()¹ add the following lines:

String[] data = loadStrings("weekly_in_situ_co2_mlo.csv");
print(data[44]);

Which should display 1958-03-29, 316.19 in the console.

How about that? We have data!

loadStrings("weekly_in_situ_co2_mlo.csv") is a function that reads all the lines from our data file and puts them line-by-line into a string array. Each of those lines can then be accessed by their line number minus 1 of the original file. Which we do in print(data[44]) which displays in the console line 45 of the original file. Remember, we are skipping 44 lines of informational text.

Your code should look something like the following:

void setup()
{
loadData();
}
void draw()
{
}
void loadData()
{
String[] data = loadStrings("weekly_in_situ_co2_mlo.csv");
print(data[44]);
}

Unfortunately the data array can only be accessed in the loadData() function. It cannot be accessed by the setup() of the draw() function. So to get around this we will create a global array of strings³.

At the very top of the code add:

String[] _data;

We have just created an array of strings called _data⁴ that is accessible to all the functions in the code.

Now replace contents of loadData() with

_data = loadStrings("weekly_in_situ_co2_mlo.csv");

And in setup() after loadData(); add println(_data[44]);

Your code should now look something like this:

String[] _data;
void setup()
{
loadData();
println(_data[44]);
}
void draw()
{
}
void loadData()
{
_data = loadStrings("weekly_in_situ_co2_mlo.csv");
}

Running that should display 1958-03-29, 316.19 again in the console.

We’ve now got past many tricky concepts which can trip up many beginner coders, in my next post we’ll get into plotting the data!


¹ By convention function names start with a lowercase letter with the next word’s starting letter capitalised. This is called “camelCased”² and is useful to be able to identify functions from other things which will have their own style. There’s nothing magic about it; it’s only for humans to understand, computers don’t care what you do.

² Get it? Cause of the humps!

³ There would have been many boos and gnashing of teeth from seasoned developers if any of them were listening in at the mention of global variables. Tell them that this is really just a private class level field and that they really should spend their energies on other things.

⁴ Another naming convention is to prefix a global variable with an underscore. There is nothing magical about this character, the computer don’t give a damn.