Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Compare Temperature Data from Three Different Days

The MATLAB® Analysis and MATLAB Visualizations apps in ThingSpeak™ let you pick from a selection of code templates as a starting point for building your own project. This example demonstrates how to read data from an existing ThingSpeak™ channel and generate a plot with three series on the same axis.

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 wite a MATLAB script using the code template provided in the MATLAB Visualizations app.

Navigate to the Apps tab, and select 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 a matrix of y-values for plotting. 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. Fix the domain to 1400 points for each series.

    allData=[temperatureDay1(1:1400),temperatureDay2(1:1400),temperatureDay3(1:1400)];
  4. Use thingSpeakPlot to generate the plot. Set the legend to differentiate the series, and then provide axis and chart titles.

     thingSpeakPlot((1:1400),allData,'legend',{'Day1','Day2','Day3'},'XLabel',...
     'Minutes','YLabel','Temperature F','Title','3 Day Temperature Comparison');
  5. You can edit the template code to fit your application. For example, you can edit the number of days to read and the plot titles in the code. 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 next to Add/Edit this Visualization to a Channel to expand the channels list.

    Click the check box that corresponds to the channel you want to add the visualization to. To add private visualizations, check 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

Was this topic helpful?