Skipping the column header from textread
11 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Walter Roberson
on 15 Apr 2018
'headerlines', 1
Note: textread() is not recommended. It has been considered obsolete for years.
3 Comments
Walter Roberson
on 15 Apr 2018
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.
Gabriel Felix
on 24 May 2020
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};
0 Comments
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!