This example shows 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. 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.
To create a histogram visualization of a set of data from a ThingSpeak channel, you can create a MATLAB script using a code template.
Go to the Apps tab and select MATLAB Visualizations. Click New, select Use a histogram to understand variation in data, and click Create.
ThingSpeak populates the MATLAB Code field with the code to generate the temperature histogram.
1) Specify 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 = '';
thingSpeakRead to retrieve 1200 minutes of temperature data.
tempF = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,... 'NumMinutes',20*60,'ReadKey',readAPIKey);
histogram to generate the plot. Set the x-axis and y-axis labels and the chart title.
histogram(tempF); 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, edit the code to change the number of minutes to read and the plot titles. 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 sign next to Add/Edit this Visualization to a Channel to expand the channels list.
Select 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.
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,... 'DateRange',[datetime('today')-days(2),datetime('today')-days(1)],'ReadKey',readAPIKey);
hold on to keep the data on the same plot. Use
histogram and set
'FaceColor'to red to differentiate the data. Add a legend as well.
% Plot the original Data histogram(tempF) xlabel('Temperature (F)'); ylabel('Number of Measurements for Each Temperature'); title('Histogram of Temperature Variation'); hold on histogram(tempFYesterday,'FaceColor','r'); legend('Today','Yesterday');
3) Press Save and Run to generate the dual plot.
The output visualization demonstrates the difference in the variation for the two time periods.