ThingSpeak

This is machine translation

Translated by Microsoft
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.

Visualize Directional Data with Compass Plot

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 matrix calculations in MATLAB and the use of a compass plot in a MATLAB Visualizations App to show directional distribution.

ThingSpeak™ channel 12397 contains weather data from a weather station on top of a parking garage on the MathWorks® campus in Natick, MA. Field 1 contains the wind speed measurement.

Create a MATLAB Visualization from Template Code

To create a compass plot visualization of a set of data from a ThingSpeak channel, create a MATLAB script using the code template provided.

Navigate to the Apps tab, and select MATLAB Visualizations. Click New, choose Visualize directional data with compass plot, and click Create.

Visualize Your Data

ThingSpeak populates the MATLAB Code field with template code to read the data and then generate the area plot.

  1. Set the variables for communicating with ThingSpeak. The weather station channel is public, so you do not need to add the Read API Key. If you are reading from your own channel, you can modify these values.

    readChannelID = 12397;
    WindDirFieldID = 1;
    WindSpeedFieldID = 2;
    readAPIKey = '';
  2. Use thingSpeakRead to read 60 points of wind direction data from field 1. Read the wind speed data from field 2.

    windDir = thingSpeakRead(readChannelID,'Fields',WindDirFieldID,'NumPoints',60,...
    'ReadKey',readAPIKey);
    
    windSpeed = thingSpeakRead(readChannelID,'Fields',WindSpeedFieldID,'NumPoints',60,...
    'ReadKey',readAPIKey);
  3. Convert the wind direction from degrees to radians, and shift the direction to align with north. These equations operate on each element in the array read from ThingSpeak.

    rad = windDir*2*pi/360;
    rad = rad+pi/2;
  4. Convert the wind measurements to Cartesian coordinates to be consistent with the compass plot inputs. You can edit the template code to fit your application. For example, you can edit the number of points to read with thingSpeakRead.

    u = cos(rad) .* windSpeed; 
    v = sin(rad) .* windSpeed; 
    compass(u,v);
  5. Press Save and Run to generate the plot.

    The compass plot is generated from real-time data so your visualization is different than this plot.

  6. The plot here shows a distinct grouping of southeast wind measurements.

  7. Optionally, you can add saved visualizations to your channel. In the section below your code labeled Add this Visualization to a Channel, click the check box that corresponds to the channel you want to see the visualization on. You can add private visualizations to the Private View. You can share the URL and add the visualization to the Public View after you click Create a public URL.

Show Weighted Average of Wind Measurements

You can compare multiple outputs on the same plot with MATLAB Visualizations. For example, you can add an arrow indicating the weighted average direction.

  1. Calculate the weighted average.

    aveDir = sum(rad.*windSpeed)/sum(windSpeed);

  2. Convert to Cartesian coordinates. Turn hold on to keep the data on the same plot, and then generate the plot. Use the line type 'ro-' to distinguish the average line from the others.

    w = mean(windSpeed)*cos(aveDir); 
    x = mean(windSpeed)*sin(aveDir);
    hold on
    compass(w,x,'ro-');

See Also

Functions

Related Examples

More About

Was this topic helpful?