Problem keeping input file in the same order using textscan
1 回表示 (過去 30 日間)
古いコメントを表示
I have a csv file with about 2000 rows. The format of the file is below.
I use this code to read in the file and parse out the input vectors
fid = fopen('temp.csv');
data = textscan(fid,'%n%n%f', 'headerlines', '1', 'delimiter', ',');
tradeDate=data{:,1};
stockId = data{:,2};
price = data{:,3};
I then calculate the returns using this code returns = tick2ret(price);
As a sanity check I list the first four prices and they are not the same as the first four items in the text file. These records are somewhere in the middle of the file.
so price(1) is not equal to the first record in the input file price (2) is not equal to the second record and so on.
So return(1) does not match what the first return should be.
================INPUT FILE ================= t_date stock_id price_low 37623 16622 38.035 37624 16622 38.665 37627 16622 38.885 37628 16622 38.82 37629 16622 38.6 37630 16622 38.99 37631 16622 38.855 37634 16622 39.09 37635 16622 39.26 37636 16622 39.075 37637 16622 39.19 37638 16622 38.45 37642 16622 38.04
0 件のコメント
採用された回答
Matt Tearle
2011 年 2 月 17 日
Ahahahahaha! I'm gonna take a bet that price(1) is actually the price from the 49th row, right?
You'll love this. Instead of
data = textscan(fid,'%n%n%f', 'headerlines', '1', 'delimiter', ',');
try
data = textscan(fid,'%n%n%f', 'headerlines', 1, 'delimiter', ',');
See the difference? It's subtle... 1 ~= '1'!
6 件のコメント
Elsa Birch
2011 年 3 月 16 日
Hey Tearle! I stumbled upon this while trying to solve a problem about importing .csv files, and remember you emphasizing awareness of this in class. - Elsa
Matt Tearle
2011 年 3 月 17 日
Wow, someone actually listened to me babbling on in class? I've seen many cases of weird char2double issues, but this specific one was new to me. For the record, I've submitted an enhancement request that this behavior be changed, and it's under consideration.
その他の回答 (1 件)
Walter Roberson
2011 年 2 月 17 日
It works when I try it.
I note though that the text file you show as input does not match your format description: you set 'delimiter' to ',' as would be common for csv files, but the input file you presented has no commas.
Also, you should specify 'rt' as an additional option on fopen() as you are dealing with a text file.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!