Reading Selected Columns from an Excel Using the "readtable" Option

조회 수: 269 (최근 30일)
FW
FW 2019년 9월 21일
댓글: FW 2019년 9월 21일
If we have an Excel file, and instead of selecting a range of columns, I would like to select certain columns. The following syntax can read the range of columns
Table_1 = readtable('FileName.xlsx','Sheet','SheetName','Range','A1:E11'). What is the syntax for reading column A from row 50 to 10000 and column C from row 50 to10000 and skipping columns B?
This is MATLAB 2019a.
Thanks.

채택된 답변

Guillaume
Guillaume 2019년 9월 21일
readtable does not accept disjoint ranges (e.g. 'A50:A10000,C50:C10000') so you don't have a choice but read the B column as well and discard afterward:
Table_1 = readtable('FileName.xlsx','Sheet','SheetName','Range','A50:C10000'); %read column A to C
Table_1(:, 2) = []; %discard B column
Alternatively, you could use detectImportOptions then modify the import options to remove the columns you don't want:
opts = detectImportOptions('FileName.xlsx','Sheet','SheetName','Range','A50:C10000'); %still have to specify the full range
opts.SelectedVariableNames = opts.SelectedVariableNames([1, 3]); %ignore second column
Table_1 = readtable(FileName.xlsx', opts)
Another workaround might be to name the desired range in excel but I can't remember if you can create named disjoint ranges. If it's possible, you could pass the range name to readtable. Of course, this requires the range to be named beforehand in excel, so may not be practical.
  댓글 수: 1
FW
FW 2019년 9월 21일
Thank you. Glad to know that disjoint columns cannot be read. I was searching for almost a day.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by