Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

Use MATLAB ‘timetable’ to Merge ThingSpeak Data Channels 7

Posted by Hans Scharler,

We released a new version of MATLAB® and it’s available now for every ThingSpeak user. MATLAB R2016b includes many new features that make it easy to work with time-stamped tabular data, manipulate, compare, and store text data efficiently, and find, fill, and remove missing data.

With multiple sensors around my house or office, I want to be able to send data to multiple ThingSpeak channels. But, when I want to perform data analysis, I have a hard time working with data from multiple channels. The channels do not have the same time stamps and are out-of-sync with each other.

With R2016b of MATLAB, I am able to use the new timetable data container. Once the data is a stored as a timetable, I can perform powerful operations such as retime, synchronize, and rmmissing.

In this example, I have two sensors outside of my office here in Natick, MA. One sensor is a temperature sensor that is sending data to ThingSpeak channel 163540. My other sensor is writing humidity data to channel 163545. Both channels are public. My goal is to plot temperature versus humidity over one time series. To accomplish this, I will use timetable and synchronize inside of a new MATLAB Visualization on ThingSpeak.

% Read from the temperature channel
temperatureTT = thingSpeakRead(163540,'Fields',1,'NumPoints',100,'outputFormat','timetable');

% Read from the humidity channel
humidityTT = thingSpeakRead(163545,'Fields',1,'NumPoints',100,'outputFormat','timetable');

% Synchronize two timestables and fill in missing data using linear interpolation
TT = synchronize(temperatureTT,humidityTT,'union','linear')

% Plot Temperature and Humidity over time
plotyy(TT.Timestamps,TT.Temperature,...
       TT.Timestamps,TT.Humidity);
        
title('Temperature and Humidity Synchronized From Two Channels')
xlabel('Temperature and Humidity in Natick, MA')
legend('Temperature','Humidity')

The first part of the script reads in ThingSpeak data from two different channels and stores the data in two timetables. Once the data is stored in a timetable, I am able to take advantage of synchronize. With synchronize, I can combine both timetables with one time series and fill in missing data using linear interpolation. This results in a plot that shows my data over time without any missing data. To create the plot, I signed into ThingSpeak, selected Apps, and created a new MATLAB Visualization with my MATLAB code.

All ThingSpeak users are able to try this example or explore the other new MATLAB features directly on ThingSpeak. I will leave my temperature (163540) and humidity (163545) channels public, so you can try out timetable example without having to connect devices to ThingSpeak.

7 CommentsOldest to Newest

Cyber1000 replied on : 1 of 7

Hey nice, that worked perfect for me.
How did you get the legend (Temperature, Humidity), they don’t show up with this code (at least for me). am I missing something?
Thanks!

Glad that the example worked for you. In the example code, the box looks like it is cutting off 3 lines that need to be scrolled to see.

title(‘Temperature and Humidity Synchronized From Two Channels’)
xlabel(‘Temperature and Humidity in Natick, MA’)
legend(‘Temperature’,’Humidity’)

I hope that helps.

Cyber1000 replied on : 3 of 7

Think it was too late when I did this, too late for scrolling 🙂

Thanks that solved my problem!
Greetings from Austria

FBeauchemin replied on : 4 of 7

Exactly what i need but i have an error when i execute the code.

The code stop to plotyy() and i have this message.

“Unrecognized variable name ‘Temperature’ “

thanks

Temperature corresponds to what I named my field in my channel. You can name the field what you want, but the name will have to match the code.

Spencer Koch replied on : 7 of 7

This is really nice. I need to combine a bunch of different channels to make a dashboard for sensors all around my house!

SilverNodashi replied on : 9 of 7

I tried this example on 6 fields but it failed. With 2 fields it works fine but not with 6. What do I need to change, in order to read more than 2 fields successfully?

Add A Comment

What is 4 + 10?

Preview: hide