Separating repeated values in an excel column.
显示 更早的评论
I have two excel columns - one contains 'Names' and other contains 'Reading' ( numerical value between -2 and +2) corresponding to the same.
For each unique name, there are 10-12 readings available.
I need to write a program which reads every unique name from the column and plots all the readings corresponding to that name in a violin plot (I have the code for it).
eg. I must choose one unique entry at a time (say Alpha) and plot all the corresponding readings for alpha on a violin plot. This will give me two plots, one for alpha and another for beta.
How do I read all the repeating values of only one unique entry at a time and perform operations on them?
回答(2 个)
Scott MacKenzie
2021-5-26
When you say "plot all the 12 readings", I assume there are exactly 12 readings per name. In this case...
T = readtable('yourdata.xlsx');
names = unique(T{:,1});
j = 1;
for i=1:length(names)
nameLogical = strcmp(T{:,1}, names{i});
M(:,j) = T{nameLogical, 2};
j = j + 1;
end
% plot data in M
Alternatively...
T = readtable('yourdata.xlsx');
T = sortrows(T, 1); % if data are not sorted
M = T{:, 2};
M = reshape(M, 12, [])
% plot data in M
1 个评论
Aditya Raghav Trivedi
2021-5-26
编辑:Aditya Raghav Trivedi
2021-5-26
Scott MacKenzie
2021-5-26
If the number of readings varies, as you say, from 8 to 12, then...
T = readtable('yourdata.xlsx');
names = unique(T{:,1});
j = 1;
for i=1:length(names)
nameLogical = strcmp(T{:,1}, names{i});
M(:,j) = T{nameLogical, 2};
M(end+1:12,j) = nan;
j = j + 1;
end
% plot data in M
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!