- 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. "
Why doesn't 'HeaderLines' work in the readtable command?
146 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2018-3-31
"When reading:
You are reading a spreadsheet file. You need to use Range instead of HeaderLines
1 个评论
更多回答(1 个)
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
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
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 Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!