Main Content

Convert Data from Web Service

This example shows how to download data from a web service and use a function as a content reader with webread.

The National Geophysical Data Center (NGDC) provides various geophysical and space weather data via a web service. Among other data sets, the NGDC aggregates sunspot numbers published by the American Association of Variable Star Observers (AAVSO). Use webread to download sunspot numbers for every year since 1945.

api = '';
url = [api 'solar-data/solar-indices/sunspot-numbers/' ...
spots = webread(url);
  Name       Size              Bytes  Class    Attributes

  spots      1x1269             2538  char               

The NGDC web service returns the sunspot data as text. By default, webread returns the data as a character array.

ans =

   Year     SSN
    1945    32.3
    1946    99.9
    1947   170.9
    1948   166.6

webread can use a function to return the data as a different type. You can use readtable with webread to return the sunspot data as a table.

Create a weboptions object that specifies a function for readtable.

myreadtable = @(filename)readtable(filename,'HeaderLines',1, ...
options = weboptions('ContentReader',myreadtable);

For this data, call readtable with several Name,Value input arguments to convert the data. For example, Format indicates that each row has two numbers. Spaces are delimiters, and multiple consecutive spaces are treated as a single delimiter. To call readtable with these input arguments, wrap readtable and the arguments in a new function, myreadtable. Create a weboptions object with myreadtable as the content reader.

Download sunspot data and return the data as a table.

spots = webread(url,options);
  Name        Size            Bytes  Class    Attributes

  spots      76x2              2932  table              

Display the sunspot data by column and row.

ans = 

    Year     SSN 
    ____    _____

    1945     32.3
    1946     99.9
    1947    170.9
    1948    166.6

Plot sunspot numbers by year. Use table functions to select sunspot numbers up to the year 2013. Convert the Year and SSN columns to arrays and plot them.

rows = spots.Year < 2014;
vars = {'Year','SSN'};
spots = spots(rows,vars);
year = spots.Year;
numspots = spots.SSN;
title('Sunspot Data');
ylabel('Number of Sunspots');
xlim([1940 2015])
ylim([0 180])

Sunspot data plotted as the number of sunspots by year

Aggregated data and web service courtesy of the NGDC. Sunspot data courtesy of the AAVSO, originally published in AAVSO Sunspot Counts: 1943-2013, AAVSO Solar Section (R. Howe, Chair).