File: ef230-2023-08/www/examples/temperature_from_web.m Download
%% pull current temperature from a web site
% This is a crude example of extracting data from a web service
% It is very page dependent.
% Spring, 2011

clear all; clc;
% get text (XML) from web site
txt = urlread('http://www.google.com/ig/api?weather=37931')

% set starting and ending strings
startstring = 'temp_f data="';
endstring = '"/>';

% look for first start string
istart=strfind(txt,startstring);
n = length(istart);
if n==0, error('did not find start string'); end
istart=istart(1)+length(startstring);

% look for first end string after this
iend = strfind(txt(istart:end),endstring) + istart - 2;
n=length(iend);
if n==0, error('did not find end string'); end
iend = iend(1);

% extract the value
valtxt = txt(istart:iend);
valnum = str2num(valtxt)