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.

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 New, choose Plot temperature and wind speed of two different y-axes, and click Create.

Visualize Your Data

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.

    [data, timeStamps ] = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID windSpeedFieldID], 'NumPoints',300,'ReadKey',readAPIKey);
  3. Extract the temperature and windspeed data

    temperatureData = data(:, 1);
    windSpeedData = data(:, 2);
  4. Use plot and yyaxis to generate the two axis plot. Set the y-axis labels for each side using ylabel.

    yyaxis left
    plot(timeStamps, temperatureData);
    yyaxis right
    plot(timeStamps, windSpeedData);
    ylabel('Wind Speed');
  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 plot.

    Since the plot is generated from real-time 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.

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.

  2. If averageTemperature is greater than the hotThreshold, change the line color character array.

    if averageTemperature >hotThreshold
  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 plot code by using the % symbol.

    %yyaxis left
    %plot(timeStamps, temperatureData);
    %yyaxis right
    %plot(timeStamps, windSpeedData);
    %ylabel('Wind Speed');
    yyaxis left
    yyaxis right
  4. Press Save and Run to generate the dual plot.

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

See Also


Related Examples

More About