Read csv data from multiple websites in for loop

4 visualizaciones (últimos 30 días)
Kurt
Kurt el 14 de Ag. de 2014
Respondida: Kurt el 15 de Ag. de 2014
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,

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 15 de Ag. de 2014
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!

Más respuestas (1)

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by