importing text from URL
조회 수: 7 (최근 30일)
이전 댓글 표시
I would like to retrieve ASCII/text data from a web service URL and import into Matlab as either a Table or Structure.
The basic format of the text is as below:
# comment_header1_line1
# comment_header1_line2
...
# comment_header1_lineN
time1, data1
time2, data2
...
timeX, dataX
# comment_header2_line1
# comment_header2_line2
...
# comment_header2_lineM
time1, data1
time2, data2
...
timeY, dataY
So there could be dozens of different sections of this text, each corresponding with its own separate "comment_headerZ_line" section. I don't exactly have a physical .txt file to work with since this info is obtained from a URL, so I've been trying to use the 'webread' function with the weboptions object to make this work (below).
webopts = weboptions('ContentReader',@readtable);
mydata = webread('theURL',webopts);
Unfortunately, the readtable function seems to break when it encounters the end of one data chunk, and the start of the next comment_header section:
Error using readContentFromFile (line 28)
The content reader issued the error message: "Reading failed at line 620. All lines of a text file must have the same
number of delimiters. Line 620 has 1 delimiters, while preceding lines have 2.
Do I need to try this with textscan instead? Or create a custom "@read_webtext" function for the handle of ContentReader? If possible, I would very much like to avoid having to download the physical file onto my workspace.
댓글 수: 1
Star Strider
2022년 1월 26일
That file may be difficult to read. It might be easier to import it as a single block of text using:
webopts = weboptions('ContentType','text');
and then read it with textscan, probably saving it as a .txt file first. There are ways to deal with those sorts of files, and they require a while loop to iterate through the file and read each section. This is not necessarily difficult, however it can be an interesting challenge.
답변 (0개)
참고 항목
카테고리
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!