How to extract certain rows and columns with the readtable option for Excel files.
Show older comments
Suppose we have an Excel file Data.xlsx. The file have certain descriptive text in the initial rows and then there is tabular data on several sheets. I would like to extract column 1 and column 3 but the useful row number starts from say, 30 and ends at 10000.
Table1=readtable('Data.xlsx', 'Sheet', 'Signal', 'Range', 'A:C');
Is there a better way to read such an Excel file? There is a lot of text in the initial rows of the Excel file. This is default way the instrument exports the data. How can we only read column A and column C whose useful rows start from 30 and end at 10000?
Thanks.
Answers (1)
(edited)
Using the import tool, then generating code and clearing unnecessary details, this appears to be the way:
opts = spreadsheetImportOptions("NumVariables", 3);
% Specify sheet and range
opts.Sheet = "Signal";
opts.DataRange = "A30:C10000";
% Specify column names and types
opts.VariableNames = ["A", "Var2", "C"];
opts.SelectedVariableNames = ["A", "C"];
% Import the data
Table1 = readtable("Data.xlsx", opts);
8 Comments
Sindar
on 24 Jan 2020
if this split range doesn't work, loading each separately and combining the tables may be more efficient than loading everything (i.e., A30:C10000) and discarding the unwanted slice (from column B)
FW
on 24 Jan 2020
Sindar
on 24 Jan 2020
Well, my optimism was misplaced. I've edited to a method that should actually work.
Sindar
on 25 Jan 2020
Try adding this line before readtable:
opts.VariableTypes = ["double", "char", "double"];
I assumed it would default correctly, but maybe not
Sindar
on 25 Jan 2020
Try looking at Table1.Variables, rather than using table2array. What type does this have?
Also, please don't edit comments to ask new questions, it makes it less likely that people will notice and be able to answer, and also makes the conversation harder to follow for people that have similar issues
FW
on 25 Jan 2020
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!