How to subset a table and pass the variable names?

조회 수: 19 (최근 30일)
Leon
Leon 2020년 5월 8일
댓글: Leon 2020년 5월 8일
I'm trying to subset a large table to only get the rows that I'm interested in. Below is my code:
My table is named T
% unique list of variable A (Variable A is saved in the column of VAR_A):
VAR_A_unique = unique(T.VAR_A, 'rows');
Ind = T.VAR_A == VAR_A_unique(1);
T2 = T{Ind,:}
T2.Properties.VariableNames = TT.Properties.VariableNames;
My question is how do I assign the Properties variable names to T2? Why does my command (the last row) get the below error?
Unable to perform assignment because dot indexing is not supported for
variables of this type.
Instead of using
T2 = TT{Ind,:}
Is there a better way of subsetting my table?
Many thanks!

채택된 답변

Akira Agata
Akira Agata 2020년 5월 8일
Instead of using T2 = T{Ind,:} (I believe TT{Ind,:} is typo and T{Ind,:} is correct), the following can extract the corresponding rows with keeping variable names.
T2 = T(Ind,:);
Another way to do this more efficiently would be utilizing output variable of unique function, like;
[~,~,pt] = unique(T.VAR_A); % <- No need to use 'rows' option
T2 = T(pt==1,:);

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by