This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Read Live Web Data for Vessels at the Port of Boston

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.

Create a MATLAB Analysis Script from Template Code

To scrape web data about vessels 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 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 containing information about 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 = '';
    filteredData = urlfilter(url,'Vessels in Port:',2);
  2. Display the results from scraping the website data. In this example, two elements are recorded and displayed.

    display(filteredData(1),'Vessels in Port');
    display(filteredData(2),'Expected Arrivals');
  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 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:

    • 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 under the Channel Info panel on the right side of the page.

    % Replace the [] with channel ID to write data to:
    writeChannelID = [];
    % Enter the Write API Key between the '' below:
    writeAPIKey = '';

  3. Write the vessel data to your channel.

  4. 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.

  5. Click Save and Run again after some time to update your channel with another data point.

See Also


Related Examples

More About