How to sum according to specific ranges within same table?

2 次查看(过去 30 天)
Hello!
Available data: Please see the attached table.
Required: As shown in the same Excel file i would like to calculate the summ of test results for each person and based on four different intervals (1-10), (10-20), (20-30) and (30-40). then return a plot also for each person.
Any suggestions?

采纳的回答

Pavan Guntha
Pavan Guntha 2021-4-1
Hi Hedi,
You could load the excel file as a table. You could refer to this documentation page link for more details. The following code illustrates the further steps to calculate sum based on a condition:
c = categories(TestConditionalSum.AvailableData);
resCat = zeros(length(c),4);
for i = 1:length(c)
t = TestConditionalSum(TestConditionalSum.AvailableData == c{i},:);
for j = 1:size(t,1)
if(t(j,:).From >= 30)
resCat(i,4) = resCat(i,4) + t(j,:).Res; % Res is the name of T-Results column in the table
elseif(t(j,:).From >= 20)
resCat(i,3) = resCat(i,3) + t(j,:).Res;
elseif(t(j,:).From >= 10)
resCat(i,2) = resCat(i,2) + t(j,:).Res;
elseif(t(j,:).From >= 1)
resCat(i,1) = resCat(i,1) + t(j,:).Res;
end
end
end
b = barh(resCat(4,end:-1:1)); % Example plot for datapoint 'Maya'
ylabel('Maya')
yticklabels({'4','3','2','1'})
The resCat matrix contains the required sum for the 4 respective intervals for each of the available datapoints. You could refer to the documentation of barh function for more details on plotting. The plot for the above code is as follows:
Hope this helps!

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by