Why does webread() fail to find my host?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!