How to read Excel files with unknown number of header rows?
Show older comments
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!
6 Comments
Walter Roberson
on 11 Feb 2020
detectImportOptions can often figure it out. Not always though.
Walter Roberson
on 5 Oct 2022
If you use
opt = detectImportOptions('example.xlsx', 'DataRange', 3, 'VariableNamesRange', 1, 'DataRange', 3, 'VariableUnitsRange', 2);
then it gets all the parts right -- but for reasons not clear to me, it gets two extra rows of missing data at the end.
Leon
on 5 Oct 2022
Walter Roberson
on 5 Oct 2022
Is there anything that is consistent between the files? Same number of variables with the same headers?
If there is a variable number of header lines, then is the variable names always going to be the first row, and the variable units always going to be the second row? Or is the variable names always the first row but the variable units is always the row before the data? Or are there a variable number of headers all followed by names and then units and then data?
Leon
on 6 Oct 2022
Accepted Answer
More Answers (1)
Shashwat Bajpai
on 13 Feb 2020
1 vote
Hope this Helps!
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!