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

Read Web Data

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

  1. 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 = '';
    filteredData = urlfilter(url,['"Ships currently in Port"','>'],4);
  2. 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(4),'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: Ships at the Port of Boston

    • Field 1: Ships in port now

    • Field 2: Departures

    • Field 3: Arrivals

    • Field 4: 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 ship 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

Was this topic helpful?