Documentation

This is machine translation

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

Plot Temperature and Wind Speed on Two Different Axes

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 two types of data from an existing ThingSpeak™ channel and generate a line plot with two independent y-axes.

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, and field 2 contains the wind speed measurement.

Create a MATLAB Visualization from Template Code

To create a visualization of temperature and wind speed 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 , choose Plot temperature and wind speed of two different y-axes, and click .

ThingSpeak populates the MATLAB Code field with the code to generate the two axes plot.

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; windSpeedFieldID = 2; readAPIKey = ''; ```
2. Use `thingSpeakRead` to retrieve 300 points of temperature and wind speed data.

```[temperatureData, timeTemp] = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, 'NumPoints',300,'ReadKey',readAPIKey); [windSpeedData,timeWindSpeed] = thingSpeakRead(readChannelID,'Fields',windSpeedFieldID,'NumPoints',300,'ReadKey',readAPIKey);```
3. Use `thingSpeakPlotYY` to generate the two axis plot. Set the y-axis labels for each side.

```thingSpeakPlotYY(timeTemp,temperatureData,timeWindSpeed,windSpeedData,'Ylabel1','Temperature', 'Ylabel2','Wind Speed'); ```
4. 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 to generate the plot.

Since the plot is generated from real-time data, your plot looks similar, but not identical to this plot.

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

Create Conditionally Colored Plot

The live nature of ThingSpeak data allows you to include code that determines the plot properties at run time. For example, set the color of the temperature line based on the average temperature. The following code is not included in the template. To conditionally change your plot, add this code to the template.

1. Set a threshold , define the cold line color, and determine the average value of the temperature.

```hotThreshold=45; lineColor='cyan' averageTemperature=mean(temperatureData);```
2. If `averageTemperature` is greater than the `hotThreshold`, change the line color character array.

```if averageTemperature >hotThreshold lineColor='red' end```
3. Now create the plot with the new color. Also add a title, x-axis title, and change the color of the other line for differentiation. The MATLAB Plot Output shows only one plot, so if you add this code, comment the previous thingSpeakPlotYY code by using the `%` symbol.

```%thingSpeakPlotYY(timeTemp,temperatureData,timeWindSpeed,windSpeedData,'Ylabel1','Temperature', 'Ylabel2','Wind Speed'); thingSpeakPlotYY(timeTemp,temperatureData,timeWindSpeed,windSpeedData,'Ylabel1','Temperature','Ylabel2','windspeed','Xlabel','Time','Color1',lineColor,'Color2','k'); ```
4. Press to generate the dual plot.

The output visualization depends on the average temperature, and both lines have a different color from the plot above.