主要内容

从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 struct array with fields: year data . S = 112x1 struct array with fields: year data

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

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

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

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

绘制一条与温度的最小二乘拟合曲线。

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

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