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

4 views (last 30 days)
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

Accepted Answer

Michael Haderlein
Michael Haderlein on 8 Aug 2014
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 Comments
Robbin
Robbin on 8 Aug 2014
Edited: Robbin on 8 Aug 2014
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 on 8 Aug 2014
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 ;-)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!