Main Content

Read Live Web Data for Vessels at the Port of Boston

This example demonstrates how to scrape the latest live data from a website and display it. In the example, you modify one of the code templates provided by the MATLAB Analysis and MATLAB Visualizations apps. The example uses data from the MarineTraffic website, which provides information on traffic at the Port of Boston.

Create a MATLAB Analysis Script from Template Code

To scrape web data on vessels at the Port of Boston, you can write a MATLAB® script using a code template.

Go to the Apps tab in ThingSpeak and select MATLAB Analysis. Click New, select Read live web data for vessels at the Port of Boston, and click Create.

Read Web Data

The MATLAB Code field is prepopulated with code to read live web data and count the number of vessels.

1) Specify the URL for accessing information on vessels at the Port of Boston. Use urlFilter to read the first two 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 = 'https://www.marinetraffic.com/en/ais/details/ports/131/USA_port:BOSTON';
filteredData = urlfilter(url,'Vessels in Port:',2);

2) Display the results of scraping the website data. In this example, two elements are recorded and displayed.

display(filteredData(1),'Vessels in Port');
    89
display(filteredData(2),'Expected Arrivals');
     8

3) Execute your code by clicking Save and Run. The Output field displays your results.

Write Data to a Channel

1) Store your results by writing them 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:

  • Name — Vessels at the Port of Boston

  • Field 1 — Vessels in Port

  • Field 2 — Expected Arrivals

Click Save Channel.

2) In the MATLAB Code field, set the variables for writing to your private channel. Replace the given values for writeChannelID and writeAPIKey with your values. You can find the channel ID and API key in the Channel Info panel on the right side of the page.

% Replace the [] with ID of the channel to write data to:
writeChannelID = 17504;
% Enter the write API key between the ''.
writeAPIKey = '23ZLGOBBU9TWHG2H';

3) Write the vessel data to your channel.

thingSpeakWrite(writeChannelID,filteredData,'Writekey',writeAPIKey);

4) Execute your code by clicking Save and Run. Each of the charts in your ThingSpeak channel is populated with a single point representing an field in the website data. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.

5) Click Save and Run again after a few minutes to update your channel with another data point.

See Also

Functions

Related Examples

More About