Why does webread() fail to find my host?

조회 수: 4 (최근 30일)
zach tyler
zach tyler 2020년 11월 14일
댓글: zach tyler 2020년 11월 28일
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

답변 (1개)

Rohit Pappu
Rohit Pappu 2020년 11월 24일
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
zach tyler
zach tyler 2020년 11월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by