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 multiple variables from an existing ThingSpeak™ channel and generate a scatter plot to examine the correlation in temperature and pressure.
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, field 3 contains the humidity measurement.
To create a scatter plot visualization of data from a ThingSpeak channel, you can write 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 Visualize correlation between temperature and humidity, and click Create.
ThingSpeak populates the MATLAB Code field with the code to generate the temperature histogram.
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; HumidityFieldID = 3; readAPIKey = '';
thingSpeakRead to retrieve 300
points from the temperature and humidity fields.
data = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID HumidityFieldID], ... 'NumPoints',300, 'ReadKey',readAPIKey);
Extract the temperature and wind speed data.
temperatureData = data(:, 1); humidityData = data(:, 2);
scatter(temperatureData,humidityData); xlabel('Temperature'); ylabel('Humidity');
You can edit the template code to fit your application. For example, you can edit the number of points to read and the plot titles in the code. Press Save and Run to generate the output.
Since the scatter plot is generated from live data, your plot looks similar, but not 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.
polyfit function can provide a fit to
linear data when the degree of fit is set to a value of 1. The following code is not
included in the template. To plot multiple distributions, add this code to the template.
polyfit to perform a fit to
the data. Specify
1 as the last argument for a linear
fitData=polyfit(temperatureData,humidityData,1); display(fitData(1),'Slope'); display(fitData(2),'Intercept');
Since there is not a definite relation between temperature and humidity, the linear fit is not necessarily a good fit. You can check the fit quality with a correlation coeficient. Calculate and display the correlation coefficient.
r=corrcoef(temperatureData,humidityData); display(r(2),'Correlation Coefficient');
Press Save and Run to generate the dual plot.
There is a negative slope in the scatter plot, and thus a negative correlation coefficient: humidity decreases with increasing temperature for this measurement. An r value of 1 or -1 indicates a perfect fit. The r value of -0.81 also indicates that there is a relatively poor fit, as expected.