Using Summary on a table to access just the last 2 column stats
5 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
dpb
2025 年 4 月 25 日
編集済み: dpb
2025 年 4 月 25 日
You fell into a trap of using a routine that isn't suitable for your purposes...while summary does compute a bunch of statistics without much need for user input, its output wasn't designed to return those parameters for other than viewing.
For your purposes I'd suggest doing something more on the line of
tM=array2table(magic(5)); % out friendly sample dataset yet again...
format bank, format compact % just to make output consistent...
% our supporting and engine code...
ourstats={'Min','Max','Mean','Median','StD'}; % a list of wanted statistics
fnS=@(x)[min(x);max(x);mean(x);median(x);std(x)]; % define our wanted statistics as anonymous function
Statistics=fnS(tM.(width(tM)-1)); % compute the statistics as array
tS=array2table(Statistics); % convert to table
tS.Properties.RowNames=ourstats % add the identifier of which is which
The statistic names can be augmented as desired or the variable identified as the column header, your choice. It's also straightforward to add the units column.
I haven't messed with the uitable enough to know about all the internal formatting it supports although I know there is quite a bit for presentation-only purposes available, but building the base data is pretty straightforward from a "deadahead" path rather than trying to parse a difficult-to-use builtin function output.
Probably you didn't recognize that can build an anonymous function comprising several of the builtin functions outputs catenated as long as they all reduce to a common data type.
NOTA BENE:
Statistics=fnS(tM.(width(tM)-1));
is yet another addressing mode of using the desired column number. width()-1 is the same as the subscripting expression end-1 without needing the array reference for the end function to key against.
6 件のコメント
その他の回答 (2 件)
Steven Lord
2025 年 4 月 24 日
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 件のコメント
Steven Lord
2025 年 4 月 24 日
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
dpb
2025 年 4 月 24 日
編集済み: dpb
2025 年 4 月 24 日
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)
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!