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.

Visualize Correlation Between Temperature and Humidity

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.

Create a MATLAB Visualization from Template Code

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.

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. 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 = '';
  2. Use thingSpeakRead to retrieve 300 points from the temperature and humidity fields.

    data = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID HumidityFieldID], ...
          'NumPoints',300, 'ReadKey',readAPIKey); 
  3. Extract the temperature and wind speed data.

    temperatureData = data(:, 1);
    humidityData = data(:, 2);
  4. Use scatter to generate the plot. Set the x-axis and y-axis labels using xlabel and ylabel.

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

  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.

Determine Linear Fit

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

  1. Use polyfit to perform a fit to the data. Specify 1 as the last argument for a linear fit.

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

    display(r(2),'Correlation Coefficient');
  3. 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.

See Also


Related Examples

More About