How to find the index of string column in a table?
조회 수: 35 (최근 30일)
이전 댓글 표시
How to get the column ids of a table with string column.
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Health = {'Fair';'Poor';'Excellent';'Good';'Fair'};
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T1 = table(LastName,Age,Health,Height,Weight,BloodPressure)
In the above example the first and third columns are of string datatype.
So I wanted to get
idx = [1 3];
How do I find the index of string column?
댓글 수: 0
채택된 답변
Cris LaPierre
2023년 4월 25일
First comment - none of your table variables are strings. They are cell arrays of character vectors. That may matter depending how you implement a solution.
There are a couple ways depending on what you ultimately want to do. If you want to extract the colums, see the Access Data in Tables doc page. Use this syntax: S = vartype(type); T{rows,S}
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Health = {'Fair';'Poor';'Excellent';'Good';'Fair'};
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T1 = table(LastName,Age,Health,Height,Weight,BloodPressure)
S = vartype("cell");
T1(:,S)
However, if you do want the index number, the most direct way I can think of is to use varfun with iscell.
idx_LI = varfun(@iscell,T1,'OutputFormat','uniform')
C = 1:width(T1);
idx = C(idx_LI)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!