Skipping the column header from textread

10 次查看(过去 30 天)
I'm importing data from a text file with a list of names, but I need to skip the column header that says "FirstName".
list = textread(names.text,'%s %*s')' % skips second column and translates from column vector to row vector
How do I tell MATLAB to skip the header? In other words, I need textread to skip the first row in the list.

回答(2 个)

Walter Roberson
Walter Roberson 2018-4-15
'headerlines', 1
Note: textread() is not recommended. It has been considered obsolete for years.
  3 个评论
Walter Roberson
Walter Roberson 2018-4-15
fid = fopen(filename, 'rt');
listcell = textscan(fid, '%s %*s', 'headerlines', 1);
fclose(fid);
list = listcell{1};
However I would suggest
fid = fopen(filename, 'rt');
listcell = textscan(fid, '%s%*[^\n]', 'headerlines', 1);
fclose(fid);
list = listcell{1};
This would read to end of line and skip that. The %*s you used would only skip to the end of the first non-whitespace.
Matt Rulli
Matt Rulli 2018-4-15
Walter.... You are an angel walking amongst mere mortals! Thank you, sir!!

请先登录,再进行评论。


Gabriel Felix
Gabriel Felix 2020-5-24
I had to use \n at the end of each line. Without it I couldn't make textscan() work properly, even thoug the "HeaderLines" was configured according to the text file lines. This was the only solution I found after struggling with the code for an intire day.
This was the text:
!
!
! alfa (graus) = 5.0
!
! Id. x/s z/s alfai cl c*cl/cmed cdi cmc/4
! (graus)
1 .246 .050 -1.209 .255 .332 .00538 .0170
2 .292 .150 -1.098 .259 .319 .00496 .0545
3 .339 .250 -.925 .254 .297 .00410 .0944
4 .385 .350 -.741 .243 .268 .00315 .1341
5 .432 .450 -.561 .227 .235 .00223 .1714
6 .479 .550 -.393 .206 .199 .00141 .2034
7 .525 .650 -.238 .181 .163 .00075 .2266
8 .572 .750 -.101 .152 .126 .00027 .2362
9 .619 .850 .014 .116 .089 -.00003 .2236
10 .659 .938 .103 .074 .052 -.00013 .1693
!
! CL asa = .208
! CDi asa = .00258
! e (%) = 88.9
! CMc/4 asa = .1339
My code:
%! alfa (graus) = 5.0
P = textscan(fid,'! alfa (graus) = %f','Delimiter',' ','MultipleDelimsAsOne',true,'headerLines',2,'CollectOutput',1);
alpha(1) = P{1};
%! CL asa = .208
P = textscan(fid,'! CL asa = %f\n','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'headerLines',4+n);
CL(1) = P{1};
%! CDi asa = .00258
P = textscan(fid,'! CDi asa = %f\n','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'headerlines',0);
CDi(1) = P{1};
%! CMc/4 asa = .1339
P = textscan(fid,'! CMc/4 asa = %f','Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',1,'HeaderLines',2);
Cmc4(1) = P{1};

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by