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.

Use Histogram to Understand Variation in Data

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 histogram plot. The histogram displays the number of times each particular temperature is recorded in the selected data.

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 histogram visualization of a set of data from a ThingSpeak channel, create a MATLAB script using the code template provided.

Navigate to the Apps tab, and select MATLAB Visualizations. Click New, choose Use a histogram to understand variation in data, and click Create.

Visualize Your Data

ThingSpeak populates the MATLAB Code field with the code to generate the temperature histogram.

  1. Set the variables for communicating with ThingSpeak. 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 600 minutes of temperature data.

    tempF = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,...
  3. Use histogram to generate the plot. Set the x-axis and y-axis labels and the chart title.

    xlabel('Temperature (F)');
    ylabel('Number of Measurements for Each Temperature');
    title('Histogram of Temperature Variation');
  4. You can edit the template code to fit your application. For example, you can edit the number of minutes to read and the plot titles in the code. Press Save and Run to generate the plot.

    Since the histogram is generated from real-time data, your histogram does not look identical to this plot.

  5. 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.

Compare Two Histograms

Histogram also allows you to compare distributions for different conditions. You can compare the present fluctuation to the fluctuation from the past day. The following code is not included in the template. To plot multiple distributions, add this code to the template.

  1. Read additional data from the previous day to compare to the original data set.

    tempFYesterday = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,...
  2. Turn hold on to keep the data on the same plot. Use histogram with 'FaceColor' red to differentiate the data. Add a legend as well.

    hold on
  3. Press Save and Run to generate the dual plot.

    The output visualization demonstrates the difference in the variation for the two time periods.

See Also


Related Examples

More About

Was this topic helpful?