Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:

93 ビュー (過去 30 日間)
Hello, I am new to MATLAB. I am using the example of forecasting a sequence using deep learning by adapting the code from this example:
I am getting an error that suggests I use a subscript on the part of the code where data is trained. I placed a line underneath where I read in the data, thinking this would suffice. I am not sure why MATLAB is expecting something more for the part where you allocate training and test datasets.
data = readtable('numfile.xlsx')
data(5572,1)
numObservations = numel(data);
idxTrain = 1:floor(0.9*numObservations);
idxTest = floor(0.9*numObservations)+1:numObservations;
dataTrain = data(idxTrain);
dataTest = data(idxTest);
The error appears to be highlighting dataTrain. Why would this error occur? I'm using the same code but a different data file.
Thank you.

採用された回答

Voss
Voss 2022 年 3 月 13 日
The error seems to be happening because data is a (scalar) table, but the rest of the code was written to expect data to be an array of numbers.
You can try getting the data out of the table and using that:
data = readtable('numfile.xlsx')
data = data{:,1}; % get the first column of data from the table 'data'. store it as 'data'.
numObservations = numel(data);
idxTrain = 1:floor(0.9*numObservations);
idxTest = floor(0.9*numObservations)+1:numObservations;
dataTrain = data(idxTrain);
dataTest = data(idxTest);
  4 件のコメント
Peter Perkins
Peter Perkins 2022 年 3 月 14 日
Unless the data are a column vector, it's probably counterproductive to turn this table into a (numeric?) matrix.
If the data are a numeric column vector, use readmatrix instead of readtable.
Shawn Berry
Shawn Berry 2022 年 3 月 14 日
Hi Peter, thanks. It is definitely as a single column.

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

その他の回答 (2 件)

yanqi liu
yanqi liu 2022 年 3 月 14 日
data = xlsread('numfile.xlsx')
data(5572,1)
numObservations = size(data, 1);
idxTrain = 1:floor(0.9*numObservations);
idxTest = floor(0.9*numObservations)+1:numObservations;
dataTrain = data(idxTrain,:);
dataTest = data(idxTest,:);
  2 件のコメント
Peter Perkins
Peter Perkins 2022 年 3 月 14 日
編集済み: Peter Perkins 2022 年 3 月 14 日
DON'T use xlsread, as the doc warns. It's old and not recommended. Use readtable (as you were doing) or readmatrix.

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


Peter Perkins
Peter Perkins 2022 年 3 月 14 日
As the error message suggests, all you need is a colon as the second subscript.
data = readtable('numfile.xlsx')
...
dataTrain = data(idxTrain,:);
dataTest = data(idxTest,:);
If the data are one column vector (which seems unlikely since you are doing deep learning), use readmatrix instead of readtable, and then you can use just one subscript.
  2 件のコメント
Shawn Berry
Shawn Berry 2022 年 3 月 14 日
Hi Peter, just a question because your suggestion may explain why I'm getting another error (I am new to deep learning and working with a single column). The previous answer that I accepted seems to have solved that error but could your suggestion (readmatrix) work to solve a subsequent error I got? It's the 'brace indexing not supported for variables of this type"? In the example I am working from, they use curly braces for XTrain and TTrain but that dataset has more variables, while my data is univariate. Would this affect the error I am getting?
Thanks !
Peter Perkins
Peter Perkins 2022 年 3 月 15 日
If you are using someone else's code that expects a table, then you need to use tables, even if you only have one variable. It sounds like you are in that situation. "Someone else" might be The MathWorks, and if that's the case I recommend that you read the doc for whatever functions you are using.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by