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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by