Using Summary on a table to access just the last 2 column stats
Show older comments
Hello, I have an array of numbers that I want to apply the "summary" feature associated with tables to. This is because I want to exploit the stats this gives

The issue Im having is that the number of cloumns and clomn names isnt always the same. what is the same is I really want the stats for the last 2 columns always
This is what I have done so far:
T=app.UITable;
d=T.Data;
cn=T.ColumnName
d2=array2table(d,'VariableNames',cn)
% d2.Properties
S=summary(d2)
% B = string(fieldnames(S))
% B(end)
But is is just a struct, so I wanted to access the last 2 column details which I understand is via
S.fieldname.
But thats my issue, the field name is in cn but doing
S.cn(end)
doesn't work
Accepted Answer
More Answers (2)
As shown on this documentation page, one of the ways to extract a sub-table from a table is numeric indices.
M = magic(5);
sampleTable = array2table(M)
I could use variable names:
subtable1 = sampleTable(:, ["M4", "M5"])
Or I could use numbers.
subtable2 = sampleTable(:, [4 5])
Using end in the indexing expression also works.
subtable3 = sampleTable(:, [end-1 end])
4 Comments
Another syntax way that uses variable names with the [end-1:end] indexing
M = magic(5);
sampleTable = array2table(M)
sampleTable(:,sampleTable.Properties.VariableNames([end-1:end]))
This syntax is useful if one knows a given matching name or pattern of names but the position isn't necessarily fixed.
Yes, you could match patterns or use string processing functions if you operate on the variable names.
M = magic(5);
sampleTable = array2table(M, ...
VariableNames = ["ace", "king", "queen", "jack", "ten"])
namesContainA = sampleTable(:, ...
contains(sampleTable.Properties.VariableNames, 'a'))
Jason
on 24 Apr 2025
You could use indexing or dynamic {field, variable, property} names, depending on whether S is a {struct, table, object}.
M = magic(5);
sampleTable = array2table(M)
n = randi(width(M), 1)
varname = "M"+n
S1 = sampleTable.(varname) % double vector
S2 = sampleTable(:, varname) % table
S3 = sampleTable{:, varname} % double vector
Use the previous illustrated syntax on the summary table, too...you said you knew the columns of interest are the last two, so
tM=array2table(magic(5));
S=summary(tM(:, [end-1 end]))
S.Var4
fnames=fieldnames(S)
stats_I_want=S.(char(fnames(end)))
It might be more simpler to use groupsummary:here depending on just what you are looking for even though may have to create a fake grouping variable...
tM1=tM(:, [end-1 end]);
tM1=addvars(tM1,ones(height(tM1),1),'NewVariableNames','Group','Before',tM1.Properties.VariableNames(1))
S=groupsummary(tM1,'Group','all',tM1.Properties.VariableNames(end))
You can select any subset of the above and/or add others as well...
Alternatively, with it you can select which variables to use as the data variables as input rather than subselecting another table...
datavars=false(1,width(tM)); datavars(end)=true; % select only last variable
tM=addvars(tM,ones(height(tM),1),'NewVariableNames','Group','Before',1)
S=groupsummary(tM,'Group','all',datavars)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!