How to insert already computed values in a uitable

2 次查看(过去 30 天)
Hi, I’m computing some statistical values from a dataset (z). I’ll repeat this code for 7 more datasets and I would like that all statistical values are displayed in a table for my 8 datasets as follow:
std var mean ...
z1
z2
z3
.
.
.
For the moment, my code look like this and only displays the data in the command window. Thanks for your help!
load C1_Z1
z = C1_Z1(:,3);
%%%%%% STATISTICS
% number of observations
Nz = size(z);
% compute mean
mz = mean(z);
% compute the standard deviation
sigma = std(z);
% compute the median
medianz = median(z);
% compute variance
variance_z = var(z);
% compute skewness
coef_skew = skewness(z);
%compute max value
MaxValue = max(z);
% compute min value
MinValue = min(z);
% STEP 1 - rank the data
a = sort(z);
% compute 25th percentile (first quartile)
Q(1) = median(a(find(a<median(a))));
% compute 50th percentile (second quartile)
Q(2) = median(a);
% compute 75th percentile (third quartile)
Q(3) = median(a(find(a>median(a))));
% compute Interquartile Range (IQR)
IQR = Q(3)-Q(1);
% determine extreme Q1 outliers (e.g., x < Q1 - 3*IQR)
ia = find(a<Q(1)-3*IQR);
if length(ia)>0,
outliersQ1 = a(ia);
else
outliersQ1 = [];
end
% determine extreme Q3 outliers (e.g., x > Q1 + 3*IQR)
ia = find(a>Q(1)+3*IQR);
if length(ia)>0,
outliersQ3 = a(ia);
else
outliersQ3 = [];
end
% compute total number of outliers
Noutliers = length(outliersQ1)+length(outliersQ3);
% display results
disp(['Mamimum Value:',num2str(MaxValue)]);
disp(['Minimum Value:',num2str(MinValue)]);
disp(['Number of Observations:',num2str(Nz)]);
disp(['Mean: ',num2str(mz)]);
disp(['Standard Deviation: ',num2str(sigma)]);
disp(['Variance: ',num2str(variance_z)]);
disp(['Coefficient of Skewness: ',num2str(coef_skew)]);
disp(['Median: ',num2str(medianz)]);
disp(['25th Percentile: ',num2str(Q(1))]);
disp(['50th Percentile: ',num2str(Q(2))]);
disp(['75th Percentile: ',num2str(Q(3))]);
% disp(['Semi Interquartile Deviation: ',num2str(SID)]);
disp(['Number of outliers: ',num2str(Noutliers)]);
%%%Creation of a uitable
t = uitable;
set(t,'ColumnWidth',{25})
cnames = {'Mean','Median','STD', '...’};
rnames = {'z1','z2','z3', '...', 'z8’};
t = uitable('ColumnName',cnames,'RowName',rnames);
  2 个评论
Jan
Jan 2013-5-7
@Saskia: Did you look at your question after typing it? The unformatted code is such ugly, that I'm deeply surprised, that you do not ask how it can be formatted nicely.
It really puzzles me, that so many user simply do not care about the layout, although their question are obviously not readable. I'd expect dozens of questions in the comment sections like: "Hey, why does my code look such ugly, but in the other threads it is nice?!"
To your problem: Where should this "table" appear? In the command window, a UITABLE or in a file? What did you try and which problems occurred? Until now, you have only explained what you want to do, but you did not ask a question which could be answered.
Saskia
Saskia 2013-5-8
@Jan: Oups sorry no, I checked only the beginning of the prewiew. True it was really ugly but should be readable now.
For my problem: My code is basically computing many statistical values for one data set and the results are now displayed on my command window. I would like that they appear in a uitable at the end of the computation (figure) because I’ll compute exactly the same values for 7 other datasets (and I would like to set them all in 1 table). I was able to create a uitable with the good size and row/column names but I dunno how to set the data into it. Do you know ho to do that?
Cheers,
Saskia

请先登录,再进行评论。

采纳的回答

David Sanchez
David Sanchez 2013-5-8
Save the data of each set in an row of a previously created array, Then
my_data_matrix = zeros(m,n); % where m are the number of datasets and n the computed data ( mean, median.... ).
for k=1:m
my_data_matrix(k,:) = my_calculated_data_array_here;
end
% code to create table....from MATLAB help
f = figure;
data = my_data_matrix;
colnames = {'X-Data', 'Y-Data', 'Z-Data'};
t = uitable(f, 'Data', data, 'ColumnName', colnames, ...
'Position', [20 20 260 100]);

更多回答(1 个)

Image Analyst
Image Analyst 2013-5-8
Here's a demo for you:
% Create sample data and row and column headers.
columnHeaders = {'n', 'Data Set #1', 'Data Set #2'};
for n=1:10,
rowHeaders{n} = sprintf('Row #%d', n);
tableData{n,1} = n;
tableData{n,2} = 10*rand(1,1);
tableData{n,3} = sprintf('Value = %.2f %s %.2f', rand(1,1), 177, rand(1));
end
% Create the table and display it.
hTable = uitable();
% Apply the row and column headers.
set(hTable, 'RowName', rowHeaders);
set(hTable, 'ColumnName', columnHeaders);
% Display the table of values.
set(hTable, 'data', tableData);
% Size the table.
set(hTable, 'units', 'normalized');
set(hTable, 'Position', [.1 .1 .8 .8]);
set(hTable, 'ColumnWidth', {40, 120, 180});
set(gcf,'name','Image Analysis Demo','numbertitle','off')

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by