How to use webread with a variable (from a table) in the url

2 views (last 30 days)
Hello,
I'm trying to create a map of temperature evolution over time in the world.
So i need to get api data from each country and i would like to have this kind of url: http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999/FRA
clear
data = readtable('countries_codes_and_coordinates.csv');
code = data(76,3); % code = 'FRA'
url = sprintf( 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999/', code ) ;
FRANCE = webread( url ) ;
But when I run the code I get an error..
Error in readContentFromWebService (line 46)
byteArray = copyContentToByteArray(connection);
Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
Error in Projet (line 8)
FRANCE = webread( url ) ;
Do you know why my code is false? I also want to remove '...' from 'FRA' because it's probably also an error.
Thanks in advance

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Apr 2020
Edited: Geoff Hayes on 6 Apr 2020
Benjamin - you need to specify a format when using sprintf so that the code is included in the string. For example,
>> code = 'FRA'; % code = 'FRA'
>> url = sprintf( 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999/', code);
>> url
url =
http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999
and note how url does not have the FRA country included..because we didn't "tell" sprintf about it. Since code is a string, we need to include the %s to indicate where it should "fit" in the URL:
>> code = 'FRA'; % code = 'FRA'
>> url = sprintf( 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999/%s', code);
>> url
url =
http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/tas/1980/1999/FRA

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!