File Exchange Pick of the Week

Our best user submissions

World Bank API Access

Sean‘s pick this week is World Bank API Access by Kim9091.

Last year, I blogged about writing your own function signatures for code suggestions in MATLAB. Today, I found my first instance of someone using this in the wild! And on a well documented useful tool to boot.

Kim9091’s World Bank API takes care of making the HTTP requests to get data from the World Bank. But not only that, there’s also a live script that documents the capabilities and provides examples. Let’s give the example a whirl.

% Make the API Connection
api = wb;

% Fetch 2015 GDP Data
gdp2015 = query(api, 'source', '2', 'series', 'NY.GDP.PCAP.CD', ...
    'country', 'all', 'time', 'YR2015')
gdp2015 = 
  struct with fields:

           page: 1
          pages: 1
       per_page: 9999
          total: 264
    lastupdated: '2019-01-30'
         source: [1×1 struct]

The data comes back as a series of nested structs and cells. To make it easier to work with, we’ll convert the struct to a table.

gdp2015 = struct2table(gdp2015.source.data);

Then we’ll remove all of the empty values.

empties = cellfun(@isempty, gdp2015.value);
gdp2015 = gdp2015(~empties, :)
gdp2015 =
  248×2 table
      variable         value    
    ____________    ____________
    [3×1 struct]    [6.4291e+03]
    [3×1 struct]    [9.8352e+03]
    [3×1 struct]    [1.2453e+04]
    [3×1 struct]    [3.2813e+03]
    ...

Since empty values were not stored as NaNs, the array was a cell. We can now convert it to double by vertically concatenating the value of each cell.

gdp2015.value = vertcat(gdp2015.value{:})
gdp2015 =
  248×2 table
      variable        value   
    ____________    __________
    [3×1 struct]        6429.1
    [3×1 struct]        9835.2
    [3×1 struct]         12453
    [3×1 struct]        3281.3
    ...

And finally a plot.

histogram(gdp2015.value)
xlabel('GDP ($US)')
ylabel('Count')

Comments

Give it a try and let us know what you think here or leave a comment for Kim9091.

Published with MATLAB® R2019a

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.