{"id":10582,"date":"2019-03-08T09:00:24","date_gmt":"2019-03-08T14:00:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10582"},"modified":"2019-03-06T17:48:56","modified_gmt":"2019-03-06T22:48:56","slug":"world-bank-api-access","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/08\/world-bank-api-access\/","title":{"rendered":"World Bank API Access"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/70375\">World Bank API Access<\/a>  by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4155636\"> Kim9091<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>Last year, I <a href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/08\/10\/string-art\/\">blogged<\/a> about writing your own <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/customize-code-suggestions-and-completions.html\">function signatures<\/a> 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.\n   <\/p>\n<p>Kim9091&#8217;s World Bank API takes care of making the HTTP requests to get data from the World Bank.  But not only that, there&#8217;s also a live script that documents the capabilities and provides examples. Let&#8217;s give the example a whirl.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Make the API Connection<\/span>\r\napi = wb;\r\n\r\n<span style=\"color: #228B22\">% Fetch 2015 GDP Data<\/span>\r\ngdp2015 = query(api, <span style=\"color: #A020F0\">'source'<\/span>, <span style=\"color: #A020F0\">'2'<\/span>, <span style=\"color: #A020F0\">'series'<\/span>, <span style=\"color: #A020F0\">'NY.GDP.PCAP.CD'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'country'<\/span>, <span style=\"color: #A020F0\">'all'<\/span>, <span style=\"color: #A020F0\">'time'<\/span>, <span style=\"color: #A020F0\">'YR2015'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">gdp2015 = \r\n  struct with fields:\r\n\r\n           page: 1\r\n          pages: 1\r\n       per_page: 9999\r\n          total: 264\r\n    lastupdated: '2019-01-30'\r\n         source: [1&times;1 struct]\r\n<\/pre>\n<p>The data comes back as a series of nested structs and cells.  To make it easier to work with, we&#8217;ll convert the struct to a table.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gdp2015 = struct2table(gdp2015.source.data);<\/pre>\n<p>Then we&#8217;ll remove all of the empty values.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">empties = cellfun(@isempty, gdp2015.value);\r\ngdp2015 = gdp2015(~empties, :)<\/pre>\n<pre style=\"font-style:oblique\">gdp2015 =\r\n  248&times;2 table\r\n      variable         value    \r\n    ____________    ____________\r\n    [3&times;1 struct]    [6.4291e+03]\r\n    [3&times;1 struct]    [9.8352e+03]\r\n    [3&times;1 struct]    [1.2453e+04]\r\n    [3&times;1 struct]    [3.2813e+03]\r\n    ...\r\n<\/pre>\n<p>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.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gdp2015.value = vertcat(gdp2015.value{:})<\/pre>\n<pre style=\"font-style:oblique\">gdp2015 =\r\n  248&times;2 table\r\n      variable        value   \r\n    ____________    __________\r\n    [3&times;1 struct]        6429.1\r\n    [3&times;1 struct]        9835.2\r\n    [3&times;1 struct]         12453\r\n    [3&times;1 struct]        3281.3\r\n    ...\r\n<\/pre>\n<p>And finally a plot.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">histogram(gdp2015.value)\r\nxlabel(<span style=\"color: #A020F0\">'GDP ($US)'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'Count'<\/span>)<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainwbapi\/mainwbapi_01.png\"> <\/p>\n<h3>Comments<a name=\"6\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10582#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/70375#comments\">comment<\/a> for Kim9091.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_b66ac873fe034f92989c255ff01bc432() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='b66ac873fe034f92989c255ff01bc432 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b66ac873fe034f92989c255ff01bc432';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_b66ac873fe034f92989c255ff01bc432()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2019a<\/p>\n<\/div>\n<p><!--\nb66ac873fe034f92989c255ff01bc432 ##### SOURCE BEGIN #####\n%% World Bank API Access\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/70375 World Bank API Access > by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4155636 Kim9091>.\n% \n\n%% \n%\n% Last year, I <https:\/\/blogs.mathworks.com\/pick\/2018\/08\/10\/string-art\/\n% blogged> about writing your own\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/customize-code-suggestions-and-completions.html\n% function signatures> for code suggestions in MATLAB.  Today, I found my\n% first instance of someone using this in the wild!  And on a well\n% documented useful tool to boot.\n%\n% Kim9091's World Bank API takes care of making the HTTP requests to get\n% data from the World Bank.  But not only that, there's also a live script\n% that documents the capabilities and provides examples. Let's give the\n% example a whirl.\n\n% Make the API Connection\napi = wb;\n\n% Fetch 2015 GDP Data\ngdp2015 = query(api, 'source', '2', 'series', 'NY.GDP.PCAP.CD', ...\n    'country', 'all', 'time', 'YR2015')\n\n\n%%\n% The data comes back as a series of nested structs and cells.  To make it\n% easier to work with, we'll convert the struct to a table.\n\ngdp2015 = struct2table(gdp2015.source.data);\n\n%%\n% Then we'll remove all of the empty values.  \n\nempties = cellfun(@isempty, gdp2015.value);\ngdp2015 = gdp2015(~empties, :)\n\n%%\n% Since empty values were not stored as NaNs, the array was a cell.  We can\n% now convert it to double by vertically concatenating the value of each\n% cell.\n\ngdp2015.value = vertcat(gdp2015.value{:})\n\n%%\n% And finally a plot\n\nhistogram(gdp2015.value)\nxlabel('GDP ($US)')\nylabel('Count')\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10582#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/70375#comments\n% comment> for Kim9091.\n%\n \n\n##### SOURCE END ##### b66ac873fe034f92989c255ff01bc432\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainwbapi\/mainwbapi_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Sean&#8216;s pick this week is World Bank API Access  by  Kim9091.<\/p>\n<p>Last year, I blogged about writing your own function signatures for code suggestions in MATLAB.  Today, I found my&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/08\/world-bank-api-access\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10582"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=10582"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10582\/revisions"}],"predecessor-version":[{"id":10592,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10582\/revisions\/10592"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}