Why doesn't 'HeaderLines' work in the readtable command?

조회 수: 140 (최근 30일)
njj1
njj1 2018년 3월 31일
댓글: Jeremy Hughes 2022년 7월 7일
I am importing some excel files into Matlab using the readtable command. There are a couple of header lines in the file that I would like to skip, but when I use the name-value pair ('HeaderLines',2), I get an error:
Error using readtable (line 198)
Invalid parameter name: HeaderLines.
Has anyone else experienced this? Is there a way to get around this error?

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 31일
"When reading:
  • Text files, only these parameter names apply: FileType, ReadVariableNames, ReadRowNames, TreatAsEmpty, DatetimeType, Delimiter, HeaderLines, Format, EmptyValue, MultipleDelimsAsOne, CollectOutput, CommentStyle, ExpChars, EndOfLine, DateLocale, and Encoding.
  • Spreadsheet files, only these parameter names apply: FileType, ReadVariableNames, ReadRowNames, TreatAsEmpty, DatetimeType, Sheet, Range, Basic, and TextType.
  • Text or Spreadsheet files with the opts import options, only these parameter names apply: ReadVariableNames, ReadRowNames, DateLocale, Encoding, Sheet, and Basic. "
You are reading a spreadsheet file. You need to use Range instead of HeaderLines
  댓글 수: 1
Jeremy Hughes
Jeremy Hughes 2022년 7월 7일
For more recent releases, NumHeaderLines is allowed in spreadsheet files.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bradley Stiritz
Bradley Stiritz 2018년 10월 21일
편집: Bradley Stiritz 2019년 4월 25일
Walter's solution is a good one, when the number of table rows are known in advance. In my case, I have Excel files with a fixed number of header rows, and variable number of data rows.
I reached out to tech support, and got the following solution. This works well for me, I hope it can help others also--
% Assume Excel file has 6 header rows
>>path_ = 'C:\data\my_file.xlsx';
>>ImportOptions_ = detectImportOptions(path_, 'NumHeaderLines', 6);
>>tb = readtable(path_,ImportOptions_);
(Edit: corrected minor bugs per Doug Bergman)
  댓글 수: 2
Doug Bergman
Doug Bergman 2019년 4월 25일
편집: Doug Bergman 2019년 4월 25일
Brad, your solution worked well for me, once i realized that
path
is the full filename and not just the directory path, and
IOpts
is the same as
ImportOptions_
Thanks for posting, saved me a day of pulling my hair out.
Bradley Stiritz
Bradley Stiritz 2019년 4월 25일
Hi Doug, thank you for your comment and correction notes. I'm glad you were able to save some time. Apologies for the sloppy code example, I have edited my post accordingly.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by