How do I read a file from todays date and previous date using .CSV file
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dipesh Ramesh
am 21 Jul. 2014
Kommentiert: Geoff Hayes
am 22 Jul. 2014
I am new to Matlab, I was wondering how do i read 1st row and 2nd row from a .CSV file. This is what I have done:
str = urlread('http://ichart.yahoo.com/table.csv?s=AMZN&a=0&b=1&c=2014&d=0&e=31&n=2014&g=d&ignore=.csv');
i = 0;
[dates,o,h,l,c] = dataread('string',str,'%s%f%f%f%f%*f%*f','delimiter',',','headerlines',1);
dates = datenum(dates);
firstDate = dates(2)
secondDate = firstDate(3)
if ()
i = i+1;
end
At the moment I can get the first row and second row, but what I need is the final row and final row +1. So from backward compare two rows at the time. Check if open price number in first row is grater than open price number from the second row (bottom up) if yes +1 and save file. For example from the above file the last few files looks like this:
Date,Open,High,Low,Close,Volume,Adj Close
2014-01-17,394.26,403.49,393.66,399.61,4505100,399.61
2014-01-16,393.68,399.29,389.41,395.80,2601200,395.80
2014-01-15,398.94,399.31,392.53,395.87,2678300,395.87
2014-01-14,392.13,398.63,391.29,397.54,2340100,397.54
2014-01-13,397.98,399.78,388.45,390.98,2844900,390.98
2014-01-10,402.53,403.76,393.80,397.66,2679500,397.66
2014-01-09,403.71,406.89,398.44,401.01,2103000,401.01
2014-01-08,398.47,403.00,396.04,401.92,2316500,401.92
2014-01-07,395.04,398.47,394.29,398.03,1916000,398.03
2014-01-06,395.85,397.00,388.42,393.63,3170600,393.63
2014-01-03,398.29,402.71,396.22,396.44,2210200,396.44
2014-01-02,398.80,399.36,394.02,397.97,2137800,397.97
So take 1st and 2nd lines below the file e.g.
2014-01-03,398.29,402.71,396.22,396.44,2210200,396.44
2014-01-02,398.80,399.36,394.02,397.97,2137800,397.97
Now compare if 398.80>398.29 if yes +1 in file. Once this is done go on to 2nd and 3rd line e.g.:
2014-01-06,395.85,397.00,388.42,393.63,3170600,393.63
2014-01-03,398.29,402.71,396.22,396.44,2210200,396.44
Now compare if 398.29>395.85 if yes +1 in the file so (2) etc.. do it till the first file in the list.
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 21 Jul. 2014
If you want to start from the end of the list, comparing the last two lines, the second and third last, etc., then use a for loop and just iterate backwards
[dates,o,h,l,c] = dataread('string',str,'%s%f%f%f%f%*f%*f','delimiter',',','headerlines',1);
dates = datenum(dates);
numElems = length(dates);
% iterate from last element in list to second element, comparing two at a time
for k=numElems:-1:2
% compare the two open price numbers
if o(k)>o(k-1)
% do something
end
end
The above code will allow you to do the correct comparisons; the do something comment refers to your if yes +1 in file (which I'm a little unclear on).
2 Kommentare
Geoff Hayes
am 22 Jul. 2014
If you want to start from the beginning of the list then iterate from 2 to the end (and so the code compares each row with the previous)
for k=2:numElems
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!