Problem using textscan to read strings with space inbetween from CSV file

조회 수: 5 (최근 30일)
Hi, I've got a problem when trying to using textscan to load the FX data into matlab. The columns are separated by semicolon, but the problem is that there is a space between the date and time strings in the second column. So far, I can only load the data with the following code after trying various delimiter settings:
fid = fopen('test.csv');
rawData = textscan(fid, '%s %s');
However, the outcome is that, counter mixed with date, time stamp mixed with bid/offer prices. Could any one please help me to write the code, so that the first cell contains counter, second column contains date and time, third column for bid price, and fourth for offer price?
Thanks in advance!
USD/JPY,20120102 00:00:00.307,77.023,7.055
USD/JPY,20120102 00:00:00.493,77.03,77.049
USD/JPY,20120102 00:00:05.003,77.03,77.05
USD/JPY,20120102 00:00:05.005,77.023,77.056
USD/JPY,20120102 00:00:05.006,77.024,77.056
USD/JPY,20120102 00:00:06.008,77.023,77.056
USD/JPY,20120102 00:00:06.239,77.03,77.049

채택된 답변

Michael Haderlein
Michael Haderlein 2014년 8월 8일
First, you have 4 columns (not 2), so the format is '%s%s%s%s'. Additionally, you have to set the delimiter (which seems to be comma not semicolon):
rawData=textscan(fid,'%s%s%s%s','delimiter',',');
  댓글 수: 2
Robbin
Robbin 2014년 8월 8일
편집: Robbin 2014년 8월 8일
Thank you, Michael! Perfect solution. I tried '%s%s%s%s' and '%s%s%f%f' before posting the question, but it didn't work. I guess it's because of the version.
Michael Haderlein
Michael Haderlein 2014년 8월 8일
You're welcome, however, I find that %s%s%f%f does make much more sense than all %s as I have posted. Anyway, you're gonna make it ;-)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by