怎么在matlab中从Web服务下载数据?

2022年7月25日 25点热度 0人点赞

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

工具/原料

  • matlab软件
  • 电脑

方法/步骤

  1. 1

    使用webread将美国年平均气温读入一个结构数组。

    api = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/'; 

    url = [api 'country/cru/tas/year/USA']; 

    S = webread(url)

  2. 2

    S(1)

  3. 3

    S(112)

  4. 4

    绘制出每年的平均气温。将温度和年份转换为数字数组。将年份转换为日期时间对象以便于绘制,并将温度转换为华氏度。

  5. 5

    temps = [S.data]; 

    temps = 9/5 * temps + 32; 

    years = [S.year]; 

    yearstoplot = datetime(years,1,1); 

    figure
    plot(yearstoplot, temps); 

    title('USA Average Temperature 1901-2012') 

    xlabel('Year') 

    ylabel('Temperature (^{\circ}F)') 

    xmin = datetime(1899,1,1); 

    xmax = datetime(2014,1,1); 

    xlim([xmin xmax])

  6. 6

    一条直线与温度的最小二乘拟合过度。

    p = polyfit(years,temps,1); 

    ptemps = polyval(p,years); 

    deltat = p(1); 

    hold on

    fl = plot(yearstoplot, ptemps); 

    xlim([xmin xmax]) 

    title('USA Average Temperature Trend 1901-2012') 

    xlabel('Year') 

    ylabel('Temperature (^{\circ}F)') 

    deltat = num2str(10.0*deltat); 

    legend(fl,['Least Squares Fit, ', deltat, '^{\circ}F/decade']) 

    hold off

    END
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。

展开阅读全部

laozhao

这个人很懒,什么都没留下

文章评论