필터 지우기
필터 지우기

Find Column Number in Table

조회 수: 77 (최근 30일)
Robot
Robot 2014년 10월 7일
댓글: Robot 2014년 10월 9일
I would like to subset a table into columns before a certain variable and columns after that variable. Is there a way to locate a column number within a table? Is there a more clever way to do this?
load patients
BloodPressure = [Systolic Diastolic];
T = table(Gender,Age,Smoker,BloodPressure,'RowNames',LastName);
A = *findColNumber*(T, 'Age'); % Some function that will locate the column number of 'Age'.
T1 = T(:, 1:A);
T2 = T(:, A+1:end);
'patients' is a standard example data set included in R2014a. Entering the command 'load patients' should load the relevant data.
Thanks!

채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 10월 8일
Table variable have property fields
you can use that to find the column number like this
find(strcmpi(T.Properties.VariableNames,'Age'))
ans =
2
  댓글 수: 4
Image Analyst
Image Analyst 2014년 10월 8일
편집: Image Analyst 2014년 10월 8일
Yeah, your T is slightly different than mine. No time now, but tomorrow I'll have to investigate why your code and mine behave differently and have slightly different T's. You'd think they should be the same even though they were gotten in different ways.
Actually I just figured it out. With my code, load() returns T as a structure not a table. So that makes sense.
Robot
Robot 2014년 10월 9일
Thanks for the help!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 7일
You don't need the column number. Just do
ages = T.Age;
  댓글 수: 3
Image Analyst
Image Analyst 2014년 10월 8일
편집: Image Analyst 2014년 10월 8일
You can get the age column like this:
T = load('patients')
ageColumn = find(ismember(fieldnames(T), 'Age'))
Image Analyst
Image Analyst 2014년 10월 8일
Actually T is a structure, so I recommend Mohammad's way.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by