readtable changing range (not all contents)

8 ビュー (過去 30 日間)
Dave
Dave 2018 年 12 月 11 日
コメント済み: Jeremy Hughes 2018 年 12 月 14 日
Hello,
Manually Importing an XLS file (right click import data) the window by default highlights a matrix, say C10:X90
If I manually change the default "column vectors" to "table" and then click import, then it imports the matrix I need.
I need to do this for many files. However, using readtable('file1.xls') it imports all contents, beyond that matrix.
The matrix range changes from file to file so I cannot fix it to C10:X90, so file2.xls can be C15:X80, etc
How can I import the highlighted matrix (which changes from file to file) in a loop?
  7 件のコメント
Dave
Dave 2018 年 12 月 13 日
I manage to get the startRow but cannot seem to get the endRow, help
opts = detectImportOptions('Importing.xls');
startRow=opts.DataRange
Walter Roberson
Walter Roberson 2018 年 12 月 13 日
With that file the highlighted area is A7:F20. There is numeric data in F7 and there is no obvious reason why it should be excluded.
If you were to use
[num,txt,raw] = xlsread('Importing.xls')
then num would correspond to B7:F15
If you want to automate then you need to define more rigorously what is to be imported or not. For example is the rule that you are always extracting from the left side, column A? And is the rule that you always stop at the first empty column (excluding header lines) ?

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2018 年 12 月 14 日
編集済み: Cris LaPierre 2018 年 12 月 14 日
Not sure how to detect the end of your dataset before using readtable, but what about reading it in and then deleting the extra rows? For this, I'm assuming Title1 will be populated for every row that has data. Adjust for how your data actually behaves.
opts = detectImportOptions('Importing.xls');
startRow=opts.DataRange
tbl = readtable('Importing.xls',opts)
tbl(ismissing(tbl.Title1),:) = []
Probably worth stating that this also assumes every file has a column header Title1 that contains the Char2, ... values.
  1 件のコメント
Jeremy Hughes
Jeremy Hughes 2018 年 12 月 14 日
If you're already using import options,
opts.MissingRule = 'omitrow' will remove rows with missing. However that will omit rows with ANY missing data in ANY column.
But, if all you want is to read a block, setting opts.DataRange to the beginning cell of that range will read until it reaches the bottom, then stop.
e.g.
opts.DataRange = 'B3'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by