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 Center および File ExchangeStandard File Formats についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by