textscan not eating EOL

1 view (last 30 days)
Jérôme
Jérôme on 14 Feb 2014
Answered: Walter Roberson on 14 Feb 2014
Hi.
I'm trying to read the following (tab separated values) file with textscan :
-------------------------------------
year day in year day in month month hour
Y DY DM M H
1900 1 1 1 1
1900 1 1 1 2
-------------------------------------
I would use the following code :
-------------------------------------
fid=fopen('test.txt', 'rt');
nb_col = 5;
t = textscan(fid, repmat('%s ', 1, nb_col),1, 'Delimiter','\t\n', 'CollectOutput',1);
headers = t{1};
t = textscan(fid, repmat('%f ', 1, nb_col), 'Delimiter','\t', 'HeaderLines', 1, 'CollectOutput',1);
values = t{1};
fclose(fid);
disp(headers)
disp(values)
-------------------------------------
Except this doesn't work. It seems the EOL of the header line is not eaten by textscan after the first call, and HeaderLines counts from there in the second call.
I need to enter 2 as HeaderLines.
t = textscan(fid, repmat('%f ', 1, nb_col), 'Delimiter','\t', 'HeaderLines', 2, 'CollectOutput',1);
Another workaround I found here is to enter \t\n as delimiter, although I don't really understand it since my delimiter is only \t and \n is only present at the end of the line.
t = textscan(fid, repmat('%s ', 1, nb_col),1, 'Delimiter','\t\n', 'CollectOutput',1);
t = textscan(fid, repmat('%f ', 1, nb_col), 'Delimiter','\t\n', 'HeaderLines', 1, 'CollectOutput',1);
Is this a known issue ?
I'd call it a bug, since textscan does not show the behavior I expect from it reading the doc, but it might be my understanding that is incorrect.
Thank you for your help.

Answers (1)

Walter Roberson
Walter Roberson on 14 Feb 2014
Perhaps
'Delimiter',{'\t', \n'}

Community Treasure Hunt

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

Start Hunting!