필터 지우기
필터 지우기

Problem keeping input file in the same order using textscan

조회 수: 3 (최근 30일)
luke
luke 2011년 2월 17일
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

채택된 답변

Matt Tearle
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
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
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
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by