How to use multiple grouping variables in boxplot? Why box plot grouping variable 'not same length' error?

9 views (last 30 days)
1. How do I use multiple grouping variables?
2. How do I resolve the borrowing incorrect error?
Error message:
>> BoxplotGICategoriesDVH
Error using boxplot>straightenX (line 969)
G must be the same length as X or the same length as the
number of columns in X.
Error in boxplot (line 273)
[xDat,gDat,origRow,xlen,gexplicit,origInd,origNumXCols]
= straightenX(x,g);
Error in BoxplotGICategoriesDVH (line 8)
boxplot(data,group)
Verifying that G is "the same length as the number of columns in X":
>> length(group)
ans =
3
>> data
data =
4 18 30
5 8 9
Code causing the error:
data = round(10*rand(2,3));
data(:,2)=2*data(:,2);
data(:,3)=3*data(:,3);
group = cell(3,1);
group{1} = {'1','a'};
group{2} = {'2','b'};
group{3} = {'3','c'};
boxplot(data,group)
How do I resolve this error?
  1 Comment
Daniel Bridges
Daniel Bridges on 27 Feb 2018
Edited: Daniel Bridges on 27 Feb 2018
For a simple example, I'm currently trying to boxplot to compare data A and B from year 1 and 2; the first two columns being data A, columns 2 and 4 being year 2:
data = round(10*rand(10,4));
data(:,2)=2*data(:,2);
data(:,4)=4*data(:,4);
The end goal is to group by three toxicity categories over 60 dose intervals. I'm trying here just to learn the syntax, what code MATLAB wants to see to get the correct boxplot.

Sign in to comment.

Accepted Answer

Daniel Bridges
Daniel Bridges on 28 Feb 2018
Edited: Daniel Bridges on 28 Feb 2018
This StackOverflow answer appears to contain the solution I'm seeking.
This code works:
data = round(10*rand(10,4));
data(:,2)=2*data(:,2);
data(:,3)=3*data(:,3);
data(:,4)=2*3*data(:,4);
group = {reshape(repmat('A':'B',2,1),4,1) repmat((1:2)',2,1)};
boxplot(data,group)
Apparently the error was caused by having a cell array with cells being 1x4 vectors instead of 4x1 vectors.
Removing the repmat and reshape commands for simplicity:
data = round(10*rand(10,4));
data(:,2)=2*data(:,2);
data(:,3)=3*data(:,3);
data(:,4)=2*3*data(:,4);
group{1} = {'1';'1';'2';'2'};
group{2} = {'a';'b';'a';'b'};
boxplot(data,badgroup)
The problem was caused by using commas instead of semi-colons, i.e. a row vector instead of a column vector.

More Answers (1)

Jan
Jan on 27 Feb 2018
I'm not sure what you are exactly asking for. But maybe this helps:
data = round(10*rand(2,3));
data(:,2) = 2*data(:,2);
data(:,3) = 3*data(:,3);
group = {'a', 'b', 'c'}
boxplot(data, group)
  2 Comments
Daniel Bridges
Daniel Bridges on 28 Feb 2018
Edited: Daniel Bridges on 28 Feb 2018
No, I want to use multiple grouping variables, e.g. data class and year. You have given three data types: How do you also group by an additional category, e.g. year, at the same time? See the comment to my OP.
For my project, I want to group by toxicity grade and dose interval, i.e. two or three box plots per dose interval. I must group the data both by toxicity level and by dose exposure. Two grouping variables, not only one. MATLAB documentation only provides an example for one grouping variable; I consider this an oversight for beginners.
So I have asked:
1. What causes that incorrect error message?
2. How do I use multiple grouping variables with boxplot?
For example, the following code reproduces the 'same length' error message -- I want to group by multiple variables, e.g. a and b and 1 and 2:
data = round(10*rand(10,4));
data(:,2)=2*data(:,2);
data(:,4)=4*data(:,4);
group = cell(4,1);
group{1} = {'1','a'};
group{2} = {'2','a'};
group{3} = {'1','b'};
group{4} = {'2','b'};
boxplot(data,group)
So does this attempt:
data = round(10*rand(10,4));
data(:,2)=2*data(:,2);
data(:,4)=4*data(:,4);
group{1} = {'1','1','2','2'};
group{2} = {'a','b','a','b'};
boxplot(data,group)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!