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, perform a calculation, and display the data. The example uses data from ThingSpeak channel 12397, which collects weather data from an Arduino-based weather station in Natick, MA.
To convert temperature in Fahrenheit to temperature in Celsius from the Natick weather station, write a MATLAB® script using the code template provided.
Navigate to the Apps tab in ThingSpeak, and select MATLAB Analysis. Click New, choose Convert temperature units, and click Create.
The MATLAB Code field is prepopulated with code to convert temperature from Fahrenheit to Celsius.
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
temperatureFieldID is the field in the channel that
contains temperature values. Assign a value to
only if you are reading data from your own private channel instead of the
weather station channel.
readChannelID = 12397; temperatureFieldID = 4; readAPIKey = '';
Read the most recent temperature value using the
tempF = thingSpeakRead(readChannelID,'Fields',temperatureFieldID,'ReadKey',readAPIKey);
Calculate the temperature in Celsius and display the result.
tempC = (5/9)*(tempF-32); display(tempC,'Temperature in Celsius');
Execute your code by clicking Save and Run. The Output field now displays your results.
Store your temperature conversion 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:
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 Fahrenheit and Celsius temperature readings to your channel.
Execute your code by clicking Save and Run. The charts in your ThingSpeak channel are populated with a single point in each, representing the latest temperature reading. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
Click Save and Run again after some time to update your channel with another data point.