73 views (last 30 days)
Show older comments
Robot on 7 Oct 2014
Commented: Robot on 9 Oct 2014
Accepted Answer: Mohammad Abouali
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!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Mohammad Abouali on 8 Oct 2014
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 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Image Analyst on 8 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241522
Cool - I didn't know about that. You taught me something. Of course he still won't be able to do what he wants to do (I tried).
Mohammad Abouali on 8 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241524
load patients
BloodPressure = [Systolic Diastolic];
T = table(Gender,Age,Smoker,BloodPressure,'RowNames',LastName);
A = find(strcmpi(T.Properties.VariableNames,'Age'));
T1 = T(:, 1:A);
T2 = T(:, A+1:end);
Strange. This works for me.
And I have learned also a lot from your posts and your matlab examples.
Image Analyst on 8 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241526
Edited: Image Analyst on 8 Oct 2014
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.
Sign in to comment.
More Answers (1)
Image Analyst on 7 Oct 2014
You don't need the column number. Just do
ages = T.Age;
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Robot on 7 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241508
Thanks for the response, however T.Age will return the values within column 'Age'. I am looking for the column number for 'Age' (IE: A = 2).
Image Analyst on 8 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241515
Edited: Image Analyst on 8 Oct 2014
You can get the age column like this:
T = load('patients')
ageColumn = find(ismember(fieldnames(T), 'Age'))
Image Analyst on 8 Oct 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157742-find-column-number-in-table#comment_241527
Actually T is a structure, so I recommend Mohammad's way.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsData TypesTables
Find more on Tables in Help Center and File Exchange
Tags
- table
- column
- find
- subset
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office