textscan not eating EOL

조회 수: 1 (최근 30일)
Jérôme
Jérôme 2014년 2월 14일
답변: Walter Roberson 2014년 2월 14일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2014년 2월 14일
Perhaps
'Delimiter',{'\t', \n'}

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by