Read csv data from multiple websites in for loop

조회 수: 4 (최근 30일)
Kurt
Kurt 2014년 8월 14일
답변: Kurt 2014년 8월 15일
I'm trying to read csv data from a website using the urlread command. I can do this successfully if I just want to read in data from one station. However, I would like to be able to do this for multiple stations simultaneously. Because the only thing that changes in the url as I go from one station to the next is the 3-character id (shown below), I figure this could be done somehow in a for loop.
Here is what I have so far:
url_start = {'text_a'};
url_end = {'text_b'};
id={'abc';'bcd';'cde';'def';'efg';'fgh'};
url=cell(6,1);
for i = 1:6
url{i} = strcat(url_start,id{i},url_end);
end
Does anyone know how I can now use the urlread on the newly created 'url' so I can read data from multiple webpages simultaneously? Thanks,

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 15일
Kurt - what do you mean by read data from multiple webpages simultaneously? Do you mean that as you create one URL (say url{1}) you want to read in the csv data for that URL, and then once url{2} is created you read in the csv data for that URL, etc.? If so, then try the following
urlData = cell(6,1);
url_start = {'text_a'};
url_end = {'text_b'};
id={'abc';'bcd';'cde';'def';'efg';'fgh'};
url=cell(6,1);
for k = 1:6
url{k} = char(strcat(url_start,id{k},url_end));
urlData{k} = urlread(url{k});
end
On each iteration of k, the URL is built (with valid start, middle, and stop strings) and we save the character equivalent of it to the url cell array. We then immediately call urlread on that URL and save the results to the urlData array.
Try the above and see what happens!

추가 답변 (1개)

Kurt
Kurt 2014년 8월 15일
That's exactly what I needed! Thank you. I think I had something similar at one point, but was missing the char call.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by