Main Content

Compare Temperature Data from Three Different Days

This example shows how to read data from an existing ThingSpeak™ channel and generate a plot with three series on the same axis. In the example, as a starting point, you use one of the code templates that the MATLAB Analysis and MATLAB Visualizations apps in ThingSpeak provide.

ThingSpeak channel 12397 contains weather data from a weather station on top of a parking garage on the MathWorks® campus in Natick, MA. Field 4 contains the temperature measurement.

Create a MATLAB Visualization from Template Code

To create a visualization of three series of data from a ThingSpeak channel, you can write a MATLAB script using the code template provided in the MATLAB Visualizations app.

Go to the Apps tab and click MATLAB Visualizations. Click New, choose Compare temperature data from three different days, and click Create.

Visualize Your Data

ThingSpeak populates the MATLAB Code field with the code to generate a three-series line plot.

1) Set the variables for communicating with ThingSpeak. In this example, the read API key is unnecessary because the weather station channel is public. If you are reading from your own channel, you can modify these values.

readChannelID = 12397;
TemperatureFieldID = 4;
readAPIKey = '';

2) Use thingSpeakRead to retrieve the data for each day. Specifying 'DateRange' allows you to set the stop and start dates for your data collection.

temperatureDay1 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,'dateRange',...
    [datetime('today')-days(1),datetime('today')],'ReadKey',readAPIKey); 

temperatureDay2 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,'dateRange',...
    [datetime('today')-days(2),datetime('today')-days(1)],'ReadKey',readAPIKey); 

temperatureDay3 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,'dateRange',...
    [datetime('today')-days(3),datetime('today')-days(2)],'ReadKey',readAPIKey);

3) Create an array of durations that matches the length of each dataset. The length of each series fluctuates depending on the exact start time. The measurements are taken every minute, but are not aligned on each day.

myTimes1=minutes(1:length(temperatureDay1));
myTimes2=minutes(1:length(temperatureDay2));
myTimes3=minutes(1:length(temperatureDay3));

4) Use plot and hold to generate the plots. Set the legend to differentiate the series using legend. Then provide axis and chart titles with xlabel, ylabel, and title.

plot(myTimes1,temperatureDay1, myTimes2,temperatureDay2, myTimes3,temperatureDay3);
legend({'Day1','Day2','Day3'});
xlabel('Minutes')
ylabel('Temperature F');
title('Three-Day Temperature Comparison');

5) You can edit the template code to fit your application. For example,edit the code to change the number of days to read and the plot titles. Press Save and Run to generate the plot.

Since the plot is generated from real data, your plot looks similar but not identical to this plot.

Overlaying the data on the same plot shows three different behaviors in the temperature variation for the day.

6) Optionally, you can add saved visualizations to your channel. In Display Settings, use the plus sign next to Add/Edit this Visualization to a Channel to expand the channels list.

Click the check box for the channel you want to add the visualization to. To add private visualizations, select Private View. To share the URL and add the visualization to the Public View, click Create a public URL. To update your selections, click Save Display Settings.

See Also

Functions

Related Examples

More About