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 two types of data from an existing ThingSpeak™ channel and generate a line plot with two independent y-axes.
ThingSpeak channel 12397 contains weather data from a weather station on top of a parking garage on the MathWorks® campus in Natick, MA. Field 4 contains the temperature measurement, and field 2 contains the wind speed measurement.
To create a visualization of temperature and wind speed from a ThingSpeak channel, you can write a MATLAB script using the code template provided in the MATLAB Visualizations app.
Navigate to the Apps tab, and select MATLAB Visualizations. Click New, choose Plot temperature and wind speed of two different y-axes, and click Create.
ThingSpeak populates the MATLAB Code field with the code to generate the two axes plot.
Set the variables for communicating with ThingSpeak. In this example, the Read API Key is unnecessary because the weather station channel is public. If you are reading from your own channel, you can modify these values.
readChannelID = 12397; TemperatureFieldID = 4; windSpeedFieldID = 2; readAPIKey = '';
thingSpeakRead to retrieve 300 points of temperature and
wind speed data.
[data, timeStamps ] = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID windSpeedFieldID], 'NumPoints',300,'ReadKey',readAPIKey);
Extract the temperature and windspeed data
temperatureData = data(:, 1); windSpeedData = data(:, 2);
yyaxis left plot(timeStamps, temperatureData); ylabel('Temperature'); yyaxis right plot(timeStamps, windSpeedData); ylabel('Wind Speed');
You can edit the template code to fit your application. For example, you can edit the number of points to read and the plot titles in the code. Press Save and Run to generate the plot.
Since the plot is generated from real-time data, your plot looks similar, but not identical to this plot.
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.
The live nature of ThingSpeak data allows you to include code that determines the plot properties at run time. For example, set the color of the temperature line based on the average temperature. The following code is not included in the template. To conditionally change your plot, add this code to the template.
Set a threshold , define the cold line color, and determine the average value of the temperature.
hotThreshold=45; lineColor='c' averageTemperature=mean(temperatureData);
averageTemperature is greater than the
hotThreshold, change the line color character
if averageTemperature >hotThreshold lineColor='r' end
Now create the plot with the new color. Also add a title, x-axis title,
and change the color of the other line for differentiation. The
MATLAB Plot Output shows only one plot, so if you
add this code, comment the previous plot code by using the
%yyaxis left %plot(timeStamps, temperatureData); %ylabel('Temperature'); %yyaxis right %plot(timeStamps, windSpeedData); %ylabel('Wind Speed'); yyaxis left plot(timeTemp,temperatureData,linecolor); xlabel('Time') ylabel('Temperature'); yyaxis right plot(timeWindSpeed,windSpeedData,'k'); ylabel('Windspeed');
Press Save and Run to generate the dual plot.
The output visualization depends on the average temperature, and both lines have a different color from the plot above.