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

## 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 , choose Visualize directional data with compass plot, and click .

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

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.

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