Why does webread() fail to find my host?

2 views (last 30 days)
zach tyler
zach tyler on 14 Nov 2020
Commented: zach tyler on 28 Nov 2020
I'm writing a script to scrape .csv file data from a webserver hosted on an ESP8266. The webserver works -- I can access the .csv data stored there through my web browser. When I try to scrape that data into MATLAB using the code below, webread() fails. Interestingly I can comment out one of the calls to webread() and the other will work. What gives?
The data is the UNIX timestamp and a temperature or distance measurement separated by a comma and delimited by newlines, if that's relevant. MATLAB reads it as a 1 x n char.
% to prevent matlab from erroring out during the webread, set up a while
% loop:
i = 1;
e = 0;
I = 100;
while i < I
try
% specify the url of the .csv:
urlT = 'http://esp8266.local/temp.csv';
% read the contents of the url with matlab's webread:
T = webread(urlT);
% specify a new url and read it into a char array:
urlD = 'http://esp8266.local/dist.csv';
D = webread(urlD);
i = i + 1;
e = 0;
catch
fprintf("webread was unsuccessful; waiting to try again.\n");
pause(0.5);
e = e + 1;
if e > 50
keyboard
end
end
end

Answers (1)

Rohit Pappu
Rohit Pappu on 24 Nov 2020
webread() might not be able to process the CSV file, directed by the URL. A possible workaround could be
D = webread(url, 'table'); %% Explicitly mention that the url points to a CSV file
%% Alternatively, download the csv file into a filename
filename = 'dummy.csv';
D = websave(filename,url);
Additional documentation about websave()
  1 Comment
zach tyler
zach tyler on 28 Nov 2020
Really interesting -- this is exactly the sort of functionality I was hoping for, but couldn't find initially (I'm a newbie). Thanks, I will give this a try.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!