Borrar filtros
Borrar filtros

How do I modify this small code to be able to extract data to a matrix and not a buffer?

1 visualización (últimos 30 días)
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

Respuesta aceptada

Geoff
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);
  1 comentario
Laurentiu Galan
Laurentiu Galan el 24 de Feb. de 2012
Thanks a lot Geoff. What do you mean that it is inefficient? Would you recommend another way of doing this?
I am eventually planning on making the URL_string a list of strings (basically more than 3000 tickers) and I am hoping to create some sort of loop that will continually pull the adj close for a predefined date and export it to a matrix.
You wouldn't happen to know how to loop the whole process?
I.E. say if I have a variable "URL_String" which is an array of URLs and have the process run so I buffer the second line and just pull the adj close for that date?
In other words, say I have:
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'
url_string='http://ichart.finance.yahoo.com/table.csv?s=BAML&a=10&b=15&c=2005&d=01&e=17&f=2006&g=d&ignore=.csv'
url_string='http://ichart.finance.yahoo.com/table.csv?s=GS&a=10&b=15&c=2005&d=01&e=17&f=2006&g=d&ignore=.csv'
Would it be possible to create a loop that opens each one of these paths and simply pulls the adjusted close for the second line?
Thanks again for everything!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Financial Toolbox 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!

Translated by