I want to call the fifth column from a table

조회 수: 1 (최근 30일)
Omer Yasin Birey
Omer Yasin Birey 2019년 1월 17일
답변: Peter Perkins 2019년 1월 23일
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

답변 (2개)

madhan ravi
madhan ravi 2019년 1월 17일
편집: madhan ravi 2019년 1월 17일
  댓글 수: 2
Omer Yasin Birey
Omer Yasin Birey 2019년 1월 17일
Thank you Madhan but I know this way. My question is about how to do it dynamically. I realized that I should edit the question.
madhan ravi
madhan ravi 2019년 1월 17일
Still not clear upload your table with datas and illustrate an example of output.

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


Peter Perkins
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)}

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by