How do I modify this small code to be able to extract data to a matrix and not a buffer?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Laurentiu Galan
el 24 de Feb. de 2012
Editada: Azzi Abdelmalek
el 23 de Oct. de 2013
url_string='http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=10&b=15&c=2005&d=01&e=17&f=2006&g=d&ignore=.csv'
buffer = java.io.BufferedReader(...
java.io.InputStreamReader(...
openStream(...
java.net.URL(url_string))));
% Begin with 2nd line (exclude header)
ptr = 1;
while 1
% Read line
buff_line = char(readLine(buffer));
% Break if this is the end
if length(buff_line)<3, break; end
% Find comma delimiter locations
commas = find(buff_line== ',');
% Extract high, low, open, close, etc. from string
adj_close = str2num( buff_line(commas(6)+1:end) )
ptr = ptr + 1;
end
%end
How do I change the code to
1.) Be able read in every line starting with the second line and transform this information into a matrix with only adj_close?
2.) This part should be easy: The format comes in a format where the most recent date is at the first line, most recent-1 is the next line and so on... How do I resort this matrix where adj_close is resorted? Basically the 1st value becomes the last and the last becomes the first (so on and so forth) Does flipud do this?
Thanks for all of your help,
-LG
0 comentarios
Respuesta aceptada
Geoff
el 24 de Feb. de 2012
Part 1: Slightly inefficient, but not really a problem. You can just split each line using strread and grow the adj_close matrix each iteration... Be aware that if the number of elements in each row changes, you will get an error. In that case, you would need to force the number of items in each row vector to some value, probably by counting the number of entries in the header. That's an exercise for you =)
% Begin with 2nd line (exclude header)
readLine(buffer);
adj_close = [];
while 1
buff_line = char(readLine(buffer));
if length(buff_line)<3, break; end
vals = strread(buff_line,'%f','delimiter',',');
adj_close = [adj_close; vals'];
end
Part 2: Yes, you can use flipud to reverse the matrix.
adj_close = flipud(adj_close);
Más respuestas (0)
Ver también
Categorías
Más información sobre Money.Net en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!