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 data from a public channel, modify the data, and display select elements. The example uses data from ThingSpeak channel 12397, which collects weather data from an Arduino® based weather station in Natick, MA.
To detect and remove outliers in wind speed data from the Natick weather station, you can write a MATLAB® script using the code template provided.
Navigate to the Apps tab in ThingSpeak, and select MATLAB Analysis. Click New, choose Remove outliers from wind speed data, and click Create.
The MATLAB Code field is prepopulated with code to detect and remove outliers from wind speed data over the past six hours.
Set the variables for communicating with ThingSpeak. The
readChannelID is the channel ID for the
public channel that collects data from the weather station. The
windSpeedFieldID is the field in the channel that
contains wind speed values. Assign a value to
if you are reading data from your own private channel instead of the weather
readChannelID = 12397; windSpeedFieldID = 2; readAPIKey = '';
Read wind speed values and timestamps from the past six hours using the
[windSpeed,timeStamp] = thingSpeakRead(readChannelID,'fields',windSpeedFieldID,'NumMinutes',360,'ReadKey',readAPIKey);
Check for outliers in the wind speed data using the MATLAB
isoutlier function. With the
default settings, this function calculates whether a value is more than three
scaled median absolute deviations away from the median of the input dataset. You
can adjust the input arguments to customize your outlier results. Identify the
indices of outlier data points and the indices of clean data points. Use these
indices to select data points and timestamps corresponding to outlier data and
outlierDataIndex = isoutlier(windSpeed); cleanDataIndex = ~outlierDataIndex; outlierData = windSpeed(outlierDataIndex); cleanData = windSpeed(cleanDataIndex); outlierTimeStamps = timeStamp(outlierDataIndex); cleanTimeStamps = timeStamp(cleanDataIndex);
Create timetables with outlier data and clean data, and display the result of the outlier data points.
outlierDataTable = timetable(outlierTimeStamps,outlierData); cleanDataTable = timetable(cleanTimeStamps,cleanData); display(outlierDataTable,'Outlier data');
Execute your code by clicking Save and Run. The Output field displays your results.
Store your clean data results by writing it to a private channel. To create a ThingSpeak channel, go to the Channels tab, and select My Channels. Click New Channel. Select the corresponding check box, and enter these channel setting values:
Cleaned Wind Speed
Click Save Channel.
In the MATLAB Code field, set the variables for writing to your
private channel. Replace the given values for
writeAPIKey with your values. You can find the channel ID
and API Key under the Channel Info panel on the right side
% Replace the  with channel ID to write data to: writeChannelID = ; % Enter the Write API Key between the '' below: writeAPIKey = '';
Write the clean wind speed readings with their respective timestamps to your channel.
Execute your code by clicking Save and Run. The chart in your ThingSpeak channel is populated with time series data for wind speed without the calculated outliers. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
To download your data in CSV format, click the Data Export button or the Data Import / Export tab. To clear all saved data in your channel, click the Channel Settings tab.
You can write additional code in the template to further analyze the wind speed data.
Besides removing outliers, another method of smoothing out a dataset is to calculate the
moving mean. In this approach, the mean of a group of local data points is calculated
over a sliding window throughout the entire dataset. Use the MATLAB
movmean function with a sliding window
of five minutes to smooth your wind speed data. This section is not included in the code
template. You can include it in your code after
smoothData = movmean(windSpeed,minutes(5),'SamplePoints',timeStamp);
To save your new data, write it to your ThingSpeak channel. Comment out the existing
thingSpeakWrite function and save the new smoothed times series
data to your channel.
To obtain the updated results, click Save and Run again.