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?

 Accepted Answer

Okay well, naturally after posting I quickly figured out the solution. posting below for anyone else who stumbles across this.
[G, ~, ai, bi] = findgroups(M, M.a, M.b);
labels = cellstr(strcat(strtrim(char(ai)), char(bi)));

More Answers (0)

Asked:

on 30 Nov 2016

Edited:

on 30 Nov 2016

Community Treasure Hunt

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

Start Hunting!