Categorical variable groups to cell array of strings for boxplot
显示 更早的评论
I have two categorical variables a and b and I am using the unique groups defined by both categories to build a boxplot, e.g.
a = categorical({'I'; 'II'; 'I'; 'I'});
b = categorical({'A'; 'B'; 'B'; 'C'});
d = [11; 20; 45; 16];
M = table(d, a, b);
G = findgroups(M, M.a, M.b)
boxplot(M.d, G)
Now I obviously know this is a silly example, the real data has many observations and ~20 unique groupings. I want to generate the x labels for the boxplot in a cell array labels such that labels contains the description of each group in the correct order they are plotted, e.g. labels = {'IA', 'IC', 'IIB', 'IB'}.
I have tried every combination of concatenation (e.g. horzcat), arrayfun, and char I can think of using the vectors a and b and also using the additional optional outputs from findgroups. For example the following seems close:
labels = strcat(strtrim(char(a(G))), char(b(G)))
labels =
IA
IB
IC
IIB
The problem is that because the lengths of the first categorical variable differ, the shorter strings are padded with spaces at the end. I feel as though this should be trivial, but how do I get these into a cell array of strings of variable length for use in labeling my boxplot?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Exploration and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!