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.
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.
ThingSpeak populates the MATLAB Code field with the code to generate the temperature histogram.
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 = '';
thingSpeakRead to retrieve 600 minutes of temperature
tempF = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,... 'NumMinutes',10*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');
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.
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.
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.
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
'FaceColor' red to
differentiate the data. Add a legend as well.
hold on histogram(tempFYesterday,'FaceColor','r'); legend('Today','Yesterday');
Press Save and Run to generate the dual plot.
The output visualization demonstrates the difference in the variation for the two time periods.