从Web服务下载数据

此示例显示如何使用。从web服务下载数据webread函数。世界银行通过世界银行气候数据API提供各种气候数据。对这个API的调用将返回JSON格式的数据。webread将JSON对象转换为便于在MATLAB中进行分析的结构®

使用webread将美国的年平均温度读取到一个结构数组中。

api =“http://climatedataapi.worldbank.org/climateweb/rest/v1/”;url = [api“国家/ cru /助教/年/美国];S = webread (url)
S = 112x1结构数组与字段:年数据

webread将数据转换为包含112个元素的结构数组。每个结构都包含给定年份的温度,从1901年到2012年。

(1)
ans =年份:1901数据:6.6187
年代(112)
ans =年份:2012数据:7.9395

画出每年的平均温度。将温度和年份转换为数字数组。将年份转换为datetime对象以便于绘图,并将温度转换为华氏温度。

临时工= [S.data];temps = 9/5 * temps + 32;年= [S.year];yearstoplot = datetime(年,1,1);图绘制(yearstoplot、临时工);标题(“美国平均气温1901-2012”)包含(“年”)ylabel (温度(^{\保监会}F)”) xmin =日期时间(1899,1,1);xmax = datetime (2014、1、1);xlim ([xmin xmax])

将一条线的最小二乘拟合覆盖到温度上。

p = polyfit(年,临时工,1);ptemps = polyval (p,年);deltat = p (1);持有fl = plot(yearstoplot, ptemps);xlim ([xmin xmax])标题(“美国平均气温趋势1901-2012”)包含(“年”)ylabel (温度(^{\保监会}F)”) deltat = num2str(10.0*deltat);传奇(fl,'最小二乘拟合'deltat,“^{\保监会}F /十年”)举行

世界银行提供的API和数据:气候数据API。(见世界银行:气候数据API有关API的更多信息,以及世界银行:使用条款)。