MATLAB: Textscan ignores last element in CSV file if it is empty.

textscan csvtextscan nan

I am using textscan to read numbers from a CSV file. Some data elements are empty and for these I want "NaN" and this works fine unless the last element in CSV file is empty, i.e. the last character in the file is a coma. When that is the case the last column is one element shorter than the others.
Example:
Data in file:
2016-12-11 08:48:06.885,4,5,-5,-2,3.3,35.5
2016-12-11 08:48:07.888,4,5,-5,-2,3.3,35.5
2016-12-11 08:48:08.885,4,5,-5,-2,3.3,35.5
2016-12-11 08:48:09.885,4,5,-5,-2,3.3,
format = '%s%n%n%n%n%n%n';
delim = ',';
data_raw = textscan(fid, format, 'delimiter', delim);
K>> data_raw{1,7}
ans =
35.5000
35.5000
35.5000
Expected answer =
35.5000
35.5000
35.5000
NaN

Best Answer

The following fixes the missing value
format = ['%s' repmat('%f',1,6)];
delim = ',';
fid=fopen('input_csv_sample.txt')
% data_raw = textscan(fid, format, 'delimiter', delim);
data_raw = textscan(fid, format, 'delimiter', delim,'EmptyValue',NaN,'CollectOutput',1);
fclose all
the output format of data_raw now is a couple of cells
data_raw{1}
ans =
'2016-12-11 08:48:06.885'
'2016-12-11 08:48:07.888'
'2016-12-11 08:48:08.885'
'2016-12-11 08:48:09.885'
>> data_raw{2}
ans =
Columns 1 through 4
4.000000000000000 5.000000000000000 -5.000000000000000 -2.000000000000000
4.000000000000000 5.000000000000000 -5.000000000000000 -2.000000000000000
4.000000000000000 5.000000000000000 -5.000000000000000 -2.000000000000000
4.000000000000000 5.000000000000000 -5.000000000000000 -2.000000000000000
Columns 5 through 6
3.300000000000000 35.500000000000000
3.300000000000000 35.500000000000000
3.300000000000000 35.500000000000000
3.300000000000000 NaN
.
if you find these lines of any help would you please be so kind to mark them as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John BG