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 scrape the latest live data from a website and display it. The example uses data from the MarineTraffic website, which contains information about the Port of Boston traffic.
To scrape web data about ships at the Port of Boston, 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 Read live web data for ships at the Port of Boston, and click Create.
The MATLAB Code field is prepopulated with code to read live web data and count the number of ships.
Specify the URL containing information about ships at the Port of Boston. Use
urlFilter to read the first four
numbers that appear after the target string. This function identifies the target
string in the source code of the page and returns the numbers that appear
immediately after it.
url = 'http://www.marinetraffic.com/en/ais/index/ports/all/port:131/portname:BOSTON'; filteredData = urlfilter(url,['"Ships currently in Port"','>'],4);
Display the results from scraping the website data. In this example, four elements are recorded and displayed.
display(filteredData(1),'Ships in Port now'); display(filteredData(2),'Departures'); display(filteredData(3),'Arrivals'); display(filteredData(4),'Expected Arrivals');
Execute your code by clicking Save and Run. The Output field displays your results.
Store your 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 boxes, and enter these channel setting values:
Ships at the Port of
Ships in port
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 ship data 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 value for each element from the website. 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.