I want to call the fifth column from a table
1 回表示 (過去 30 日間)
古いコメントを表示
Hi all, I want to call the columns from a table dynamically
cellCol ={'column_1' ,'column_2', 'column_3','column_4', 'column_5'};
inputTable = array2table(trainingData, 'VariableNames',cellCol);
predictorNames = cellCol(1:end-1);
predictors = inputTable(:, predictorNames);
response = inputTable.cellCol{length(cellCol)};
But it gives the following error
Error using tabular/subsrefDot (line 118)
Unrecognized variable name 'cellCol'.
Error in tabular/numArgumentsFromSubscript (line 55)
x = t.subsrefDot(s(1));
Is there a way to call a column from a table without typing its name?
Thank you
0 件のコメント
回答 (2 件)
madhan ravi
2019 年 1 月 17 日
編集済み: madhan ravi
2019 年 1 月 17 日
inputTable{:,5} % where inputTable is your table
2 件のコメント
madhan ravi
2019 年 1 月 17 日
Still not clear upload your table with datas and illustrate an example of output.
Peter Perkins
2019 年 1 月 23 日
Same way you'd do it with a struct: "dynamic field indexing", or "dynamic variable indexing" in this case.
response = inputTable.(cellCol{length(cellCol)})
If you want more than one var, use braces:
responses = inputTable{:,cellCol(whichVars)}
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Other Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!