- 积分
- 1931
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-4-6
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 haobang008 于 2014-7-13 22:16 编辑
最近自己写了一个程序,可以从API接口中提取实时的气象数据,并且写入到txt和xls当中,粘贴如下,
另外,需要一个自编函数,下载地址如下:
http://www.mathworks.com/matlabc ... nother--json-parser
程序的主要功能就是根据提供的API接口,从API上下载数据,每4分钟更新一次数据。
% 研究区域气象数据提取
% data.weatherinfo.city "北京" 城市
% data.weatherinfo.cityid "101010100" 城市编码
% data.weatherinfo.temp "22" 当前温度
% data.weatherinfo.WD "东南风" 风向
% data.weatherinfo.WS "3级" 风速
% data.weatherinfo.SD "31%" 相对湿度
% data.weatherinfo.WSE "3" 风力
% data.weatherinfo.time "20:15" 更新时间
% data.weatherinfo.isRadar "1" 是否有雷达图 (1表示有雷达图)
% data.weatherinfo.Radar "JC_RADAR_AZ9010_JB" 雷达图地址(AZ9010为北京雷达)
% 循环
% 打开文件
% 读取信息,写入
% 暂停
% 重新开始循环
clc;clear all;
while 1+1 > 1
pause(4*60);
c1 = clock;
% Get weather information from the Internet
url = ['http://www.weather.com.cn/data/sk/101' '120201' '.html'];
response = urlread(url);
data = parse_json(response);
str = [];
% 修改湿度的设置:
if data.weatherinfo.SD(2) == '%'
daqishidu = data.weatherinfo.SD(1);
else
daqishidu = data.weatherinfo.SD(1:2);
end
str = [ data.weatherinfo.city ' ' data.weatherinfo.cityid ' ' data.weatherinfo.time ...
' ' data.weatherinfo.temp ' ' data.weatherinfo.WD ' ' data.weatherinfo.WS ...
' ' daqishidu ' ' data.weatherinfo.WSE ' ' data.weatherinfo.isRadar ...
' ' data.weatherinfo.Radar ];
% Format: date month day hour minute
format2 = ['%d ' '%d ' '%d ' '%d ' '%d'];
fprintf(fid_txt,format2,c1(1:5));
% open temp data file txt&xls:
file_weather = [ int2str(c1(1)) '_' int2str(c1(2)) '_' int2str(c1(3)) '.txt'];
cunzai = exist(file_weather,'file');
fid_w = fopen(file_weather,'a+');
% fid_txt = fopen('test1.txt','w'); %打开后写入数据。该文件已存在则更新;不存在则创建
% save data in the file of txt
if(cunzai == 2)
fprintf(fid_w,'\r\n%s',str);
else
fprintf(fid_w,'%s',str);
end
% save data in the file of xls
file_weather_xls = [ int2str(c1(1)) '_' int2str(c1(2)) '_' int2str(c1(3)) '.xls'];
data_xls = { data.weatherinfo.city,data.weatherinfo.cityid,data.weatherinfo.time, ...
data.weatherinfo.temp,data.weatherinfo.WD,data.weatherinfo.WS, ...
data.weatherinfo.SD(1:2),data.weatherinfo.WSE,data.weatherinfo.isRadar, ...
data.weatherinfo.Radar };
cunzai = exist(file_weather_xls,'file');
if(cunzai == 2)
datan = xlsread(file_weather_xls);
dn = size(datan);
n = dn(1);
xlswrite(file_weather_xls,data_xls, 'Sheet1', ['A' int2str(n+1)]);
else
xlswrite(file_weather_xls,data_xls, 'Sheet1', 'A1');
end
% [ndata, text, alldata] = xlsread(file_weather_xls);
% xlswrite(file_weather_xls,magic(3), 'Sheet1')
% close file
fclose(fid_txt);
fclose(fid_w);
% break;
end
|
评分
-
查看全部评分
|