MATLAB Answers

Tyla Ono

Keep specific column variables and delete others?

Asked by Tyla Ono
on 18 Aug 2018
Latest activity Commented on by Tyla Ono
on 19 Aug 2018
Hi, I have a table with a list of column variables (Eg: [[Var1 Var2 Var3 Var4 VarN]. I am trying to keep only specific variables and associated column data, whilst deleting all others.
For example, I want only variable 2 and 4, while deleting all other columns. Desired output --> [Var2 Var4]
It is possible to output only the desired columns without individually deleting unwanted variables?


Sign in to comment.


1 Answer

Answer by Paolo
on 18 Aug 2018
 Accepted Answer

Var1 = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Var2 = [38;43;38;40;49];
Var3 = logical([1;0;1;0;1]);
T = table(Var1,Var2,Var3)
To keep columns one and three:
T = [T(:,1) T(:,3)]
T =
5×2 table
Var1 Var3
_________ _____
'Sanchez' true
'Johnson' false
'Li' true
'Diaz' false
'Brown' true
Adapt the code for columns 2 and 4.

  1 Comment

Thank you.

Sign in to comment.