Problem with Textscan, CSV file and "blank" char

2 views (last 30 days)
Hi!
I have to read a large .csv file. I usually work with .dat files, so I'm a bit lost. The file contains dates and numbers in the following way:
4,28/11/2011 11:38:27:621,114.73137,301.64963,81.850418,835.99581,395.05321,229.46274
5,28/11/2011 11:39:27:621,114.96312,298.70543,81.948063,837.02882,395.99334,229.92624
I don't know if it is relevant, but each complete row is in one Excel cell; so when I open the .csv file in Excel, it only has one column and multiple rows.
As you can see, the delimiter is the comma ','.
The file has 15 headlines.
I tried the following:
Scan=textscan(fileID,'%f %q %f %f %f %f %f %f','Delimiter',',','Headerlines',15);
But MATLAB creates an empty 1x8 cell. It seems MATLAB can't read the numbers like numbers (%f), because if I try this:
Scan=textscan(fileID,'%q','Delimiter',',','Headerlines',15);
MATLAB returns a 1x1 cell with contains a large 32609x1 cell. This last cell, with the example data, would be like this:
' 4 '
' 1 9 / 1 1 / 2 0 1 1 1 2 : 4 5 : 3 7 : 7 0 4 '
' 1 2 0 . 2 6 1 3 2 '
' 1 4 5 . 8 1 4 5 1 '
' 1 4 0 . 3 9 8 0 5 '
' 3 4 8 . 2 8 6 0 6 '
' 3 1 6 . 9 5 8 5 4 '
' 2 4 0 . 5 2 2 6 4 '
' 5 '
' 1 9 / 1 1 / 2 0 1 1 1 2 : 4 6 : 3 7 : 7 0 4 '
' 1 3 7 . 0 6 4 9 2 '
' 1 5 7 . 2 3 3 5 8 '
' 1 5 4 . 4 4 1 1 6 '
' 5 2 7 . 5 2 4 7 6 '
...
Why MATLAB only reads the numbers as a string? Why creates a new row when it find a comma? Why inserts a 'blank' whitespace before and after every char? This blank whitespace is not a whitespace, I verified it with isspace(), so I don't know how to delete it from the string...
So, please, how can I read correctly this .csv file?
Thanks in advance!
  2 Comments
Walter Roberson
Walter Roberson on 16 Nov 2015
Is it possible that the file is UTF-16 encoded?
Álvaro Peñalva
Álvaro Peñalva on 17 Nov 2015
Edited: Stephen23 on 17 Nov 2015
That's it! The file was UTF-16 encoded. I encoded it in ANSI and textscan works perfectly!
Huge thanks!!

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!