MATLAB Answers

How to retrieve a table entry using row name?

67 views (last 30 days)
KAE
KAE on 10 Apr 2020
Commented: KAE on 10 Apr 2020
The function anova returns a 3x5 table and I would like retreive the value from the row named Model and column named pValue. How do I do this? I don't know how to retreive a table entry by the row name, only by the row number.
mr = anova(lm,'summary')

Accepted Answer

dpb
dpb on 10 Apr 2020
It's a table; use the curlies "{}" to dereference row names to return the content of the row or regular paren's "()" to return a table:
Used example for anova to illustrate w/o your data to replicate--
load hospital
tbl = table(hospital.Age,hospital.Sex,hospital.BloodPressure(:,2), ...
'VariableNames',{'Age','Sex','BloodPressure'});
tbl.Sex = categorical(tbl.Sex);
mdl = fitlm(tbl,'BloodPressure ~ Sex + Age'); simplified model to remove nonlinear terms
>> tAnova=anova(mdl,'summary')
tAnova =
5×5 table
SumSq DF MeanSq F pValue
______ __ ______ _______ ________
Total 4757.8 99 48.059
Model 243.8 2 121.9 2.6194 0.077994
Residual 4514 97 46.537
. Lack of fit 1514 40 37.851 0.71916 0.86306
. Pure error 3000 57 52.632
>>
>> tAnova('Total',:) % retrieve Total SSQ by row name as table
ans =
1×5 table
SumSq DF MeanSq F pValue
______ __ ______ ___ ______
Total 4757.8 99 48.059 NaN NaN
>>
>> tAnova{'Total',:} % retrieve as array of values only
ans =
1.0e+03 *
4.7578 0.0990 0.0481 NaN NaN
>>
See the detailed description of addressing tables that is in the lind from "See Also" for table for all the skinny on all the ways to address tables.
  1 Comment
KAE
KAE on 10 Apr 2020
In my case, I wanted
mr{'Model', 'pValue'}
Thanks!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!