从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
画出每年的平均气温。将温度和年份转换为数字数组。将年份转换为日期时间对象以方便绘图,并将温度转换为华氏度。
temps = [S.data];温度= 9/5 *温度+ 32;years = [S.year];Yearstoplot = datetime(years,1,1);图形图(年stoplot,温度);标题(“1901-2012年美国平均气温”)包含(“年”) ylabel (温度(^{\保监会}F)”) xmin = datetime(1899,1,1);Xmax = datetime(2014,1,1);xlim ([xmin xmax])
将一条线的最小二乘拟合与温度重叠。
P = polyfit(years,temps,1);Ptemps = polyval(p,年);delta = 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的更多资料,以及世界银行:使用条款)。