MATLAB Answers

How to read Excel files with unknown number of header rows?

17 views (last 30 days)
Leon
Leon on 11 Feb 2020
Answered: Shashwat Bajpai on 13 Feb 2020
Below is my code to read an Excel file using readtable:
T1 = readtable ('test.xlsx','PreserveVariableNames',true);
Headers = T1.Properties.VariableNames;
A = T1{:,1};
Here is the problem. My Excel file could have unknow number of header rows (from 1 to 20). It seems that (a) the Headers are always the first row, and (b) the A values always start from the first all numerical row.
What I need is the Row # of the A values. If I know there is only one header row, I know the first element of A starts from Row # 2. With unknown number of header rows, how do I derive that Row # info of A?
Thanks!
  1 Comment
Walter Roberson
Walter Roberson on 11 Feb 2020
detectImportOptions can often figure it out. Not always though.

Sign in to comment.

Answers (1)

Shashwat Bajpai
Shashwat Bajpai on 13 Feb 2020
The spreadsheetDataStore function can help with this alongwith detectImportOptions
You can also use the Import Tool in the MATLAB Toolstrip to select the rows required.
Hope this Helps!

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!