1

I have three vectors if different size (86x1 double), (61x1 double) and (10x1 double).

I try:

figure
boxplot([x1,x2,x3])  

but get "error using horzcat, dimensions of matrices being concatenated are not consistent".

I've tried transposing the vectors but it looks like it combines these into one group and does a boxplot for that. i.e. if i have

boxplot([x1,x2,x3],'Labels','thing1','thing2','thing3')  

I get:

"Struct contents reference from a non-struct array object.

Error using boxplot>assignUserLabels (line 1688) There must be the same number of labels as groups or as the number of elements in X."

user3470496
  • 141
  • 7
  • 33

1 Answers1

2

Since x1, x2 and x3 have different sizes, you cannot convert them into one large matrix. And you cannot use boxplot(x) syntax where x is a matrix.

In this case, you need a grouping variable:

boxplot(x,g) creates a box plot using one or more grouping variables contained in g. boxplot produces a separate box for each set of x values that share the same g value or values.

The document came from https://www.mathworks.com/help/stats/boxplot.html

Here is an example of how to create a grouping variable based on given inputs.

g1 = ones(size(x1)) * 1;
g2 = ones(size(x2)) * 2;
g3 = ones(size(x3)) * 3;
figure()
boxplot([x1; x2; x3], [g1; g2; g3], 'Labels', {'thing1', 'thing2', 'thing3'})
dkato
  • 895
  • 10
  • 28