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

1 次查看(过去 30 天)
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

采纳的回答

Geoff Hayes
Geoff Hayes 2020-4-6
编辑:Geoff Hayes 2020-4-6
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by