Create an extra row in a table that show the means of all columns (but without any extra headings)

조회 수: 2 (최근 30일)
Hello, I have a table that I have created and display it in text area
This is my code:
params = ["ROI_L";"ROI_C";"ROI_R"];
tbl = table(params,FWHMX,FWHMY,FWHM2D,CenInt);
str = formattedDisplayText(tbl);
str = regexprep(str,'</?strong>','');
ReportMessage(app,str); % My own function, see below
At the bottom I have attempted to add the means of each column by doing this:
% Get Means
M = varfun(@mean, tbl, 'InputVariables', @isnumeric);
M1=table2cell(M)
M1=[{'Mens:'},M1]
str = formattedDisplayText(M1)
str = regexprep(str,'</?strong>','');
ReportMessage(app,str); % My own fucntion, see below
But its wrapping and not aligned to the table. The same thing happens if I DONT convert the table to a cell array via (as well as adding exctra headings
M1=table2cell(M)
M1=[{'Mens:'},M1]
This is how it looks without the table2cell step:
Is there a way in table format to get the means of the columns (but without any column header)
heres my report message function
function ReportMessage(app,msg)
currString=get(app.MessagesTextArea,'Value');
%currString=[{char(msg)};currString]; %add to top of message box
currString=[currString; {char(msg)}]; %add to bottom of message box
app.MessagesTextArea.Value=currString;
drawnow;
scroll(app.MessagesTextArea,'bottom');
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 7월 4일
Perhaps something like,
params = ["ROI_L";"ROI_C";"ROI_R"];
tbl = table(params,FWHMX,FWHMY,FWHM2D,CenInt);
M = ["Means", table2cell(varfun(@mean, tbl, 'InputVariables', @isnumeric))];
tbl{end,:} = M;
str = formattedDisplayText(tbl);
str = regexprep(str,'</?strong>','');
which should add a row with name "Means" to the end of the table.
Jason
Jason 2025년 7월 4일
편집: Jason 2025년 7월 4일
Perfect Walter thankyou.
(I did have to change this
tbl{end,:} = M;
to this
tbl{end+1,:} = M;
as it was overwriting my last row

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

채택된 답변

Chuguang Pan
Chuguang Pan 2025년 7월 4일
편집: Chuguang Pan 2025년 7월 4일
params = ["ROI_L";"ROI_C";"ROI_R"];
FWHMX = [3.09;3.14;3.36];
FWHMY = [3.00;2.92;2.88];
FWHM2D = [3.05;3.03;3.12];
CenInt = [217;212;162];
tbl = table(params,FWHMX,FWHMY,FWHM2D,CenInt);
meanTbl = mean(tbl(:,2:end));
meanTbl.params = "Mean";
newTbl = [tbl;meanTbl];
str = formattedDisplayText(newTbl,"NumericFormat","bank")
str =
" params FWHMX FWHMY FWHM2D CenInt _______ _____ _____ ______ ______ "ROI_L" 3.09 3.00 3.05 217.00 "ROI_C" 3.14 2.92 3.03 212.00 "ROI_R" 3.36 2.88 3.12 162.00 "Mean" 3.20 2.93 3.07 197.00 "

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by