# ThingSpeak

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

## Calculate and Display Average 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 data from a channel, analyze the data, and display the result. The example uses data from ThingSpeak channel 12397, which collects weather data from an Arduino-based weather station in Natick, MA.

### Create a MATLAB Analysis Script from Template Code

To calculate the average of a set of data from a ThingSpeak channel, write a MATLAB® script using the code template provided.

Navigate to the Apps tab in ThingSpeak, and select MATLAB Analysis. Click , choose Calculate and display average humidity, and click .

### Analyze Your Data

The MATLAB Code field is prepopulated with code to calculate and display humidity.

1. Set the variables for communicating with ThingSpeak. The `readChannelID` is the channel ID for the public channel that collects data from the weather station. The `humidityFieldID` is the field in the channel that contains humidity values. Assign a value to `readAPIKey` only if you are reading data from your own private channel instead of the weather station channel.

```readChannelID = 12397; humidityFieldID = 3; readAPIKey = '';```
2. Read humidity values for the last 60 minutes using the `thingSpeakRead` function.

```humidity = thingSpeakRead(readChannelID,'Fields',humidityFieldID,'NumMinutes',60,'ReadKey',readAPIKey); ```
3. Calculate and show the average humidity from the last 60 minutes.

```avgHumidity = mean(humidity); display(avgHumidity,'Average Humidity');```
4. Execute your code by clicking . The Output field now displays your results.

### Write Data to a Channel

1. Store the calculated average humidity by writing it to a private channel. To create a ThingSpeak channel, go to the Channels tab, and select My Channels. Click . Select the corresponding check box, and enter these channel setting values:

• Name: `Average Humidity`

• Field 1: `Humidity`

Click .

2. In the MATLAB Code field, set the variables for writing to your private channel. Replace the given values for `writeChannelID` and `writeAPIKey` with your values. You can find the channel ID and API Key under the Channel Info panel on the right side of the page.

```% Replace the [] with channel ID to write data to: writeChannelID = []; % Enter the Write API Key between the '' below: writeAPIKey = '';```
3. Write the average humidity to your channel.

`thingSpeakWrite(writeChannelID,avgHumidity,'writekey',writeAPIKey);`
4. Execute your code by clicking . The chart in your ThingSpeak channel is populated with a single point representing average humidity. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.

5. Click again after some time to update your channel with another data point.

### Calculate Standard Deviation

You can write additional code in the template to perform further analysis of the humidity data. For example, standard deviation is often calculated along with mean. The standard deviation is a measure of the variation of a set of data. Calculate and display the standard deviation of humidity over the last 60 minutes. This section is not included in the code template. You can include it in your code after reading `humidity` from the weather station channel.

```stdHumidity = std(humidity); display(stdHumidity,'Standard Deviation of Humidity')```

To display the updated results with standard deviation in the Output field, click again.

## More About

Was this topic helpful?