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

조회 수: 6(최근 30일)
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

채택된 답변

Geoff
Geoff 2012년 2월 24일
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
Laurentiu Galan
Laurentiu Galan 2012년 2월 24일
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!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by