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.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

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

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

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 = '';`

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);

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;

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);

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.

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

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

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

Calculate the weighted average.

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

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-');

Was this topic helpful?